Skip to content

Commit 1cc9fc3

Browse files
committed
save references to scheduled tasks
1 parent 32082cf commit 1cc9fc3

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

jupyter_collaboration/rooms.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,12 @@ def __init__(
4545

4646
self._cleaner: asyncio.Task | None = None
4747

48+
# the current `self._maybe_save_document()` task.
49+
self._maybe_save_task: asyncio.Task | None = None
50+
4851
# the task currently saving to disk via FileLoader, if any.
4952
self._save_task: asyncio.Task | None = None
5053

51-
# flag that indicates whether a previous call to
52-
# `self._maybe_save_document()` is waiting to save.
53-
#
54-
# if `self._maybe_save_document()` is called while this flag is `True`,
55-
# then the call does nothing, as a previous task will save the Ydoc
56-
# within `self._save_delay` seconds.
57-
self._waiting_to_save = False
58-
5954
# flag that indicates whether a previous call to
6055
# `self._maybe_save_document()` should call itself again after
6156
# `self._save_task` finishes. this is set to `True` when a document
@@ -219,7 +214,18 @@ def _on_document_change(self, target: str, event: Any) -> None:
219214
document. This tasks are debounced (60 seconds by default) so we
220215
need to cancel previous tasks before creating a new one.
221216
"""
222-
asyncio.create_task(self._maybe_save_document())
217+
if self._maybe_save_task and not self._maybe_save_task.done():
218+
# only one `self._maybe_save_task` needs to be running.
219+
#
220+
# if `self._save_task` is already running, then we need to set the
221+
# `self._should_resave` flag to `True` to indicate that the
222+
# `_maybe_save_document()` coroutine needs to be re-scheduled after
223+
# the current `self._save_task` completes.
224+
if self._save_task and not self._save_task.done():
225+
self._should_resave = True
226+
return
227+
228+
self._maybe_save_task = asyncio.create_task(self._maybe_save_document())
223229

224230
async def _maybe_save_document(self) -> None:
225231
"""
@@ -234,23 +240,8 @@ async def _maybe_save_document(self) -> None:
234240
# TODO: fix this; if _save_delay is unset, then this never writes to disk
235241
return
236242

237-
if self._waiting_to_save:
238-
# if a previously spawned `self._maybe_save_document()` task is
239-
# waiting to save, then that task will save the Ydoc within
240-
# `self._save_delay` seconds. therefore we can return early.
241-
return
242-
243-
if self._save_task and not self._save_task.done():
244-
# if we are currently saving, then set the `_should_resave`
245-
# flag. this indicates to the currently running `self._save_task`
246-
# that it should re-call this method after it completes.
247-
self._should_resave = True
248-
return
249-
250243
# save after `self._save_delay` seconds of inactivity
251-
self._waiting_to_save = True
252244
await asyncio.sleep(self._save_delay)
253-
self._waiting_to_save = False
254245

255246
# all async code (i.e. await statements) must be part of this try/except block
256247
# because this coroutine is run in a cancellable task and cancellation is handled here
@@ -271,7 +262,7 @@ async def _maybe_save_document(self) -> None:
271262

272263
if self._should_resave:
273264
self._should_resave = False
274-
asyncio.create_task(self._maybe_save_document())
265+
self._maybe_save_task = asyncio.create_task(self._maybe_save_document())
275266

276267
except asyncio.CancelledError:
277268
return

0 commit comments

Comments
 (0)