-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
120 lines (109 loc) · 4.14 KB
/
index.ts
File metadata and controls
120 lines (109 loc) · 4.14 KB
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
import * as React from "react";
export type BroadcastChannelData =
| string
| number
| boolean
| Record<string, unknown>
| undefined
| null;
/**
* React hook to create and manage a Broadcast Channel across multiple browser windows.
*
* @param channelName Static name of channel used across the browser windows.
* @param handleMessage Callback to handle the event generated when `message` is received.
* @param handleMessageError [optional] Callback to handle the event generated when `error` is received.
* @returns A function to send/post message on the channel.
* @example
* ```tsx
* import {useBroadcastChannel} from 'react-broadcast-channel';
*
* function App () {
* const postUserIdMessage = useBroadcastChannel('userId', (e) => alert(e.data));
* return (<button onClick={() => postUserIdMessage('ABC123')}>Send UserId</button>);
* }
* ```
* ---
* Works in browser that support Broadcast Channel API natively. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API#browser_compatibility).
* To support other browsers, install and use [broadcastchannel-polyfill](https://www.npmjs.com/package/broadcastchannel-polyfill).
*/
export function useBroadcastChannel<T extends BroadcastChannelData = string>(
channelName: string,
handleMessage?: (event: MessageEvent) => void,
handleMessageError?: (event: MessageEvent) => void
): (data: T) => void {
const [channel] = React.useState<BroadcastChannel | null>(
typeof window !== "undefined" && "BroadcastChannel" in window
? new BroadcastChannel(channelName)
: null
);
useChannelEventListener(channel, "message", handleMessage);
useChannelEventListener(channel, "messageerror", handleMessageError);
return React.useCallback((data: T) => channel?.postMessage(data), [channel]);
}
/**
* React hook to manage state across browser windows. Has the similar signature as `React.useState`.
*
* @param channelName Static name of channel used across the browser windows.
* @param initialState Initial state.
* @returns Tuple of state and setter for the state.
* @example
* ```tsx
* import {useBroadcastState} from 'react-broadcast-channel';
*
* function App () {
* const [count, setCount] = useBroadcastState('count', 0);
* return (
* <div>
* <button onClick={() => setCount(prev => prev - 1)}>Decrement</button>
* <span>{count}</span>
* <button onClick={() => setCount(prev => prev + 1)}>Increment</button>
* </div>
* );
* }
* ```
* ---
* Works in browser that support Broadcast Channel API natively. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API#browser_compatibility).
* To support other browsers, install and use [broadcastchannel-polyfill](https://www.npmjs.com/package/broadcastchannel-polyfill).
*/
export function useBroadcastState<T extends BroadcastChannelData = string>(
channelName: string,
initialState: T
): [T, React.Dispatch<React.SetStateAction<T>>, boolean] {
const [isPending, startTransition] = React.useTransition();
const [state, setState] = React.useState<T>(initialState);
const broadcast = useBroadcastChannel<T>(channelName, (ev) =>
setState(ev.data)
);
const updateState: React.Dispatch<React.SetStateAction<T>> =
React.useCallback(
(input) => {
setState((prev) => {
const newState = typeof input === "function" ? input(prev) : input;
startTransition(() => broadcast(newState));
return newState;
});
},
[broadcast]
);
return [state, updateState, isPending];
}
// Helpers
/** Hook to subscribe/unsubscribe from channel events. */
function useChannelEventListener<K extends keyof BroadcastChannelEventMap>(
channel: BroadcastChannel | null,
event: K,
handler?: (e: BroadcastChannelEventMap[K]) => void
) {
const callbackRef = React.useRef(handler);
if (callbackRef.current !== handler) {
callbackRef.current = handler;
}
React.useEffect(() => {
const callback = callbackRef.current;
if (!channel || !callback) {
return;
}
channel.addEventListener(event, callback);
return () => channel.removeEventListener(event, callback);
}, [channel, event]);
}