Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure room is in store before emitting events from it #4617

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ClientEvent, IStoredClientOpts, MatrixClient } from "./client.ts";
import {
ISyncStateData,
SyncState,
_createAndReEmitRoom,
_createAndSetupRoom,
SyncApiOptions,
defaultClientOpts,
defaultSyncApiOpts,
Expand Down Expand Up @@ -388,7 +388,7 @@ export class SlidingSyncSdk {
logger.debug("initial flag not set but no stored room exists for room ", roomId, roomData);
return;
}
room = _createAndReEmitRoom(this.client, roomId, this.opts);
room = _createAndSetupRoom(this.client, roomId, this.opts);
}
await this.processRoomData(this.client, room!, roomData);
}
Expand Down Expand Up @@ -644,7 +644,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) => {
Expand Down Expand Up @@ -724,8 +723,7 @@ export class SlidingSyncSdk {

room.recalculate();
if (roomData.initial) {
client.store.storeRoom(room);
client.emit(ClientEvent.Room, room);
this.client.emit(ClientEvent.Room, room);
}

// check if any timeline events should bing and add them to the notifEvents array:
Expand Down
41 changes: 24 additions & 17 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class SyncApi {
}

public createRoom(roomId: string): Room {
const room = _createAndReEmitRoom(this.client, roomId, this.opts);
const room = _createAndSetupRoom(this.client, roomId, this.opts);

room.on(RoomStateEvent.Marker, (markerEvent, markerFoundOptions) => {
this.onMarkerStateEvent(room, markerEvent, markerFoundOptions);
Expand Down Expand Up @@ -356,7 +356,7 @@ export class SyncApi {

let leaveRooms: WrappedRoom<ILeftRoom>[] = [];
if (data.rooms?.leave) {
leaveRooms = this.mapSyncResponseToRoomArray(data.rooms.leave);
leaveRooms = this.processRoomArray(data.rooms.leave);
}

const rooms = await Promise.all(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -468,7 +467,6 @@ export class SyncApi {
response.messages.start,
);

client.store.storeRoom(this._peekRoom);
client.emit(ClientEvent.Room, this._peekRoom);

this.peekPoll(this._peekRoom);
Expand Down Expand Up @@ -1226,16 +1224,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);
}
}

Expand Down Expand Up @@ -1271,7 +1269,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
Expand Down Expand Up @@ -1469,10 +1466,8 @@ 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);
}

Expand Down Expand Up @@ -1500,7 +1495,6 @@ export class SyncApi {

room.recalculate();
if (leaveObj.isBrandNewRoom) {
client.store.storeRoom(room);
client.emit(ClientEvent.Room, room);
}

Expand Down Expand Up @@ -1529,7 +1523,6 @@ export class SyncApi {

if (knockObj.isBrandNewRoom) {
room.recalculate();
client.store.storeRoom(room);
client.emit(ClientEvent.Room, room);
} else {
// Update room state for knock->leave->knock cycles
Expand Down Expand Up @@ -1669,7 +1662,12 @@ export class SyncApi {
);
}

private mapSyncResponseToRoomArray<T extends ILeftRoom | IJoinedRoom | IInvitedRoom | IKnockedRoom>(
/**
* 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<T extends ILeftRoom | IJoinedRoom | IInvitedRoom | IKnockedRoom>(
obj: Record<string, T>,
): Array<WrappedRoom<T>> {
// Maps { roomid: {stuff}, roomid: {stuff} }
Expand Down Expand Up @@ -1940,9 +1938,17 @@ export class SyncApi {
};
}

// /!\ This function is not intended for public use! It's only exported from
// here in order to share some common logic with sliding-sync-sdk.ts.
export function _createAndReEmitRoom(client: MatrixClient, roomId: string, opts: Partial<IStoredClientOpts>): Room {
/**
* Creates a new room, wires up the client remitter for it, and adds it to the client store.
* @param client the client using which to create the room
* @param roomId the ID of the room to create
* @param opts the options to use when creating the room
* @internal
* @privateRemarks
* /!\ This function is not intended for public use! It's only exported from
* here in order to share some common logic with sliding-sync-sdk.ts.
*/
export function _createAndSetupRoom(client: MatrixClient, roomId: string, opts: Partial<IStoredClientOpts>): Room {
const { timelineSupport } = client;

const room = new Room(roomId, client, client.getUserId()!, {
Expand Down Expand Up @@ -1983,6 +1989,7 @@ export function _createAndReEmitRoom(client: MatrixClient, roomId: string, opts:
RoomMemberEvent.Membership,
]);
});
client.store.storeRoom(room);

return room;
}
Loading