Treat camera unavailability in a more friendly way.

Also creates a `components-lib` directory for major webcomponent support code.
This commit is contained in:
Dermot Duffy
2023-10-05 21:18:03 -07:00
parent 95e42ab767
commit d895c43e67
23 changed files with 207 additions and 89 deletions
@@ -0,0 +1,99 @@
import { ReactiveController, ReactiveControllerHost } from 'lit';
import { Timer } from '../utils/timer';
export class CachedValueController<T> implements ReactiveController {
protected _value?: T;
protected _host: ReactiveControllerHost;
protected _timerSeconds: number;
protected _callback: () => T;
protected _timerStartCallback?: () => void;
protected _timerStopCallback?: () => void;
protected _timer = new Timer();
constructor(
host: ReactiveControllerHost,
timerSeconds: number,
callback: () => T,
timerStartCallback?: () => void,
timerStopCallback?: () => void,
) {
this._timerSeconds = timerSeconds;
this._callback = callback;
this._timerStartCallback = timerStartCallback;
this._timerStopCallback = timerStopCallback;
(this._host = host).addController(this);
}
/**
* Remove the controller for the host.
*/
public removeController(): void {
this.stopTimer();
this._host.removeController(this);
}
/**
* Get the value.
*/
get value(): T | undefined {
return this._value;
}
/**
* Update the cached value.
*/
public updateValue(): void {
this._value = this._callback();
}
/**
* Clear the cached value.
*/
public clearValue(): void {
this._value = undefined;
}
/**
* Disable the timer.
*/
public stopTimer(): void {
if (this._timer.isRunning()) {
this._timer.stop();
this._timerStopCallback?.();
}
}
/**
* Enable the timer. Repeated calls will have no effect.
*/
public startTimer(): void {
this.stopTimer();
this._timerStartCallback?.();
this._timer.startRepeated(this._timerSeconds, () => {
this.updateValue();
this._host.requestUpdate();
});
}
public hasTimer(): boolean {
return this._timer.isRunning();
}
/**
* Host has connected to the cache.
*/
hostConnected(): void {
this.updateValue();
this.startTimer();
this._host.requestUpdate();
}
/**
* Host has disconnected from the cache.
*/
hostDisconnected(): void {
this.clearValue();
this.stopTimer();
}
}