Prevent old HA state from using an old image access token.

This commit is contained in:
Dermot Duffy
2022-03-04 19:48:36 -08:00
parent bf53776c73
commit b947df3e14
4 changed files with 73 additions and 49 deletions
+23 -16
View File
@@ -1,8 +1,7 @@
import { ReactiveController, ReactiveControllerHost } from 'lit';
export class CachedValueController<T> implements ReactiveController {
public value?: T;
protected _value?: T;
protected _host: ReactiveControllerHost;
protected _timerSeconds: number;
protected _callback: () => T;
@@ -21,38 +20,44 @@ export class CachedValueController<T> implements ReactiveController {
this._host.removeController(this);
}
/**
* Get the value.
*/
get value(): T | undefined {
return this._value;
}
/**
* Update the cached value (and reset the timer).
*/
public updateValue(): void {
this.value = this._callback();
this._setTimer();
this._value = this._callback();
}
/**
* Update the value and render it.
* Clear the cached value.
*/
protected _updateValueAndRender(): void {
this.updateValue();
this._host.requestUpdate();
public clearValue(): void {
this._value = undefined;
}
/**
* Remove the timer.
* Disable the timer.
*/
protected _removeTimer(): void {
public stopTimer(): void {
clearInterval(this._timerID);
this._timerID = undefined;
}
/**
* Set the timer.
* Enable the timer. Repeated calls will have no effect.
*/
protected _setTimer(): void {
clearInterval(this._timerID);
public startTimer(): void {
this.stopTimer();
if (this._timerSeconds > 0) {
this._timerID = window.setInterval(() => {
this._updateValueAndRender();
this.updateValue();
this._host.requestUpdate();
}, this._timerSeconds * 1000);
}
}
@@ -61,13 +66,15 @@ export class CachedValueController<T> implements ReactiveController {
* Host has connected to the cache.
*/
hostConnected(): void {
this._updateValueAndRender();
this.updateValue();
this.startTimer();
this._host.requestUpdate();
}
/**
* Host has disconnected from the cache.
*/
hostDisconnected(): void {
this._removeTimer();
this.stopTimer();
}
}