Only fetch thumbnails when absolutely necessary.

This commit is contained in:
Dermot Duffy
2022-05-13 20:49:24 -07:00
parent 1d55b09726
commit 30381b9139
+38 -26
View File
@@ -1,6 +1,12 @@
import { Task } from '@lit-labs/task';
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit'; import {
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
unsafeCSS
} from 'lit';
import { customElement, property } from 'lit/decorators.js'; import { customElement, property } from 'lit/decorators.js';
import surroundThumbnailsStyle from '../scss/surround.scss'; import surroundThumbnailsStyle from '../scss/surround.scss';
import { import {
@@ -9,7 +15,7 @@ import {
FrigateCardView, FrigateCardView,
ThumbnailsControlConfig ThumbnailsControlConfig
} from '../types.js'; } from '../types.js';
import { dispatchFrigateCardEvent } from '../utils/basic.js'; import { contentsChanged, dispatchFrigateCardEvent } from '../utils/basic.js';
import { import {
getFirstTrueMediaChildIndex, getFirstTrueMediaChildIndex,
multipleBrowseMediaQueryMerged multipleBrowseMediaQueryMerged
@@ -36,42 +42,32 @@ export class FrigateCardSurround extends LitElement {
@property({ attribute: true, type: Boolean }) @property({ attribute: true, type: Boolean })
protected fetch?: boolean; protected fetch?: boolean;
@property({ attribute: false }) @property({ attribute: false, hasChanged: contentsChanged })
protected browseMediaParams?: protected browseMediaParams?:
| BrowseMediaQueryParameters | BrowseMediaQueryParameters
| BrowseMediaQueryParameters[]; | BrowseMediaQueryParameters[];
protected _browseTask = new Task(this, this._fetchMedia.bind(this), () => [
this.view,
this.browseMediaParams,
this.fetch,
]);
/** /**
* Fetch thumbnail media when a target is not specified in the view (e.g. for * Fetch thumbnail media when a target is not specified in the view (e.g. for
* the live view). * the live view).
* @param param Task parameters. * @param param Task parameters.
* @returns * @returns
*/ */
protected async _fetchMedia([view, browseMediaParams, fetch]: ( protected async _fetchMedia(): Promise<void> {
| Readonly<View> if (
| BrowseMediaQueryParameters !fetch ||
| BrowseMediaQueryParameters[] !this.hass ||
| boolean !this.view ||
| undefined !this.config ||
)[]): Promise<void> { this.config.mode === 'none' ||
view = view as Readonly<View>; this.view.target ||
browseMediaParams = browseMediaParams as !this.browseMediaParams
| BrowseMediaQueryParameters ) {
| BrowseMediaQueryParameters[];
fetch = fetch as boolean;
if (!fetch || !this.hass || !view || view.target || !browseMediaParams) {
return; return;
} }
let parent: FrigateBrowseMediaSource | null; let parent: FrigateBrowseMediaSource | null;
try { try {
parent = await multipleBrowseMediaQueryMerged(this.hass, browseMediaParams); parent = await multipleBrowseMediaQueryMerged(this.hass, this.browseMediaParams);
} catch (e) { } catch (e) {
return dispatchErrorMessageEvent(this, (e as Error).message); return dispatchErrorMessageEvent(this, (e as Error).message);
} }
@@ -97,6 +93,22 @@ export class FrigateCardSurround extends LitElement {
return !!this.config && ['left', 'right'].includes(this.config.mode); return !!this.config && ['left', 'right'].includes(this.config.mode);
} }
/**
* Called before each update.
*/
protected willUpdate(changedProperties: PropertyValues): void {
// Once the component will certainly update, dispatch a media request. Only
// do so if properties relevant to the request have changed (as per their
// hasChanged).
if (
['view', 'targetView', 'fetch', 'browseMediaParams'].some((prop) =>
changedProperties.has(prop),
)
) {
this._fetchMedia();
}
}
/** /**
* Master render method. * Master render method.
* @returns A rendered template. * @returns A rendered template.
@@ -123,7 +135,7 @@ export class FrigateCardSurround extends LitElement {
@frigate-card:thumbnails:open=${(ev: CustomEvent) => changeDrawer(ev, 'open')} @frigate-card:thumbnails:open=${(ev: CustomEvent) => changeDrawer(ev, 'open')}
@frigate-card:thumbnails:close=${(ev: CustomEvent) => changeDrawer(ev, 'close')} @frigate-card:thumbnails:close=${(ev: CustomEvent) => changeDrawer(ev, 'close')}
> >
${this.config?.mode !== 'none' ${this.config && this.config.mode !== 'none'
? html` <frigate-card-thumbnail-carousel ? html` <frigate-card-thumbnail-carousel
slot=${this.config.mode} slot=${this.config.mode}
.config=${this.config} .config=${this.config}