Create a new Loro WebSocket adapter.
Unique identifier for this adapter instance
URL of the Loro WebSocket server to connect to
VS Code Webview instance to send messages to
Private ReadonlyadapterUnique identifier for this adapter instance
PrivateisFlag indicating whether the adapter has been disposed
Private ReadonlymaxMaximum number of messages to queue before dropping new ones
PrivatemaxMaximum delay in milliseconds for exponential backoff
PrivatemessageQueue of messages waiting to be sent when connection is established
PrivatereconnectCurrent delay in milliseconds before next reconnection attempt
PrivatereconnectTimer for scheduled reconnection attempts
Private ReadonlywebsocketURL of the Loro WebSocket server to connect to
Private ReadonlywebviewVS Code Webview instance to send messages to
PrivatewsThe underlying WebSocket connection to the Loro server
Connect to the WebSocket server. Establishes a new WebSocket connection if one doesn't already exist. Sets up event handlers for open, message, close, and error events. Messages received while disconnected are queued and sent upon reconnection. Automatically attempts to reconnect with exponential backoff on close.
void
Disconnect from the WebSocket server. Cancels any pending reconnection timers and closes the WebSocket connection. Safe to call multiple times; handles null checks internally.
void
Clean up resources and prepare for garbage collection. Marks the adapter as disposed, disconnects from the server, and clears the message queue. After disposal, the adapter should not be used.
void
PrivateflushPrivateFlush all queued messages to the WebSocket server. Called when the WebSocket connection is first established. Clears the queue after sending to prevent duplicate transmissions.
Handle incoming messages from the webview. Routes messages to appropriate handlers:
The message from the webview with type, adapterId, and optional data
void
Check if the WebSocket is currently connected and ready to send. Verifies that the WebSocket exists and readyState is 1 (OPEN).
true if connected and ready, false otherwise
PrivateschedulePrivateSchedule a reconnection attempt with exponential backoff.
Calculates delay using reconnectDelay * 2 up to maxReconnectDelay.
Only schedules if no reconnection is already pending.
Send a message to the WebSocket server. Supports multiple data types: string (JSON), array (binary/Loro update bytes), or object (JSON). If not connected, messages are queued up to maxQueueSize and sent when connection establishes. Queue overflow is silently dropped to prevent memory issues.
The data to send (string, array, or object)
void
PrivatesendPrivateSend a message to the webview via postMessage. Wraps the call in try-catch to handle serialization or connection errors.
The message to send to the webview
WebSocket adapter that runs in the extension (Node.js context). Manages WebSocket connection to Loro collaboration server.