Merge pull request #758 from hgiesel/synchook

Add sync_will_start and sync_did_finish hook
This commit is contained in:
Damien Elmes 2020-09-15 21:20:46 +10:00 committed by GitHub
commit a8eba9d341
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 0 deletions

View file

@ -2721,6 +2721,64 @@ class _StyleDidInitFilter:
style_did_init = _StyleDidInitFilter() style_did_init = _StyleDidInitFilter()
class _SyncDidFinishHook:
"""Executes after the sync of the collection concluded.
Note that the media sync did not necessarily finish at this point."""
_hooks: List[Callable[[], None]] = []
def append(self, cb: Callable[[], None]) -> None:
"""()"""
self._hooks.append(cb)
def remove(self, cb: Callable[[], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self) -> None:
for hook in self._hooks:
try:
hook()
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
sync_did_finish = _SyncDidFinishHook()
class _SyncWillStartHook:
_hooks: List[Callable[[], None]] = []
def append(self, cb: Callable[[], None]) -> None:
"""()"""
self._hooks.append(cb)
def remove(self, cb: Callable[[], None]) -> None:
if cb in self._hooks:
self._hooks.remove(cb)
def count(self) -> int:
return len(self._hooks)
def __call__(self) -> None:
for hook in self._hooks:
try:
hook()
except:
# if the hook fails, remove it
self._hooks.remove(hook)
raise
sync_will_start = _SyncWillStartHook()
class _TagEditorDidProcessKeyHook: class _TagEditorDidProcessKeyHook:
_hooks: List[Callable[[TagEdit, QEvent], None]] = [] _hooks: List[Callable[[TagEdit, QEvent], None]] = []

View file

@ -908,9 +908,12 @@ title="%s" %s>%s</button>""" % (
def on_collection_sync_finished(): def on_collection_sync_finished():
self.col.clearUndo() self.col.clearUndo()
self.col.models._clear_cache() self.col.models._clear_cache()
gui_hooks.sync_did_finish()
self.reset() self.reset()
after_sync() after_sync()
gui_hooks.sync_will_start()
sync_collection(self, on_done=on_collection_sync_finished) sync_collection(self, on_done=on_collection_sync_finished)
def maybe_auto_sync_on_open_close(self, after_sync: Callable[[], None]) -> None: def maybe_auto_sync_on_open_close(self, after_sync: Callable[[], None]) -> None:

View file

@ -562,6 +562,14 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest)
args=["diag: aqt.emptycards.EmptyCardsDialog"], args=["diag: aqt.emptycards.EmptyCardsDialog"],
doc="""Allows changing the list of cards to delete.""", doc="""Allows changing the list of cards to delete.""",
), ),
Hook(name="sync_will_start", args=[]),
Hook(
name="sync_did_finish",
args=[],
doc="""Executes after the sync of the collection concluded.
Note that the media sync did not necessarily finish at this point.""",
),
# Adding cards # Adding cards
################### ###################
Hook( Hook(