Skip to content

Commit d1f1105

Browse files
committed
fix: connected promise maintain
1 parent df36bb3 commit d1f1105

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

  • packages/loro-websocket/src/client

packages/loro-websocket/src/client/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ export type RoomJoinStatusValue =
138138
*/
139139
export class LoroWebsocketClient {
140140
private ws!: WebSocket;
141+
// Invariant: `connectedPromise` always represents the next transition to `Connected`.
142+
// - It resolves exactly once, when the currently active socket fires `open`.
143+
// - It is replaced (via `ensureConnectedPromise`) whenever we start a new connect
144+
// attempt or a reconnect is scheduled, so callers blocking on `waitConnected()`
145+
// will wait for the next successful connection.
146+
// - It rejects only when we deliberately stop reconnecting (`close()` or fatal close).
141147
private connectedPromise!: Promise<void>;
142148
private resolveConnected?: () => void;
143149
private rejectConnected?: (e: Error) => void;
@@ -463,6 +469,8 @@ export class LoroWebsocketClient {
463469
this.failAllPendingRooms(err, this.shouldReconnect ? RoomJoinStatus.Reconnecting : RoomJoinStatus.Disconnected);
464470
return;
465471
}
472+
// Renew the promise so callers waiting on waitConnected() block until the next successful reconnect.
473+
this.ensureConnectedPromise();
466474
// Start (or continue) exponential backoff retries
467475
this.setStatus(ClientStatus.Disconnected);
468476
this.scheduleReconnect();

0 commit comments

Comments
 (0)