Skip to content

Commit

Permalink
Add retry info to connect events
Browse files Browse the repository at this point in the history
This adds retry info to the connecting and connected events. This helps
us give the user better status about the current state of their network
connection with the websocket.
  • Loading branch information
Chris Pride authored and telemenar committed Oct 4, 2023
1 parent d86f7f7 commit 485d123
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export type Event =
| EventError;

/** @category Client */
export type EventConnectingListener = () => void;
export type EventConnectingListener = (isRetry: boolean) => void;

/**
* The first argument is actually the `WebSocket`, but to avoid
Expand All @@ -124,6 +124,7 @@ export type EventOpenedListener = (socket: unknown) => void;
export type EventConnectedListener = (
socket: unknown,
payload: ConnectionAckMessage['payload'],
wasRetry: boolean,
) => void;

/**
Expand Down Expand Up @@ -637,7 +638,7 @@ export function createClient<
retries++;
}

emitter.emit('connecting');
emitter.emit('connecting', retrying);
const socket = new WebSocketImpl(
typeof url === 'function' ? await url() : url,
GRAPHQL_TRANSPORT_WS_PROTOCOL,
Expand Down Expand Up @@ -760,7 +761,7 @@ export function createClient<
);
clearTimeout(connectionAckTimeout);
acknowledged = true;
emitter.emit('connected', socket, message.payload); // connected = socket opened + acknowledged
emitter.emit('connected', socket, message.payload, retrying); // connected = socket opened + acknowledged
retrying = false; // future lazy connects are not retries
retries = 0; // reset the retries on connect
connected([
Expand Down

0 comments on commit 485d123

Please sign in to comment.