Pre-fetch events from a larger range than the timeline shows

This commit is contained in:
Dermot Duffy
2022-06-20 03:39:50 +00:00
parent 241b9b85a2
commit dce1c4b161
+21 -4
View File
@@ -1,6 +1,7 @@
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import { import {
add, add,
differenceInSeconds,
endOfHour, endOfHour,
format, format,
fromUnixTime, fromUnixTime,
@@ -760,6 +761,17 @@ export class FrigateCardTimelineCore extends LitElement {
this._wasDragged = false; this._wasDragged = false;
} }
/**
* Get a broader prefetch window from a start and end basis.
* @param start The earlier date.
* @param end The later date.
* @returns An object with a `start` and `end` key to prefetch.
*/
protected _getPrefetchWindow(start: Date, end: Date): [Date, Date] {
const delta = differenceInSeconds(end, start);
return [sub(start, { seconds: delta }), add(end, { seconds: delta })];
}
/** /**
* Handle a range change in the timeline. * Handle a range change in the timeline.
* @param properties vis.js provided range information. * @param properties vis.js provided range information.
@@ -774,14 +786,18 @@ export class FrigateCardTimelineCore extends LitElement {
return; return;
} }
if (this.hass && this.cameras && this._timeline && this.timelineConfig) { if (this.hass && this.cameras && this._timeline && this.timelineConfig) {
const [prefetchStart, prefetchEnd] = this._getPrefetchWindow(
properties.start,
properties.end,
);
this._data this._data
.fetchIfNecessary( .fetchIfNecessary(
this, this,
this.hass, this.hass,
this.cameras, this.cameras,
this.timelineConfig.media, this.timelineConfig.media,
properties.start, prefetchStart,
properties.end, prefetchEnd,
this.timelineConfig.show_recordings, this.timelineConfig.show_recordings,
) )
.then(() => { .then(() => {
@@ -1069,13 +1085,14 @@ export class FrigateCardTimelineCore extends LitElement {
? this._getStartEndFromEvent(event) ? this._getStartEndFromEvent(event)
: this._getStartEnd(); : this._getStartEnd();
const [prefetchStart, prefetchEnd] = this._getPrefetchWindow(windowStart, windowEnd);
await this._data.fetchIfNecessary( await this._data.fetchIfNecessary(
this, this,
this.hass, this.hass,
this.cameras, this.cameras,
this.timelineConfig.media, this.timelineConfig.media,
windowStart, prefetchStart,
windowEnd, prefetchEnd,
this.timelineConfig.show_recordings, this.timelineConfig.show_recordings,
); );