From 4f38e79fe91f83a3bcdb9c632b3ea7b1116b218b Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 15 Jan 2025 10:39:45 +0000 Subject: [PATCH] Ensure room is in store before emitting events from it Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/sliding-sync-sdk.ts | 8 ++------ src/sync.ts | 39 ++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/sliding-sync-sdk.ts b/src/sliding-sync-sdk.ts index 8d63e749c6f..9b15dbb6581 100644 --- a/src/sliding-sync-sdk.ts +++ b/src/sliding-sync-sdk.ts @@ -389,6 +389,8 @@ export class SlidingSyncSdk { return; } room = _createAndReEmitRoom(this.client, roomId, this.opts); + this.client.store.storeRoom(room); + this.client.emit(ClientEvent.Room, room); } await this.processRoomData(this.client, room!, roomData); } @@ -644,8 +646,6 @@ export class SlidingSyncSdk { await this.injectRoomEvents(room, inviteStateEvents); if (roomData.initial) { room.recalculate(); - this.client.store.storeRoom(room); - this.client.emit(ClientEvent.Room, room); } inviteStateEvents.forEach((e) => { this.client.emit(ClientEvent.Event, e); @@ -723,10 +723,6 @@ export class SlidingSyncSdk { room.updateMyMembership(KnownMembership.Join); room.recalculate(); - if (roomData.initial) { - client.store.storeRoom(room); - client.emit(ClientEvent.Room, room); - } // check if any timeline events should bing and add them to the notifEvents array: // we'll purge this once we've fully processed the sync response diff --git a/src/sync.ts b/src/sync.ts index 37ebc9139fb..fb23de18d70 100644 --- a/src/sync.ts +++ b/src/sync.ts @@ -356,7 +356,7 @@ export class SyncApi { let leaveRooms: WrappedRoom[] = []; if (data.rooms?.leave) { - leaveRooms = this.mapSyncResponseToRoomArray(data.rooms.leave); + leaveRooms = this.processRoomArray(data.rooms.leave); } const rooms = await Promise.all( @@ -385,7 +385,6 @@ export class SyncApi { const { timelineEvents } = await this.mapAndInjectRoomEvents(leaveObj); room.recalculate(); - client.store.storeRoom(room); client.emit(ClientEvent.Room, room); this.processEventsForNotifs(room, timelineEvents); @@ -411,6 +410,7 @@ export class SyncApi { const client = this.client; this._peekRoom = this.createRoom(roomId); + client.store.storeRoom(this._peekRoom); return this.client.roomInitialSync(roomId, limit).then((response) => { if (this._peekRoom?.roomId !== roomId) { throw new Error("Peeking aborted"); @@ -468,7 +468,6 @@ export class SyncApi { response.messages.start, ); - client.store.storeRoom(this._peekRoom); client.emit(ClientEvent.Room, this._peekRoom); this.peekPoll(this._peekRoom); @@ -1226,16 +1225,16 @@ export class SyncApi { if (data.rooms) { if (data.rooms.invite) { - inviteRooms = this.mapSyncResponseToRoomArray(data.rooms.invite); + inviteRooms = this.processRoomArray(data.rooms.invite); } if (data.rooms.join) { - joinRooms = this.mapSyncResponseToRoomArray(data.rooms.join); + joinRooms = this.processRoomArray(data.rooms.join); } if (data.rooms.leave) { - leaveRooms = this.mapSyncResponseToRoomArray(data.rooms.leave); + leaveRooms = this.processRoomArray(data.rooms.leave); } if (data.rooms.knock) { - knockRooms = this.mapSyncResponseToRoomArray(data.rooms.knock); + knockRooms = this.processRoomArray(data.rooms.knock); } } @@ -1271,7 +1270,6 @@ export class SyncApi { if (inviteObj.isBrandNewRoom) { room.recalculate(); - client.store.storeRoom(room); client.emit(ClientEvent.Room, room); } else { // Update room state for invite->reject->invite cycles @@ -1469,12 +1467,7 @@ export class SyncApi { // we deliberately don't add accountData to the timeline room.addAccountData(accountDataEvents); - room.recalculate(); - if (joinObj.isBrandNewRoom) { - client.store.storeRoom(room); - client.emit(ClientEvent.Room, room); - } this.processEventsForNotifs(room, timelineEvents); @@ -1500,7 +1493,6 @@ export class SyncApi { room.recalculate(); if (leaveObj.isBrandNewRoom) { - client.store.storeRoom(room); client.emit(ClientEvent.Room, room); } @@ -1527,14 +1519,8 @@ export class SyncApi { await this.injectRoomEvents(room, stateEvents, undefined); - if (knockObj.isBrandNewRoom) { - room.recalculate(); - client.store.storeRoom(room); - client.emit(ClientEvent.Room, room); - } else { - // Update room state for knock->leave->knock cycles - room.recalculate(); - } + // Update room state for knock->leave->knock cycles + room.recalculate(); stateEvents.forEach(function (e) { client.emit(ClientEvent.Event, e); }); @@ -1669,7 +1655,12 @@ export class SyncApi { ); } - private mapSyncResponseToRoomArray( + /** + * For each room check if it is known, if not then create the Room object, add it to the store and set isBrandNewRoom + * @param obj the rooms membership block from /sync to process + * @private + */ + private processRoomArray( obj: Record, ): Array> { // Maps { roomid: {stuff}, roomid: {stuff} } @@ -1683,6 +1674,8 @@ export class SyncApi { let isBrandNewRoom = false; if (!room) { room = this.createRoom(roomId); + client.store.storeRoom(room); + client.emit(ClientEvent.Room, room); isBrandNewRoom = true; } return {