Use _updateEntry

This commit is contained in:
RMidhunSuresh 2021-12-12 21:12:50 +05:30
parent 39f68e8c2f
commit 544dca3b18

View File

@ -133,28 +133,28 @@ export class Timeline {
const params = updater(e); const params = updater(e);
return params ? params : false; return params ? params : false;
}; };
this._findAndUpdateRelatedEntry(pee.pendingEvent.relatedTxnId, pee.relatedEventId, updateOrFalse); this._findAndUpdateEntryById(pee.pendingEvent.relatedTxnId, pee.relatedEventId, updateOrFalse);
// also look for a relation target to update with this redaction // also look for a relation target to update with this redaction
if (pee.redactingEntry) { if (pee.redactingEntry) {
// redactingEntry might be a PendingEventEntry or an EventEntry, so don't assume pendingEvent // redactingEntry might be a PendingEventEntry or an EventEntry, so don't assume pendingEvent
const relatedTxnId = pee.redactingEntry.pendingEvent?.relatedTxnId; const relatedTxnId = pee.redactingEntry.pendingEvent?.relatedTxnId;
this._findAndUpdateRelatedEntry(relatedTxnId, pee.redactingEntry.relatedEventId, updateOrFalse); this._findAndUpdateEntryById(relatedTxnId, pee.redactingEntry.relatedEventId, updateOrFalse);
} }
} }
_findAndUpdateRelatedEntry(relatedTxnId, relatedEventId, updateOrFalse) { _findAndUpdateEntryById(txnId, eventId, updateOrFalse) {
let found = false; let found = false;
// first, look in local entries based on txn id // first, look in local entries based on txn id
if (relatedTxnId) { if (txnId) {
found = this._localEntries.findAndUpdate( found = this._localEntries.findAndUpdate(
e => e.id === relatedTxnId, e => e.id === txnId,
updateOrFalse, updateOrFalse,
); );
} }
// if not found here, look in remote entries based on event id // if not found here, look in remote entries based on event id
if (!found && relatedEventId) { if (!found && eventId) {
this._remoteEntries.findAndUpdate( this._remoteEntries.findAndUpdate(
e => e.id === relatedEventId, e => e.id === eventId,
updateOrFalse updateOrFalse
); );
} }
@ -229,7 +229,7 @@ export class Timeline {
try { try {
this._remoteEntries.getAndUpdate(entry, Timeline._entryUpdater); this._remoteEntries.getAndUpdate(entry, Timeline._entryUpdater);
// Since this entry changed, all dependent entries should be updated // Since this entry changed, all dependent entries should be updated
entry.dependents?.forEach(e => this._findAndUpdateRelatedEntry(null, e.id, () => true)); entry.dependents?.forEach(e => this._updateEntry(e));
} catch (err) { } catch (err) {
if (err.name === "CompareError") { if (err.name === "CompareError") {
// see FragmentIdComparer, if the replacing entry is on a fragment // see FragmentIdComparer, if the replacing entry is on a fragment
@ -259,11 +259,17 @@ export class Timeline {
for (const entry of entries) { for (const entry of entries) {
const relatedEntry = this._fetchedEventEntries.get(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._updateEntry(e));
} }
} }
} }
_updateEntry(entry) {
const txnId = entry.isPending ? entry.id : null;
const eventId = entry.isPending ? null : entry.id;
this._findAndUpdateEntryById(txnId, eventId, () => true);
}
async _loadRelatedEvents(entries) { async _loadRelatedEvents(entries) {
const entriesNeedingContext = entries.filter(e => !!e.contextEventId); const entriesNeedingContext = entries.filter(e => !!e.contextEventId);
for (const entry of entriesNeedingContext) { for (const entry of entriesNeedingContext) {
@ -279,7 +285,7 @@ export class Timeline {
contextEvent.addDependent(entry); contextEvent.addDependent(entry);
entry.setContextEntry(contextEvent); entry.setContextEntry(contextEvent);
// emit this change // emit this change
this._findAndUpdateRelatedEntry(null, entry.id, () => true); this._updateEntry(entry);
} }
} }
} }