Improve WebSocket connection state management #14
+19
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #13
Schedule reconnects using setTimeout instead of setInterval
This prevents multiple reconnect intervals from existing at the same time.
Prevent multiple subscribe calls from trying to start concurrent connection attempts
When calling subscribe multiple times while the connection hasn't been established yet, the websocket would call the connect method multiple times. e.g.:
Each of these would call
isConnected()and call the connect method again, becauseonOpenhasn't been reached yet.isConnected()generally doesn't check if the connection is open in this exact moment, but rather if the connection is generally trying to be connected. If a reconnect is attempted, onClose doesn't set this property to false, but it's only set to true once onOpen is called.This PR changes that to make isConnected return true from the first connect() call until the connection closes without a reconnect attempt.
Arguably, this is almost the same as
#shouldConnectand we could consider making a getter for that instead.