Fix translation race condition? (#7885)

This commit is contained in:
Bram Kragten 2020-12-03 16:52:09 +01:00 committed by GitHub
parent 3e4955becd
commit 565724d201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -210,7 +210,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
this.__coreProgress = language; this.__coreProgress = language;
try { try {
const result = await getTranslation(null, language); const result = await getTranslation(null, language);
this._updateResources(result.language, result.data); await this._updateResources(result.language, result.data);
} finally { } finally {
this.__coreProgress = undefined; this.__coreProgress = undefined;
} }
@ -226,18 +226,26 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
// before this.hass is even created. In this case our base state comes // before this.hass is even created. In this case our base state comes
// from this._pendingHass instead. Otherwise the first set of strings is // from this._pendingHass instead. Otherwise the first set of strings is
// overwritten when we call _updateHass the second time! // overwritten when we call _updateHass the second time!
const baseHass = this.hass ?? this._pendingHass;
if (language !== (this.hass ?? this._pendingHass).language) {
// the language was changed, abort
return;
}
const resources = { const resources = {
[language]: { [language]: {
...baseHass?.resources?.[language], ...(this.hass ?? this._pendingHass)?.resources?.[language],
...data, ...data,
}, },
}; };
const changes: Partial<HomeAssistant> = { resources }; const changes: Partial<HomeAssistant> = {
if (this.hass && language === this.hass.language) { resources,
changes.localize = await computeLocalize(this, language, resources); localize: await computeLocalize(this, language, resources),
};
if (language === (this.hass ?? this._pendingHass).language) {
this._updateHass(changes);
} }
this._updateHass(changes);
} }
private _refetchCachedHassTranslations( private _refetchCachedHassTranslations(