Skip to content
Merged
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
30 changes: 22 additions & 8 deletions src/hotsock.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,16 @@ class Connection {
*/
#autoReconnect = true

/**
* Set to true when close() is called. Checked in onopen to avoid creating
* orphaned timers when the async WebSocket open event fires after the
* connection has already been intentionally closed.
*
* @type {boolean}
* @private
*/
#closed = false

/**
* Timeout ID for the connected message timeout. If the hotsock.connected
* message is not received within 5 seconds of the WebSocket opening, the
Expand Down Expand Up @@ -883,6 +893,7 @@ class Connection {
* @private
*/
close() {
this.#closed = true
clearTimeout(this.#connectedTimeoutId)

this.#autoReconnect = false
Expand Down Expand Up @@ -919,14 +930,17 @@ class Connection {
clientBindings.forEach(({ messageFn }) => messageFn(wsEvent))

// If hotsock.connected isn't received within 5s, close and reconnect.
this.#connectedTimeoutId = setTimeout(() => {
if (this.#connectionId === undefined) {
this.#client.logger.warn(
"[hotsock] did not receive hotsock.connected within timeout, reconnecting",
)
this.#ws?.close()
}
}, 5000)
// Skip if close() was already called to avoid leaving an orphaned timer.
if (!this.#closed) {
this.#connectedTimeoutId = setTimeout(() => {
if (this.#connectionId === undefined) {
this.#client.logger.warn(
"[hotsock] did not receive hotsock.connected within timeout, reconnecting",
)
this.#ws?.close()
}
}, 5000)
}
}

/**
Expand Down