File tree Expand file tree Collapse file tree
packages/loro-websocket/src/client Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -138,6 +138,12 @@ export type RoomJoinStatusValue =
138138 */
139139export 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 ( ) ;
You can’t perform that action at this time.
0 commit comments