Anki Forums

Post-sync hook?

What would be the correct place to add a post-sync hook?

I have an addon that updates an external site with data about how many cards are caught up on reviews. Previously I was monkey patching to be able to run the update after syncing to pick up on the reviews done on a mobile device. It broke in 2.1.28. I’d like to do it properly this time.

Probably before the reset on main.py:892

Thanks!

Why before rather than after? I’d like to pass the collection as an argument and I don’t know if the reset affects anything.

reset() rebuilds the review queues. Putting the hook before it ensures that if an add-on modifies things, the queues will accurately reflect the state of the database after the add-ons are done.

Okay, that makes sense. My addon doesn’t modify but instead needs to be able to accurately count cards are/aren’t due in a deck or tag etcetera. Will that be accurate before the reset? If not should my addon run reset itself? Or should there be two separate hooks, one for addons that modify and one for addons that don’t?

Thanks.

If you’re calling getCard() or counts() you’ll need to reset first. If you’re querying the database directly there is no need.

1 Like

Thanks!

Hi Damien, sorry to revive this old thread, but is this info still up to date after the recent changes? I’d also be interested in a hook just when synchronization has finished.

Henrik added a sync_did_finish hook about a year ago.

1 Like

EDIT 1: Just realized I’m using legacy hook handling. I’ll try with the new system and let you know if it works. Please ignore this for now.

EDIT 2: With the new hook system the problems mentioned below disappear. :heavy_check_mark: Thanks, Damien!

sync_did_finish doesn’t seem to work for me (Anki 2.1.47):

def show_msg_box():
    showInfo("sync finished")

addHook("sync_did_finish", show_msg_box)

I tried it in the following scenarios:

  • Sync is activated but there is nothing to do.

  • Sync is activated but fails because of database error:
    image

  • Sync is not activated.

I would have expected it to work in all of these. Was that wrong?

Similarly the following functions also don’t seem to register when syncing is currently in progress:

mw.interactiveState()
mw.progress.busy()

Would be great to have a way to check if some kind of dialogue is currently pushed to the foreground of Anki’s main window.