await on loading related events

This commit is contained in:
RMidhunSuresh 2021-12-06 11:49:00 +05:30
parent 2265d198a6
commit e901142661

View file

@ -214,9 +214,9 @@ export class Timeline {
} }
/** @package */ /** @package */
replaceEntries(entries) { async replaceEntries(entries) {
this._addLocalRelationsToNewRemoteEntries(entries); this._addLocalRelationsToNewRemoteEntries(entries);
this._loadRelatedEvents(entries); await this._loadRelatedEvents(entries);
for (const entry of entries) { for (const entry of entries) {
try { try {
this._remoteEntries.getAndUpdate(entry, Timeline._entryUpdater); this._remoteEntries.getAndUpdate(entry, Timeline._entryUpdater);
@ -238,9 +238,9 @@ export class Timeline {
} }
/** @package */ /** @package */
addEntries(newEntries) { async addEntries(newEntries) {
this._addLocalRelationsToNewRemoteEntries(newEntries); this._addLocalRelationsToNewRemoteEntries(newEntries);
this._loadRelatedEvents(newEntries); await this._loadRelatedEvents(newEntries);
this._remoteEntries.setManySorted(newEntries); this._remoteEntries.setManySorted(newEntries);
} }
@ -405,10 +405,10 @@ export function tests() {
// 2. test replaceEntries and addEntries don't fail // 2. test replaceEntries and addEntries don't fail
const event1 = withTextBody("hi!", withSender(bob, createEvent("m.room.message", "!abc"))); const event1 = withTextBody("hi!", withSender(bob, createEvent("m.room.message", "!abc")));
const entry1 = new EventEntry({event: event1, fragmentId: 1, eventIndex: 1}, fragmentIdComparer); const entry1 = new EventEntry({event: event1, fragmentId: 1, eventIndex: 1}, fragmentIdComparer);
timeline.replaceEntries([entry1]); await timeline.replaceEntries([entry1]);
const event2 = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!def"))); const event2 = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!def")));
const entry2 = new EventEntry({event: event2, fragmentId: 1, eventIndex: 2}, fragmentIdComparer); const entry2 = new EventEntry({event: event2, fragmentId: 1, eventIndex: 2}, fragmentIdComparer);
timeline.addEntries([entry2]); await timeline.addEntries([entry2]);
// 3. add local relation (redaction) // 3. add local relation (redaction)
pendingEvents.append(new PendingEvent({data: { pendingEvents.append(new PendingEvent({data: {
roomId, roomId,
@ -432,7 +432,7 @@ export function tests() {
await timeline.load(new User(bob), "join", new NullLogItem()); await timeline.load(new User(bob), "join", new NullLogItem());
timeline.entries.subscribe(new ListObserver()); timeline.entries.subscribe(new ListObserver());
const event = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!abc"))); const event = withTextBody("hi bob!", withSender(alice, createEvent("m.room.message", "!abc")));
timeline.addEntries([new EventEntry({event, fragmentId: 1, eventIndex: 2}, fragmentIdComparer)]); await timeline.addEntries([new EventEntry({event, fragmentId: 1, eventIndex: 2}, fragmentIdComparer)]);
let entry = getIndexFromIterable(timeline.entries, 0); let entry = getIndexFromIterable(timeline.entries, 0);
// 2. add local reaction // 2. add local reaction
pendingEvents.append(new PendingEvent({data: { pendingEvents.append(new PendingEvent({data: {
@ -503,7 +503,7 @@ export function tests() {
await timeline.load(new User(bob), "join", new NullLogItem()); await timeline.load(new User(bob), "join", new NullLogItem());
timeline.entries.subscribe(new ListObserver()); timeline.entries.subscribe(new ListObserver());
// 3. add message to timeline // 3. add message to timeline
timeline.addEntries([messageEntry]); await timeline.addEntries([messageEntry]);
const entry = getIndexFromIterable(timeline.entries, 0); const entry = getIndexFromIterable(timeline.entries, 0);
assert.equal(entry, messageEntry); assert.equal(entry, messageEntry);
assert.equal(entry.annotations["👋"].count, 1); assert.equal(entry.annotations["👋"].count, 1);
@ -512,7 +512,8 @@ export function tests() {
// 1. setup timeline // 1. setup timeline
const pendingEvents = new ObservableArray(); const pendingEvents = new ObservableArray();
const timeline = new Timeline({roomId, storage: await createMockStorage(), const timeline = new Timeline({roomId, storage: await createMockStorage(),
closeCallback: () => {}, fragmentIdComparer, pendingEvents, clock: new MockClock()}); closeCallback: () => { }, fragmentIdComparer, pendingEvents, clock: new MockClock(),
fetchEventFromStorage: () => undefined, fetchEventFromHomeserver: () => undefined});
await timeline.load(new User(bob), "join", new NullLogItem()); await timeline.load(new User(bob), "join", new NullLogItem());
timeline.entries.subscribe(new ListObserver()); timeline.entries.subscribe(new ListObserver());
// 2. add message and reaction to timeline // 2. add message and reaction to timeline
@ -524,7 +525,7 @@ export function tests() {
event: withContent(createAnnotation(messageEntry.id, "👋"), createEvent("m.reaction", "!def", bob)), event: withContent(createAnnotation(messageEntry.id, "👋"), createEvent("m.reaction", "!def", bob)),
fragmentId: 1, eventIndex: 3, roomId fragmentId: 1, eventIndex: 3, roomId
}, fragmentIdComparer); }, fragmentIdComparer);
timeline.addEntries([messageEntry, reactionEntry]); await timeline.addEntries([messageEntry, reactionEntry]);
// 3. redact reaction // 3. redact reaction
pendingEvents.append(new PendingEvent({data: { pendingEvents.append(new PendingEvent({data: {
roomId, roomId,
@ -557,7 +558,7 @@ export function tests() {
}})); }}));
await poll(() => timeline.entries.length === 1); await poll(() => timeline.entries.length === 1);
// 3. add remote reaction target // 3. add remote reaction target
timeline.addEntries([messageEntry]); await timeline.addEntries([messageEntry]);
await poll(() => timeline.entries.length === 2); await poll(() => timeline.entries.length === 2);
const entry = getIndexFromIterable(timeline.entries, 0); const entry = getIndexFromIterable(timeline.entries, 0);
assert.equal(entry, messageEntry); assert.equal(entry, messageEntry);
@ -591,7 +592,7 @@ export function tests() {
}})); }}));
await poll(() => timeline.entries.length === 1); await poll(() => timeline.entries.length === 1);
// 4. add reaction target // 4. add reaction target
timeline.addEntries([new EventEntry({ await timeline.addEntries([new EventEntry({
event: withTextBody("hi bob!", createEvent("m.room.message", messageId, alice)), event: withTextBody("hi bob!", createEvent("m.room.message", messageId, alice)),
fragmentId: 1, eventIndex: 2}, fragmentIdComparer) fragmentId: 1, eventIndex: 2}, fragmentIdComparer)
]); ]);
@ -614,14 +615,14 @@ export function tests() {
const timeline = new Timeline({roomId, storage: await createMockStorage(), closeCallback: () => {}, const timeline = new Timeline({roomId, storage: await createMockStorage(), closeCallback: () => {},
fragmentIdComparer, pendingEvents: new ObservableArray(), clock: new MockClock()}); fragmentIdComparer, pendingEvents: new ObservableArray(), clock: new MockClock()});
await timeline.load(new User(alice), "join", new NullLogItem()); await timeline.load(new User(alice), "join", new NullLogItem());
timeline.addEntries([decryptedEntry]); await timeline.addEntries([decryptedEntry]);
const observer = new ListObserver(); const observer = new ListObserver();
timeline.entries.subscribe(observer); timeline.entries.subscribe(observer);
// 3. replace the entry with one that is not decrypted // 3. replace the entry with one that is not decrypted
// (as would happen when receiving a reaction, // (as would happen when receiving a reaction,
// as it does not rerun the decryption) // as it does not rerun the decryption)
// and check that the decrypted content is preserved // and check that the decrypted content is preserved
timeline.replaceEntries([encryptedEntry]); await timeline.replaceEntries([encryptedEntry]);
const {value, type} = await observer.next(); const {value, type} = await observer.next();
assert.equal(type, "update"); assert.equal(type, "update");
assert.equal(value.eventType, "m.room.message"); assert.equal(value.eventType, "m.room.message");