From ef5d2cfa0834725ff4688031d802579955ee68a2 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 2 Jun 2019 18:15:40 +0200 Subject: [PATCH] chunk is in reverse-chronological order for backward pagination --- .../room/timeline/persistence/GapWriter.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/matrix/room/timeline/persistence/GapWriter.js b/src/matrix/room/timeline/persistence/GapWriter.js index ad721586..ba25f909 100644 --- a/src/matrix/room/timeline/persistence/GapWriter.js +++ b/src/matrix/room/timeline/persistence/GapWriter.js @@ -43,15 +43,17 @@ export default class GapWriter { } _storeEvents(events, startKey, direction, txn) { - const entries = new Array(events.length); - const reducer = direction.isBackward ? Array.prototype.reduceRight : Array.prototype.reduce; - reducer.call(events, (key, event, i) => { + const entries = []; + // events is in reverse chronological order for backwards pagination, + // e.g. order is moving away from the `from` point. + let key = startKey; + for(let event of events) { key = key.nextKeyForDirection(direction); - const eventEntry = createEventEntry(key, this._roomId, event); - txn.timelineEvents.insert(eventEntry); - entries[i] = new EventEntry(eventEntry, this._fragmentIdComparer); - return key; - }, startKey); + const eventStorageEntry = createEventEntry(key, this._roomId, event); + txn.timelineEvents.insert(eventStorageEntry); + const eventEntry = new EventEntry(eventStorageEntry, this._fragmentIdComparer); + directionalAppend(entries, eventEntry, direction); + } return entries; }