From 0407829b260974ee049ac313067836c577eee0df Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 3 Jun 2019 00:11:29 +0200 Subject: [PATCH] fix filling gaps with overlapping events although event order remains wrong, as events are reversed. step before removing premature optimization, so it's in the git commit log --- .../room/timeline/persistence/GapWriter.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/matrix/room/timeline/persistence/GapWriter.js b/src/matrix/room/timeline/persistence/GapWriter.js index dfef468e..9d22407a 100644 --- a/src/matrix/room/timeline/persistence/GapWriter.js +++ b/src/matrix/room/timeline/persistence/GapWriter.js @@ -8,15 +8,22 @@ export default class GapWriter { this._storage = storage; this._fragmentIdComparer = fragmentIdComparer; } - + // events is in reverse-chronological order (last event comes at index 0) if backwards async _findOverlappingEvents(fragmentEntry, events, txn) { - const eventIds = events.map(e => e.event_id); const {direction} = fragmentEntry; + // make it in chronological order, so findFirstOrLastOccurringEventId can + // use it's supposed optimization if event ids sorting order correlates with timeline chronology. + // premature optimization? + if (direction.isBackward) { + events = events.slice().reverse(); + } + const eventIds = events.map(e => e.event_id); const findLast = direction.isBackward; let nonOverlappingEvents = events; let neighbourFragmentEntry; const neighbourEventId = await txn.timelineEvents.findFirstOrLastOccurringEventId(this._roomId, eventIds, findLast); if (neighbourEventId) { + console.log("_findOverlappingEvents neighbourEventId", neighbourEventId); // trim overlapping events const neighbourEventIndex = events.findIndex(e => e.event_id === neighbourEventId); const start = direction.isBackward ? neighbourEventIndex + 1 : 0; @@ -24,10 +31,13 @@ export default class GapWriter { nonOverlappingEvents = events.slice(start, end); // get neighbour fragment to link it up later on const neighbourEvent = await txn.timelineEvents.getByEventId(this._roomId, neighbourEventId); - const neighbourFragment = await txn.timelineFragments.get(neighbourEvent.fragmentId); + console.log("neighbourEvent", {neighbourEvent, start, end, nonOverlappingEvents, events, neighbourEventIndex}); + const neighbourFragment = await txn.timelineFragments.get(this._roomId, neighbourEvent.fragmentId); neighbourFragmentEntry = fragmentEntry.createNeighbourEntry(neighbourFragment); } + console.log("_findOverlappingEvents events", events, nonOverlappingEvents); + return {nonOverlappingEvents, neighbourFragmentEntry}; }