Skip to content

Commit

Permalink
feat(client): handle initialPayload and remove autoOpen option
Browse files Browse the repository at this point in the history
Updated the WebSocket connection handling to manage the `initialPayload` more effectively and removed the `autoOpen` option from `ConnectOptions`. This change improves the initial connection process by automatically sending the `initialPayload` if it exists when the WebSocket connection opens. The removal of the `autoOpen` property simplifies the API and aligns with the existing behavior which requires calling `open` explicitly.
  • Loading branch information
shorwood committed Nov 28, 2024
1 parent d208dee commit 1f06be2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
19 changes: 13 additions & 6 deletions packages/client/websocket/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,31 @@ export class WebSocketChannel<T extends ConnectOptions = ConnectOptions> {
if (this.webSocket) await this.close()
const { url, protocol } = parseConnectOptions(this.channel, this.options)
this.webSocket = new WebSocket(url, protocol)

// --- Return a promise that resolves when the connection is opened.
const promise = new Promise<void>((resolve, rejects) => {
this.webSocket!.addEventListener('error', () => rejects(new Error('Failed to open the WebSocket connection')), { once: true })
this.webSocket!.addEventListener('open', () => {
if (this.options.initialPayload) this.send(this.options.initialPayload as ClientData<T>)
resolve()
}, { once: true })
})

// --- Add the options' hooks to the WebSocket connection.
if (this.options.onOpen) this.on('open', this.options.onOpen, { once: true })
if (this.options.onClose) this.on('close', this.options.onClose, { once: true })
if (this.options.onError) this.on('error', this.options.onError)
if (this.options.onMessage) this.on('message', message => this.options.onMessage!(message))

// --- Reconnect to the server if the connection is closed unexpectedly.
// --- Handle reconnection when the connection is closed unexpectedly.
this.webSocket.addEventListener('close', (event) => {
if (event.code === 1000) return
if (!this.options.autoReconnect) return
if (this.options.reconnectLimit && event.wasClean) return
setTimeout(() => void this.open(), this.options.reconnectDelay ?? 0)
}, { once: true })

// --- Return a promise that resolves when the connection is opened.
return await new Promise<void>((resolve, rejects) => {
this.webSocket!.addEventListener('open', () => resolve(), { once: true })
this.webSocket!.addEventListener('error', () => rejects(new Error('Failed to open the WebSocket connection')), { once: true })
}).then(() => this)
return promise.then(() => this)
}

/**
Expand Down
9 changes: 0 additions & 9 deletions packages/client/websocket/parseConnectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ export interface ConnectOptions<
*/
initialPayload?: Loose<ClientData>

/**
* Automatically open the connection when it is created. If `true`, the connection
* will automatically open when it is created. If `false`, the connection will not
* open when it is created.
*
* @default false
*/
autoOpen?: boolean

/**
* Weather to reconnect the connection when it is closed unexpectedly. If `true`,
* the connection will automatically reconnect when it is closed. If `false`, the
Expand Down

0 comments on commit 1f06be2

Please sign in to comment.