From 077fa3f6b2452773d82a07213435f18799f6dacf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 30 May 2022 19:03:51 -1000 Subject: [PATCH] Fix live logbook starting empty (#12833) --- src/panels/logbook/ha-logbook.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/panels/logbook/ha-logbook.ts b/src/panels/logbook/ha-logbook.ts index bf5e053027..2241d66038 100644 --- a/src/panels/logbook/ha-logbook.ts +++ b/src/panels/logbook/ha-logbook.ts @@ -310,7 +310,7 @@ export class HaLogbook extends LitElement { // Put newest ones on top. Reverse works in-place so // make a copy first. const newEntries = [...streamMessage.events].reverse(); - if (!this._logbookEntries) { + if (!this._logbookEntries || !this._logbookEntries.length) { this._logbookEntries = newEntries; return; } @@ -320,14 +320,16 @@ export class HaLogbook extends LitElement { return; } const nonExpiredRecords = this._nonExpiredRecords(purgeBeforePythonTime); - this._logbookEntries = - newEntries[0].when >= this._logbookEntries[0].when - ? // The new records are newer than the old records - // append the old records to the end of the new records - newEntries.concat(nonExpiredRecords) - : // The new records are older than the old records - // append the new records to the end of the old records - nonExpiredRecords.concat(newEntries); + this._logbookEntries = !nonExpiredRecords.length + ? // All existing entries expired + newEntries + : newEntries[0].when >= nonExpiredRecords[0].when + ? // The new records are newer than the old records + // append the old records to the end of the new records + newEntries.concat(nonExpiredRecords) + : // The new records are older than the old records + // append the new records to the end of the old records + nonExpiredRecords.concat(newEntries); }; private _updateTraceContexts = throttle(async () => {