Skip to content
Closed
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
20 changes: 19 additions & 1 deletion robust-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
}
})(function(global, navigator) {

var WebSocket = global.WebSocket
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per appuri/robust-websocket#24 (comment)

The reason for this change is: WebSocket has been overriden by browser extensions and users want to ensure that RobustWebSocket's implementation doesn't change from under them.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocket has been overriden by browser extensions and users want to ensure that RobustWebSocket's implementation doesn't change from under them.

But this can make it worse. Based on the load order (which is determined by what?) RobustWebSocket could close over the extention's implentation, or the native implementation. In the current usage, with lazy invocation, the user's code has more time and control to inject a different implementation.


var RobustWebSocket = function(url, protocols, userOptions) {
var realWs = { close: function() {} },
connectTimeout,
Expand Down Expand Up @@ -93,12 +95,17 @@
}

self.open = function() {
if (realWs.readyState !== WebSocket.OPEN && realWs.readyState !== WebSocket.CONNECTING) {
if (realWs.readyState !== self.OPEN && realWs.readyState !== self.CONNECTING) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't and should not need to reimplement logic that is in the underlying WebSocket.prototype.send method. if it's missing, that's another thing and in that case, throwing an Error indicating it's missing is the most explicit, best experience for the user.

clearPendingReconnectIfNeeded()
reconnectWhenOnlineAgain = false
explicitlyClosed = false

newWebSocket()
} else {
throw Object.assign(
new Error('An attempt was made to use a WebSocket that is not usable'),
{ name: 'InvalidStateError' }
)
}
}

Expand Down Expand Up @@ -239,5 +246,16 @@
}
}

['CONNECTING' , 'OPEN', 'CLOSING', 'CLOSED'].forEach(function (name) {
const descriptor = {
writable: false,
enumerable: true,
configurable: false,
value: WebSocket[name],
}
Object.defineProperty(RobustWebSocket, name, descriptor)
Object.defineProperty(RobustWebSocket.prototype, name, descriptor)
})

return RobustWebSocket
}, typeof window != 'undefined' ? window : (typeof global != 'undefined' ? global : this));