Skip to content

Commit 1b957f1

Browse files
authored
Merge pull request #10 from revu-design/add-onreconnectstop
Add onReconnectStop callback as per react-usewebsocket
2 parents 917d107 + 63bfd9f commit 1b957f1

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ interface Options {
309309
onClose ? : (event: WebSocketEventMap['close']) => void;
310310
onMessage ? : (event: WebSocketEventMap['message']) => void;
311311
onError ? : (event: WebSocketEventMap['error']) => void;
312+
onReconnectStop?: (numAttempts: number) => void;
312313
fromSocketIO ? : boolean;
313314
queryParams ? : {
314315
[key: string]: string | number;
@@ -339,7 +340,8 @@ Number of milliseconds to wait until it attempts to reconnect. Default is 5000.
339340

340341
Each of `Options#onMessage`, `Options#onError`, `Options#onClose`, and `Options#onOpen` will be called on the corresponding WebSocket event, if provided. Each will be passed the same event provided from the WebSocket.
341342

342-
343+
### onReconnectStop
344+
If provided in options, will be called when websocket exceeds reconnect limit, either as provided in the options or the default value of 20.
343345

344346
### share: Boolean
345347

src/lib/attach-listener.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const attachListeners = (
4747
reconnect();
4848
}, optionsRef.current.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL_MS);
4949
} else {
50+
optionsRef.current.onReconnectStop && optionsRef.current.onReconnectStop(reconnectAttempts);
5051
console.error(`Max reconnect attempts of ${reconnectAttempts} exceeded`);
5152
}
5253
}
@@ -60,6 +61,9 @@ export const attachListeners = (
6061
reconnectCount.current++;
6162
reconnect();
6263
}, optionsRef.current.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL_MS);
64+
} else {
65+
optionsRef.current.onReconnectStop && optionsRef.current.onReconnectStop(optionsRef.current.reconnectAttempts as number);
66+
console.error(`Max reconnect attempts of ${optionsRef.current.reconnectAttempts} exceeded`);
6367
}
6468
}
6569
};

src/lib/attach-shared-listeners.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const attachSharedListeners = (
5151
}, subscriber.optionsRef.current.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL_MS);
5252
}
5353
} else {
54+
subscriber.optionsRef.current.onReconnectStop && subscriber.optionsRef.current.onReconnectStop(subscriber.optionsRef.current.reconnectAttempts as number);
5455
console.error(`Max reconnect attempts of ${reconnectAttempts} exceeded`);
5556
}
5657
}

src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Options {
2020
onClose?: (event: WebSocketEventMap['close']) => void;
2121
onMessage?: (event: WebSocketEventMap['message']) => void;
2222
onError?: (event: WebSocketEventMap['error']) => void;
23+
onReconnectStop?: (numAttempts: number) => void;
2324
shouldReconnect?: (event: WebSocketEventMap['close']) => boolean;
2425
reconnectInterval?: number;
2526
reconnectAttempts?: number;

0 commit comments

Comments
 (0)