Files
advanced-camera-card/src/cached-value-controller.ts
T
2022-03-05 08:58:01 -08:00

82 lines
1.7 KiB
TypeScript

import { ReactiveController, ReactiveControllerHost } from 'lit';
export class CachedValueController<T> implements ReactiveController {
protected _value?: T;
protected _host: ReactiveControllerHost;
protected _timerSeconds: number;
protected _callback: () => T;
protected _timerID?: number;
constructor(host: ReactiveControllerHost, timerSeconds: number, callback: () => T) {
this._timerSeconds = timerSeconds;
this._callback = callback;
(this._host = host).addController(this);
}
/**
* Remove the controller for the host.
*/
public removeController(): void {
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._startTimer();
}
/**
* Clear the cached value.
*/
public clearValue(): void {
this._value = undefined;
this._stopTimer();
}
/**
* Disable the timer.
*/
protected _stopTimer(): void {
clearInterval(this._timerID);
this._timerID = undefined;
}
/**
* Enable the timer. Repeated calls will have no effect.
*/
protected _startTimer(): void {
this._stopTimer();
if (this._timerSeconds > 0) {
this._timerID = window.setInterval(() => {
this.updateValue();
this._host.requestUpdate();
}, this._timerSeconds * 1000);
}
}
/**
* Host has connected to the cache.
*/
hostConnected(): void {
this.updateValue();
this._host.requestUpdate();
}
/**
* Host has disconnected from the cache.
*/
hostDisconnected(): void {
this.clearValue();
}
}