Fix supervisor loading race (#16460)

This commit is contained in:
Bram Kragten 2023-05-08 04:48:16 +02:00 committed by GitHub
parent d04fc53f08
commit 8f75c314f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,8 +63,7 @@ export class SupervisorBaseElement extends urlSyncMixin(
}); });
} }
protected updated(changedProperties: PropertyValues) { protected willUpdate(changedProperties: PropertyValues) {
super.updated(changedProperties);
if (changedProperties.has("hass")) { if (changedProperties.has("hass")) {
const oldHass = changedProperties.get("hass") as const oldHass = changedProperties.get("hass") as
| HomeAssistant | HomeAssistant
@ -83,23 +82,10 @@ export class SupervisorBaseElement extends urlSyncMixin(
this._initializeLocalize(); this._initializeLocalize();
} }
} }
if (changedProperties.has("_collections")) {
if (this._collections) {
const unsubs = Object.keys(this._unsubs);
for (const collection of Object.keys(this._collections)) {
if (!unsubs.includes(collection)) {
this._unsubs[collection] = this._collections[collection].subscribe(
(data) => this._updateSupervisor({ [collection]: data })
);
}
}
}
}
} }
protected _updateSupervisor(obj: Partial<Supervisor>): void { protected _updateSupervisor(update: Partial<Supervisor>): void {
this.supervisor = { ...this.supervisor, ...obj }; this.supervisor = { ...this.supervisor, ...update };
} }
protected firstUpdated(changedProps: PropertyValues): void { protected firstUpdated(changedProps: PropertyValues): void {
@ -123,8 +109,7 @@ export class SupervisorBaseElement extends urlSyncMixin(
"/api/hassio/app/static/translations" "/api/hassio/app/static/translations"
); );
this.supervisor = { this._updateSupervisor({
...this.supervisor,
localize: await computeLocalize<SupervisorKeys>( localize: await computeLocalize<SupervisorKeys>(
this.constructor.prototype, this.constructor.prototype,
language, language,
@ -132,7 +117,7 @@ export class SupervisorBaseElement extends urlSyncMixin(
[language]: data, [language]: data,
} }
), ),
}; });
} }
private async _handleSupervisorStoreRefreshEvent(ev) { private async _handleSupervisorStoreRefreshEvent(ev) {
@ -165,17 +150,15 @@ export class SupervisorBaseElement extends urlSyncMixin(
collection, collection,
supervisorCollection[collection] supervisorCollection[collection]
); );
} if (this._unsubs[collection]) {
}); this._unsubs[collection]();
}
Object.keys(this._collections).forEach((collection) => { this._unsubs[collection] = this._collections[collection].subscribe(
if ( (data) =>
this.supervisor === undefined || this._updateSupervisor({
this.supervisor[collection] === undefined [collection]: data,
) { })
this._updateSupervisor({ );
[collection]: this._collections[collection].state,
});
} }
}); });
} else { } else {