Skip to content

Commit

Permalink
fix(client): Close event's wasClean is not necessary
Browse files Browse the repository at this point in the history
Closes: enisdenjo#81
  • Loading branch information
enisdenjo committed Dec 3, 2020
1 parent 5e1fe99 commit 2c65f0e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function createClient(options: ClientOptions): Client {
state.locks++;

socket.addEventListener('close', listener);
function listener(event: CloseEvent) {
function listener(event: LikeCloseEvent) {
cancellerRef.current = null;
state.locks--;
socket.removeEventListener('close', listener);
Expand Down Expand Up @@ -422,7 +422,7 @@ export function createClient(options: ClientOptions): Client {
*/
function shouldRetryConnectOrThrow(errOrCloseEvent: unknown): boolean {
// throw non `CloseEvent`s immediately, something else is wrong
if (!isCloseEvent(errOrCloseEvent)) {
if (!isLikeCloseEvent(errOrCloseEvent)) {
throw errOrCloseEvent;
}

Expand Down Expand Up @@ -597,8 +597,16 @@ export function createClient(options: ClientOptions): Client {
};
}

function isCloseEvent(val: unknown): val is CloseEvent {
return isObject(val) && 'code' in val && 'reason' in val && 'wasClean' in val;
/** Minimal close event interface required by the lib for error and socket close handling. */
interface LikeCloseEvent {
/** Returns the WebSocket connection close code provided by the server. */
readonly code: number;
/** Returns the WebSocket connection close reason provided by the server. */
readonly reason: string;
}

function isLikeCloseEvent(val: unknown): val is LikeCloseEvent {
return isObject(val) && 'code' in val && 'reason' in val;
}

function isWebSocket(val: unknown): val is typeof WebSocket {
Expand Down

0 comments on commit 2c65f0e

Please sign in to comment.