Use map and fetch from Map if available

This commit is contained in:
RMidhunSuresh 2021-12-12 18:52:45 +05:30
parent 4a6293dcdc
commit 0da94e51e0

View file

@ -46,7 +46,7 @@ export class Timeline {
this._readerRequest = null; this._readerRequest = null;
this._allEntries = null; this._allEntries = null;
// Stores event entries that we fetch for reply previews // Stores event entries that we fetch for reply previews
this._fetchedEventEntries = []; this._fetchedEventEntries = new Map();
this.initializePowerLevels(powerLevelsObservable); this.initializePowerLevels(powerLevelsObservable);
} }
@ -254,7 +254,7 @@ export class Timeline {
_updateFetchedEntries(entries) { _updateFetchedEntries(entries) {
for (const entry of 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)) { if (relatedEntry?.addLocalRelation(entry)) {
relatedEntry.dependents.forEach(e => this._findAndUpdateRelatedEntry(null, e.id, () => true)); relatedEntry.dependents.forEach(e => this._findAndUpdateRelatedEntry(null, e.id, () => true));
} }
@ -265,12 +265,12 @@ export class Timeline {
const entriesNeedingContext = entries.filter(e => !!e.contextEventId); const entriesNeedingContext = entries.filter(e => !!e.contextEventId);
for (const entry of entriesNeedingContext) { for (const entry of entriesNeedingContext) {
const id = entry.contextEventId; const id = entry.contextEventId;
let contextEvent = this.getByEventId(id); let contextEvent = this._getTrackedEvent(id);
if (!contextEvent) { if (!contextEvent) {
contextEvent = await this._fetchEventFromStorage(id) ?? await this._fetchEventFromHomeserver(id); contextEvent = await this._fetchEventFromStorage(id) ?? await this._fetchEventFromHomeserver(id);
// this entry was created from storage/hs, so it's not tracked by remoteEntries // 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 // 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) { if (contextEvent) {
contextEvent.addDependent(entry); 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. // tries to prepend `amount` entries to the `entries` list.
/** /**