MCPcopy
hub / github.com/robtaussig/react-use-websocket / cleanSubscribers

Function cleanSubscribers

src/lib/create-or-join.ts:11–40  ·  view source on GitHub ↗
(
  url: string,
  subscriber: Subscriber,
  optionsRef: MutableRefObject<Options>,
  setReadyState: (readyState: ReadyState) => void,
  clearSocketIoPingInterval: (() => void) | null,
)

Source from the content-addressed store, hash-verified

9//TODO ensure that all onClose callbacks are called
10
11const cleanSubscribers = (
12 url: string,
13 subscriber: Subscriber,
14 optionsRef: MutableRefObject<Options>,
15 setReadyState: (readyState: ReadyState) => void,
16 clearSocketIoPingInterval: (() => void) | null,
17) => {
18 return () => {
19 removeSubscriber(url, subscriber);
20 if (!hasSubscribers(url)) {
21 try {
22 const socketLike = sharedWebSockets[url];
23 if (socketLike instanceof WebSocket) {
24 socketLike.onclose = (event: WebSocketEventMap['close']) => {
25 if (optionsRef.current.onClose) {
26 optionsRef.current.onClose(event);
27 }
28 setReadyState(ReadyState.CLOSED);
29 };
30 }
31 socketLike.close();
32 } catch (e) {
33
34 }
35 if (clearSocketIoPingInterval) clearSocketIoPingInterval();
36
37 delete sharedWebSockets[url];
38 }
39 }
40};
41
42export const createOrJoinSocket = (
43 webSocketRef: MutableRefObject<WebSocketLike | null>,

Callers 1

createOrJoinSocketFunction · 0.85

Calls 2

removeSubscriberFunction · 0.90
hasSubscribersFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…