Translator tester: Only test URL-specified translators when ready.#21
Conversation
Wait for `translatorTestViewsToRun` to be populated, and then call the handler for the URL-specified translators' tests. If the `translatorTestViewsToRun` object is not ready, the handler (`runURLSpecifiedTranslators`) will silently skip the tests it is supposed to run. Fixes zotero#20.
| if(!viewerMode) { | ||
| let translators = await Zotero.Translators.getAllForType(translatorType, true); | ||
| haveTranslators(translators, translatorType); | ||
| whenReadyToRun.push(haveTranslators(translators, translatorType)); |
There was a problem hiding this comment.
This works, but it isn't very good IMO to depend on non-obvious promise behavior.
When you call a function that returns a promise, the code in that function starts executing immediately. It only pauses on the first await. In this case, it awaits pretty early on and we don't await anything in this function between here and the new Promise.all(), so everything works as expected, but that's pretty fragile. If haveTranslators() should only run once translatorTestViewsToRun is ready, we shouldn't call it at all until that's the case.
So instead we could do something like
whenReadyToRun.push({ translators, translatorType });and below:
for (let { translators, translatorType } of whenReadyToRun) {
await haveTranslators(translators, translatorType);
}(That change also makes the different translator types run sequentially instead of in parallel, which is probably more what we want here.)
There was a problem hiding this comment.
How about reverting this change, and simply do (pseudo-javascript)
return or await haveTranslators(translators, translatorType);on this line? This too works, and it's shorter.
I think here, it's not that haveTranslators() has to run after translatorTestViewsToRun is ready. Actually haveTranslators() is the function that makes translatorTestViewsToRun ready by populating it.
The problems in init(), on the line 428 with await Promise.all(... big arrow function), is that the arrow function creates runaway promises. In each loop in .map() the async arrow function calls the promise-returning haveTranslators(), forgets about it, and immediately returns (a promise that resolves to) undefined. So the await Promise.all() doesn't wait for the promises that do the actual work, only the promises that initiate the work (results of calling the arrow function). That's why this await Promise.all(...) resumes prematurely.
Wait for
translatorTestViewsToRunto be populated, and then call the handler for the URL-specified translators' tests. If thetranslatorTestViewsToRunobject is not ready, the handler (runURLSpecifiedTranslators) will silently skip the tests it is supposed to run.Fixes #20.