| 1 | from itertools import count |
| 2 | from threading import Lock |
| 3 | |
| 4 | |
| 5 | class TaskStore: |
| 6 | def __init__(self): |
| 7 | self._tasks = {} |
| 8 | self._ids = count(1) |
| 9 | self._lock = Lock() |
| 10 | |
| 11 | def all(self): |
| 12 | with self._lock: |
| 13 | return list(self._tasks.values()) |
| 14 | |
| 15 | def add(self, title, done=False): |
| 16 | with self._lock: |
| 17 | task_id = next(self._ids) |
| 18 | task = {"id": task_id, "title": title, "done": done} |
| 19 | self._tasks[task_id] = task |
| 20 | return task |
| 21 | |
| 22 | def get(self, task_id): |
| 23 | with self._lock: |
| 24 | return self._tasks.get(task_id) |
| 25 | |
| 26 | def remove(self, task_id): |
| 27 | with self._lock: |
| 28 | return self._tasks.pop(task_id, None) is not None |