Fix race in theme setting (#9027)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2021-04-29 21:04:31 +02:00 committed by GitHub
parent 542f169b36
commit 1904c4d057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,8 @@ const mql = matchMedia("(prefers-color-scheme: dark)");
export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
class extends superClass {
private _themeApplied = false;
protected firstUpdated(changedProps) {
super.firstUpdated(changedProps);
this.addEventListener("settheme", (ev) => {
@ -32,7 +34,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
storeState(this.hass!);
});
mql.addListener((ev) => this._applyTheme(ev.matches));
if (mql.matches) {
if (!this._themeApplied && mql.matches) {
applyThemesOnElement(
document.documentElement,
{
@ -51,6 +53,7 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
super.hassConnected();
subscribeThemes(this.hass!.connection, (themes) => {
this._themeApplied = true;
this._updateHass({ themes });
invalidateThemeCache();
this._applyTheme(mql.matches);