Handle logbook updates where the new records are in the middle of the old records ()

This commit is contained in:
J. Nick Koston 2022-09-05 10:47:48 -05:00 committed by GitHub
parent 8ee9655bd5
commit 5842b10a10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -377,16 +377,32 @@ export class HaLogbook extends LitElement {
return; return;
} }
const nonExpiredRecords = this._nonExpiredRecords(purgeBeforePythonTime); const nonExpiredRecords = this._nonExpiredRecords(purgeBeforePythonTime);
this._logbookEntries = !nonExpiredRecords.length
? // All existing entries expired // Entries are sorted in descending order with newest first.
newEntries if (!nonExpiredRecords.length) {
: newEntries[0].when >= nonExpiredRecords[0].when // We have no records left, so we can just replace the list
? // The new records are newer than the old records this._logbookEntries = newEntries;
} else if (
newEntries[newEntries.length - 1].when > // oldest new entry
nonExpiredRecords[0].when // newest old entry
) {
// The new records are newer than the old records
// append the old records to the end of the new records // append the old records to the end of the new records
newEntries.concat(nonExpiredRecords) this._logbookEntries = newEntries.concat(nonExpiredRecords);
: // The new records are older than the old records } else if (
nonExpiredRecords[nonExpiredRecords.length - 1].when > // oldest old entry
newEntries[0].when // newest new entry
) {
// The new records are older than the old records
// append the new records to the end of the old records // append the new records to the end of the old records
nonExpiredRecords.concat(newEntries); this._logbookEntries = nonExpiredRecords.concat(newEntries);
} else {
// The new records are in the middle of the old records
// so we need to re-sort them
this._logbookEntries = nonExpiredRecords
.concat(newEntries)
.sort((a, b) => b.when - a.when);
}
}; };
private _updateTraceContexts = throttle(async () => { private _updateTraceContexts = throttle(async () => {