Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/loro-websocket/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "loro-websocket",
"name": "@karstenda/loro-websocket",
"version": "0.6.2",
"private": false,
"description": "WebSocket client and SimpleServer for syncing CRDTs base on loro-protocol",
Expand Down
26 changes: 16 additions & 10 deletions packages/loro-websocket/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class LoroWebsocketClient {
private roomAuth: Map<string, AuthOption | undefined> = new Map();
private roomStatusListeners: Map<
string,
Set<(s: RoomJoinStatusValue) => void>
Set<(s: RoomJoinStatusValue, messageType?: MessageType, messageCode?: number) => void>
> = new Map();
private socketListeners = new WeakMap<WebSocket, SocketListeners>();

Expand Down Expand Up @@ -625,7 +625,7 @@ export class LoroWebsocketClient {
})
.finally(() => {
this.pendingRooms.delete(id);
this.emitRoomStatus(id, RoomJoinStatus.Joined);
this.emitRoomStatus(id, RoomJoinStatus.Joined, MessageType.JoinResponseOk);
});
},
reject: (error: Error) => {
Expand Down Expand Up @@ -718,7 +718,7 @@ export class LoroWebsocketClient {
} else {
// Remove local room state so client does not auto-retry unless requested
this.cleanupRoom(msg.roomId, msg.crdt);
Comment on lines 719 to +720

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Emit error before cleanup can block immediate rejoin

If an onStatusChange callback attempts to rejoin the room synchronously on RoomJoinStatus.Error, this new ordering emits the status while the room is still registered. join() short-circuits when it sees an active room (activeRooms.get(id) returns truthy), so the callback will get the stale room and no rejoin is issued, even though cleanupRoom runs immediately afterward. This only occurs when clients trigger a rejoin in the status callback, but it makes the documented “caller can rejoin manually” path unreliable. Consider cleaning up before emitting or temporarily allowing join to bypass the existing room in this error path.

Useful? React with 👍 / 👎.

this.emitRoomStatus(roomId, RoomJoinStatus.Error);
this.emitRoomStatus(roomId, RoomJoinStatus.Error, MessageType.RoomError, msg.code);
}
break;
}
Expand Down Expand Up @@ -840,7 +840,7 @@ export class LoroWebsocketClient {
}

this.pendingRooms.delete(id);
this.emitRoomStatus(id, RoomJoinStatus.Joined);
this.emitRoomStatus(id, RoomJoinStatus.Joined, MessageType.JoinResponseOk);
}

private async handleJoinError(
Expand All @@ -857,7 +857,9 @@ export class LoroWebsocketClient {
this.pendingRooms.delete(roomId);
this.emitRoomStatus(
pending.adaptor.crdtType + pending.roomId,
RoomJoinStatus.Error
RoomJoinStatus.Error,
MessageType.JoinError,
msg.code
);
return;
}
Expand Down Expand Up @@ -895,7 +897,9 @@ export class LoroWebsocketClient {
const err = new Error(`Join failed: ${msg.code} - ${msg.message}`);
this.emitRoomStatus(
pending.adaptor.crdtType + pending.roomId,
RoomJoinStatus.Error
RoomJoinStatus.Error,
MessageType.JoinError,
msg.code
);
// Remove active room references so caller can rejoin manually if this was a rejoin
if (pending.isRejoin) {
Expand All @@ -913,7 +917,9 @@ export class LoroWebsocketClient {
this.roomAdaptors.delete(id);
this.roomIds.delete(id);
this.roomAuth.delete(id);
this.roomStatusListeners.delete(id);
// Status listeners are intentionally kept so any emitRoomStatus call
// made immediately after cleanupRoom (e.g. RoomJoinStatus.Error) still
// reaches subscribers. They are cleared when the client is destroyed.
}

waitConnected() {
Expand Down Expand Up @@ -983,7 +989,7 @@ export class LoroWebsocketClient {
roomId: string;
crdtAdaptor: CrdtDocAdaptor;
auth?: AuthOption;
onStatusChange?: (s: RoomJoinStatusValue) => void;
onStatusChange?: (s: RoomJoinStatusValue, messageType?: MessageType, messageCode?: number) => void;
}): Promise<LoroWebsocketClientRoom> {
const id = crdtAdaptor.crdtType + roomId;
// Check if already joining or joined
Expand Down Expand Up @@ -1561,12 +1567,12 @@ export class LoroWebsocketClient {
}
}

private emitRoomStatus(roomKey: string, status: RoomJoinStatusValue) {
private emitRoomStatus(roomKey: string, status: RoomJoinStatusValue, messageType?: MessageType, messageCode?: number) {
const set = this.roomStatusListeners.get(roomKey);
if (!set || set.size === 0) return;
for (const cb of Array.from(set)) {
try {
cb(status);
cb(status, messageType, messageCode);
} catch (err) {
this.logCbError("onRoomStatusChange", err);
}
Expand Down
Loading