Bug 1815339 - Fix a race condition in the LanguageState event handler; r=nordzilla

Differential Revision: https://phabricator.services.mozilla.com/D192248
This commit is contained in:
Greg Tatum
2023-11-02 17:11:12 +00:00
parent 9a1c552a38
commit 2040688319
@@ -1459,9 +1459,15 @@ var TranslationsPanel = new (class {
}
break;
}
case "TranslationsParent:LanguageState":
// Check this value after every `await` to guard against race conditions.
case "TranslationsParent:LanguageState": {
// Check these values after every `await` to guard against race conditions.
const handleEventId = ++this.handleEventId;
const win =
gBrowser.selectedBrowser.browsingContext.currentWindowGlobal;
const isRequestStale = () =>
handleEventId !== this.handleEventId ||
win !== gBrowser.selectedBrowser.browsingContext.currentWindowGlobal;
const {
detectedLanguages,
requestedTranslationPair,
@@ -1501,8 +1507,7 @@ var TranslationsPanel = new (class {
(hasSupportedLanguage &&
(await TranslationsParent.getIsTranslationsEngineSupported()))
) {
if (handleEventId !== this.handleEventId) {
// A new handleEvent was received, this one is stale.
if (isRequestStale()) {
return;
}
button.hidden = false;
@@ -1569,8 +1574,7 @@ var TranslationsPanel = new (class {
PageActions.sendPlacedInUrlbarTrigger(button);
}
} else {
if (handleEventId !== this.handleEventId) {
// A new handleEvent was received, this one is stale.
if (isRequestStale()) {
return;
}
this.#hideTranslationsButton();
@@ -1605,6 +1609,7 @@ var TranslationsPanel = new (class {
console.error("Unknown translation error", error);
}
break;
}
}
};
})();