-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
use-socket-state.hooks.ts
135 lines (118 loc) · 4.24 KB
/
use-socket-state.hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { useRef } from "react";
import { SocketInstance } from "@hyper-fetch/sockets";
import { useDidMount, useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
import { UseSocketStateType, initialSocketState, UseSocketStateProps } from "helpers";
export const useSocketState = <DataType>(socket: SocketInstance, { dependencyTracking }: UseSocketStateProps) => {
const forceUpdate = useForceUpdate();
const onOpenCallback = useRef<null | VoidFunction>(null);
const onCloseCallback = useRef<null | VoidFunction>(null);
const onErrorCallback = useRef<null | ((event: any) => void)>(null);
const onConnectingCallback = useRef<null | VoidFunction>(null);
const onReconnectingCallback = useRef<null | ((attempts: number) => void)>(null);
const onReconnectingStopCallback = useRef<null | ((attempts: number) => void)>(null);
const state = useRef<UseSocketStateType<DataType>>(initialSocketState);
const renderKeys = useRef<Array<keyof UseSocketStateType<DataType>>>([]);
// ******************
// Dependency Tracking
// ******************
const renderKeyTrigger = (keys: Array<keyof UseSocketStateType<DataType>>) => {
const shouldRerender = renderKeys.current.some((renderKey) => keys.includes(renderKey));
if (shouldRerender) forceUpdate();
};
const setRenderKey = (renderKey: keyof UseSocketStateType<DataType>) => {
if (!renderKeys.current.includes(renderKey)) {
renderKeys.current.push(renderKey);
}
};
// ******************
// Turn off dependency tracking
// ******************
useDidUpdate(
() => {
const handleDependencyTracking = () => {
if (!dependencyTracking) {
Object.keys(state.current).forEach((key) => setRenderKey(key as Parameters<typeof setRenderKey>[0]));
}
};
handleDependencyTracking();
},
[dependencyTracking],
true,
);
// ******************
// Actions
// ******************
const actions = {
setData: (data: DataType | null) => {
state.current.data = data;
renderKeyTrigger(["data"]);
},
setConnected: (connected: boolean) => {
state.current.connected = connected;
renderKeyTrigger(["data"]);
},
setConnecting: (connecting: boolean) => {
state.current.connecting = connecting;
renderKeyTrigger(["data"]);
},
setTimestamp: (timestamp: number | null) => {
state.current.timestamp = timestamp;
renderKeyTrigger(["timestamp"]);
},
};
const callbacks = {
onOpen: (callback: VoidFunction) => {
onOpenCallback.current = callback;
},
onClose: (callback: VoidFunction) => {
onCloseCallback.current = callback;
},
onError: <ErrorType = Event>(callback: (event: ErrorType) => void) => {
onErrorCallback.current = callback;
},
onConnecting: (callback: VoidFunction) => {
onConnectingCallback.current = callback;
},
onReconnecting: (callback: (attempts: number) => void) => {
onReconnectingCallback.current = callback;
},
onReconnectingStop: (callback: (attempts: number) => void) => {
onReconnectingStopCallback.current = callback;
},
};
// ******************
// Hook to events
// ******************
useDidMount(() => {
const umountOnError = socket.events.onError((event) => {
onErrorCallback.current?.(event);
});
const umountOnConnecting = socket.events.onConnecting(() => {
actions.setConnecting(true);
onConnectingCallback.current?.();
});
const umountOnOpen = socket.events.onOpen(() => {
actions.setConnected(true);
onOpenCallback.current?.();
});
const umountOnClose = socket.events.onClose(() => {
actions.setConnected(false);
onCloseCallback.current?.();
});
const umountOnReconnecting = socket.events.onReconnecting((attempts) => {
onReconnectingCallback.current?.(attempts);
});
const umountOnReconnectingStop = socket.events.onReconnectingStop((attempts) => {
onReconnectingStopCallback.current?.(attempts);
});
return () => {
umountOnError();
umountOnConnecting();
umountOnOpen();
umountOnClose();
umountOnReconnecting();
umountOnReconnectingStop();
};
});
return [state.current, actions, callbacks, { setRenderKey }] as const;
};