From 0da94e51e0e96c8e606e409fa2e7b6d72f5f11cb Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Sun, 12 Dec 2021 18:52:45 +0530 Subject: [PATCH] Use map and fetch from Map if available --- src/matrix/room/timeline/Timeline.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/matrix/room/timeline/Timeline.js b/src/matrix/room/timeline/Timeline.js index 0da418ce..0551d223 100644 --- a/src/matrix/room/timeline/Timeline.js +++ b/src/matrix/room/timeline/Timeline.js @@ -46,7 +46,7 @@ export class Timeline { this._readerRequest = null; this._allEntries = null; // Stores event entries that we fetch for reply previews - this._fetchedEventEntries = []; + this._fetchedEventEntries = new Map(); this.initializePowerLevels(powerLevelsObservable); } @@ -254,7 +254,7 @@ export class Timeline { _updateFetchedEntries(entries) { for (const entry of entries) { - const relatedEntry = this._fetchedEventEntries.find(e => e.id === entry.relatedEventId); + const relatedEntry = this._fetchedEventEntries.get(entry.relatedEventId); if (relatedEntry?.addLocalRelation(entry)) { relatedEntry.dependents.forEach(e => this._findAndUpdateRelatedEntry(null, e.id, () => true)); } @@ -265,12 +265,12 @@ export class Timeline { const entriesNeedingContext = entries.filter(e => !!e.contextEventId); for (const entry of entriesNeedingContext) { const id = entry.contextEventId; - let contextEvent = this.getByEventId(id); + let contextEvent = this._getTrackedEvent(id); if (!contextEvent) { contextEvent = await this._fetchEventFromStorage(id) ?? await this._fetchEventFromHomeserver(id); // this entry was created from storage/hs, so it's not tracked by remoteEntries // we track them here so that we can update reply preview of dependents on redaction - this._fetchedEventEntries.push(contextEvent); + this._fetchedEventEntries.set(id, contextEvent); } if (contextEvent) { contextEvent.addDependent(entry); @@ -280,6 +280,10 @@ export class Timeline { } } } + + _getTrackedEvent(id) { + return this.getByEventId(id) ?? this._fetchedEventEntries.get(id); + } // tries to prepend `amount` entries to the `entries` list. /**