Skip to content

Support async itemDone on child translator#29

Open
AbeJellinek wants to merge 1 commit into
zotero:masterfrom
AbeJellinek:async-itemDone
Open

Support async itemDone on child translator#29
AbeJellinek wants to merge 1 commit into
zotero:masterfrom
AbeJellinek:async-itemDone

Conversation

@AbeJellinek
Copy link
Copy Markdown
Member

@zoe-translates pointed out that you can't set an async itemDone handler on a child translator, like

let translate = Zotero.loadTranslator('web');
translate.setTranslator('...');
translate.setHandler('itemDone', (obj, item) => {
	let abstractNote = (await requestJSON('https://...')).description;
	item.abstractNote = abstractNote;
	item.complete();
}
await translate.translate();

Instead, that code will cause translation to fail (no items returned) when it awaits the HTTP request.

It looks like we already support async handlers (in most cases, at least!). We just weren't awaiting handlers set on child safeTranslators, nor were we incrementing/decrementing async processes in order to wait for the itemDone handler to complete when the child translator doesn't await Item#complete(). This PR fixes those two issues. Client translation tests are passing.

@zoe-translates
Copy link
Copy Markdown

Thank you - this will make writing translator code much easier. Right now quite a lot of "itemDone" handlers make use of the blocking doGet() function that we want to get rid of, but we can't without this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants