Fix async update localize (#16667)

This commit is contained in:
Bram Kragten 2023-05-30 12:25:21 +02:00 committed by GitHub
parent 26d4839dfd
commit 4b7a517d98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,6 +53,8 @@ interface LoadedTranslationCategory {
configFlow: boolean;
}
let updateResourcesIteration = 0;
/*
* superClass needs to contain `this.hass` and `this._updateHass`.
*/
@ -362,6 +364,9 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
}
private async _updateResources(language: string, data: any) {
updateResourcesIteration++;
const i = updateResourcesIteration;
// Update the language in hass, and update the resources with the newly
// loaded resources. This merges the new data on top of the old data for
// this language, so that the full translation set can be loaded across
@ -394,11 +399,17 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
const localize = await computeLocalize(this, language, resources);
if (language === (this.hass ?? this._pendingHass).language) {
if (
updateResourcesIteration !== i ||
language !== (this.hass ?? this._pendingHass).language
) {
// if a new iteration has started or the language changed, abort
return;
}
this._updateHass({
localize,
});
}
fireEvent(this, "translations-updated");
}