mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-02 05:57:54 +00:00
Throttle?
This commit is contained in:
parent
d40acc227c
commit
1cd2aa891d
53
src/common/util/throttle.ts
Normal file
53
src/common/util/throttle.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
export const throttle = (callback: Function, wait: number) => {
|
||||||
|
let isCalled = false;
|
||||||
|
|
||||||
|
return function (...args: any) {
|
||||||
|
if (!isCalled) {
|
||||||
|
callback(...args);
|
||||||
|
isCalled = true;
|
||||||
|
setTimeout(() => {
|
||||||
|
isCalled = false;
|
||||||
|
}, wait);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const throttleAndQueue = (
|
||||||
|
callback: Function,
|
||||||
|
wait: number,
|
||||||
|
delay?: number
|
||||||
|
) => {
|
||||||
|
let isCalled = false;
|
||||||
|
let timer: number | undefined;
|
||||||
|
let delaying = false;
|
||||||
|
|
||||||
|
const processQueue = () => {
|
||||||
|
if (isCalled) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
clearInterval(timer);
|
||||||
|
timer = undefined;
|
||||||
|
isCalled = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
const setUpThrottle = () => {
|
||||||
|
delaying = false;
|
||||||
|
|
||||||
|
processQueue(); // start immediately on the first invocation
|
||||||
|
timer = window.setInterval(processQueue, wait);
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isCalled = true;
|
||||||
|
if (timer !== undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (delay === undefined) {
|
||||||
|
setUpThrottle();
|
||||||
|
} else if (!delaying) {
|
||||||
|
delaying = true;
|
||||||
|
setTimeout(setUpThrottle, delay);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
@ -18,6 +18,7 @@ import { getLogbookData, LogbookEntry } from "../../data/logbook";
|
|||||||
import "../../panels/logbook/ha-logbook";
|
import "../../panels/logbook/ha-logbook";
|
||||||
import { haStyle } from "../../resources/styles";
|
import { haStyle } from "../../resources/styles";
|
||||||
import { HomeAssistant } from "../../types";
|
import { HomeAssistant } from "../../types";
|
||||||
|
import { throttleAndQueue } from "../../common/util/throttle";
|
||||||
|
|
||||||
@customElement("ha-more-info-history")
|
@customElement("ha-more-info-history")
|
||||||
export class MoreInfoHistory extends LitElement {
|
export class MoreInfoHistory extends LitElement {
|
||||||
@ -35,6 +36,8 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
|
|
||||||
private _lastLogbookDate?: Date;
|
private _lastLogbookDate?: Date;
|
||||||
|
|
||||||
|
private _throttleHistoryFunction?: Function;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.entityId) {
|
if (!this.entityId) {
|
||||||
return html``;
|
return html``;
|
||||||
@ -45,7 +48,8 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
|
|
||||||
return html`<state-history-charts
|
return html`
|
||||||
|
<state-history-charts
|
||||||
up-to-now
|
up-to-now
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.historyData=${this._stateHistory}
|
.historyData=${this._stateHistory}
|
||||||
@ -72,9 +76,12 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
.userIdToName=${this._persons}
|
.userIdToName=${this._persons}
|
||||||
></ha-logbook>
|
></ha-logbook>
|
||||||
`
|
`
|
||||||
: html`<div class="no-entries">
|
: html`
|
||||||
|
<div class="no-entries">
|
||||||
${this.hass.localize("ui.components.logbook.entries_not_found")}
|
${this.hass.localize("ui.components.logbook.entries_not_found")}
|
||||||
</div>`}`;
|
</div>
|
||||||
|
`}
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated(): void {
|
protected firstUpdated(): void {
|
||||||
@ -85,21 +92,43 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
super.updated(changedProps);
|
super.updated(changedProps);
|
||||||
if (!this.entityId) {
|
if (!this.entityId) {
|
||||||
clearInterval(this._historyRefreshInterval);
|
clearInterval(this._historyRefreshInterval);
|
||||||
|
this._stateHistory = undefined;
|
||||||
|
this._entries = undefined;
|
||||||
|
this._lastLogbookDate = undefined;
|
||||||
|
this._throttleHistoryFunction = undefined;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (changedProps.has("entityId")) {
|
if (changedProps.has("entityId")) {
|
||||||
this._stateHistory = undefined;
|
this._stateHistory = undefined;
|
||||||
this._entries = undefined;
|
this._entries = undefined;
|
||||||
this._lastLogbookDate = undefined;
|
this._lastLogbookDate = undefined;
|
||||||
|
this._throttleHistoryFunction = undefined;
|
||||||
|
|
||||||
this._getStateHistory();
|
this._getStateHistory();
|
||||||
this._getLogBookData();
|
this._getLogBookData();
|
||||||
|
|
||||||
clearInterval(this._historyRefreshInterval);
|
this._throttleHistoryFunction = throttleAndQueue(
|
||||||
this._historyRefreshInterval = window.setInterval(() => {
|
() => {
|
||||||
this._getStateHistory();
|
this._getStateHistory();
|
||||||
this._getLogBookData();
|
this._getLogBookData();
|
||||||
}, 60 * 1000);
|
},
|
||||||
|
10000,
|
||||||
|
5000
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!changedProps.has("hass")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stateObj = this.hass.states[this.entityId];
|
||||||
|
const oldHass = changedProps.get("hass") as HomeAssistant;
|
||||||
|
const oldStateObj = oldHass.states[this.entityId];
|
||||||
|
|
||||||
|
if (stateObj !== oldStateObj && this._throttleHistoryFunction) {
|
||||||
|
this._throttleHistoryFunction();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +137,7 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
this.hass!,
|
this.hass!,
|
||||||
this.entityId,
|
this.entityId,
|
||||||
{
|
{
|
||||||
refresh: 60,
|
refresh: 10,
|
||||||
cacheKey: `more_info.${this.entityId}`,
|
cacheKey: `more_info.${this.entityId}`,
|
||||||
hoursToShow: 24,
|
hoursToShow: 24,
|
||||||
},
|
},
|
||||||
@ -132,7 +161,7 @@ export class MoreInfoHistory extends LitElement {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (this._entries) {
|
if (this._entries) {
|
||||||
this._entries = [...this._entries, ...newEntries];
|
this._entries = [...newEntries, ...this._entries];
|
||||||
} else {
|
} else {
|
||||||
this._entries = newEntries;
|
this._entries = newEntries;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user