Merge pull request #795 from dermotduffy/image-going-crazy

Fix issue with image timers being created recursively
This commit is contained in:
Dermot Duffy
2022-08-07 19:24:21 -07:00
committed by GitHub
2 changed files with 14 additions and 7 deletions
+10 -7
View File
@@ -28,11 +28,10 @@ export class CachedValueController<T> implements ReactiveController {
} }
/** /**
* Update the cached value (and reset the timer). * Update the cached value.
*/ */
public updateValue(): void { public updateValue(): void {
this._value = this._callback(); this._value = this._callback();
this._startTimer();
} }
/** /**
@@ -40,22 +39,24 @@ export class CachedValueController<T> implements ReactiveController {
*/ */
public clearValue(): void { public clearValue(): void {
this._value = undefined; this._value = undefined;
this._stopTimer();
} }
/** /**
* Disable the timer. * Disable the timer.
*/ */
protected _stopTimer(): void { public stopTimer(): void {
clearInterval(this._timerID); if (this._timerID !== undefined) {
window.clearInterval(this._timerID);
}
this._timerID = undefined; this._timerID = undefined;
} }
/** /**
* Enable the timer. Repeated calls will have no effect. * Enable the timer. Repeated calls will have no effect.
*/ */
protected _startTimer(): void { public startTimer(): void {
this._stopTimer(); this.stopTimer();
if (this._timerSeconds > 0) { if (this._timerSeconds > 0) {
this._timerID = window.setInterval(() => { this._timerID = window.setInterval(() => {
this.updateValue(); this.updateValue();
@@ -69,6 +70,7 @@ export class CachedValueController<T> implements ReactiveController {
*/ */
hostConnected(): void { hostConnected(): void {
this.updateValue(); this.updateValue();
this.startTimer();
this._host.requestUpdate(); this._host.requestUpdate();
} }
@@ -77,5 +79,6 @@ export class CachedValueController<T> implements ReactiveController {
*/ */
hostDisconnected(): void { hostDisconnected(): void {
this.clearValue(); this.clearValue();
this.stopTimer();
} }
} }
+4
View File
@@ -145,12 +145,14 @@ export class FrigateCardImage extends LitElement {
connectedCallback(): void { connectedCallback(): void {
super.connectedCallback(); super.connectedCallback();
document.addEventListener('visibilitychange', this._boundVisibilityHandler); document.addEventListener('visibilitychange', this._boundVisibilityHandler);
this._cachedValueController?.startTimer();
} }
/** /**
* Component disconnected callback. * Component disconnected callback.
*/ */
disconnectedCallback(): void { disconnectedCallback(): void {
this._cachedValueController?.stopTimer();
document.removeEventListener('visibilitychange', this._boundVisibilityHandler); document.removeEventListener('visibilitychange', this._boundVisibilityHandler);
super.disconnectedCallback(); super.disconnectedCallback();
} }
@@ -170,6 +172,7 @@ export class FrigateCardImage extends LitElement {
// re-generation of a new URL would generate an unauthorized request // re-generation of a new URL would generate an unauthorized request
// (401), see: // (401), see:
// https://github.com/dermotduffy/frigate-hass-card/issues/398 // https://github.com/dermotduffy/frigate-hass-card/issues/398
this._cachedValueController?.stopTimer();
this._cachedValueController?.clearValue(); this._cachedValueController?.clearValue();
this._forceSafeImage(); this._forceSafeImage();
} else { } else {
@@ -177,6 +180,7 @@ export class FrigateCardImage extends LitElement {
// restore the image src. If the HASS object is old (i.e. browser tab was // restore the image src. If the HASS object is old (i.e. browser tab was
// inactive for some time) this update request may be (correctly) // inactive for some time) this update request may be (correctly)
// rejected. // rejected.
this._cachedValueController?.startTimer();
this.requestUpdate(); this.requestUpdate();
} }
} }