1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | import {useCallback, useEffect, useRef} from 'react';
|
14 |
|
15 | interface GlobalListeners {
|
16 | addGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void,
|
17 | addGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void,
|
18 | removeGlobalListener<K extends keyof DocumentEventMap>(el: EventTarget, type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void,
|
19 | removeGlobalListener(el: EventTarget, type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void,
|
20 | removeAllGlobalListeners(): void
|
21 | }
|
22 |
|
23 | export function useGlobalListeners(): GlobalListeners {
|
24 | let globalListeners = useRef(new Map());
|
25 | let addGlobalListener = useCallback((eventTarget, type, listener, options) => {
|
26 |
|
27 | let fn = options?.once ? (...args) => {
|
28 | globalListeners.current.delete(listener);
|
29 | listener(...args);
|
30 | } : listener;
|
31 | globalListeners.current.set(listener, {type, eventTarget, fn, options});
|
32 | eventTarget.addEventListener(type, listener, options);
|
33 | }, []);
|
34 | let removeGlobalListener = useCallback((eventTarget, type, listener, options) => {
|
35 | let fn = globalListeners.current.get(listener)?.fn || listener;
|
36 | eventTarget.removeEventListener(type, fn, options);
|
37 | globalListeners.current.delete(listener);
|
38 | }, []);
|
39 | let removeAllGlobalListeners = useCallback(() => {
|
40 | globalListeners.current.forEach((value, key) => {
|
41 | removeGlobalListener(value.eventTarget, value.type, key, value.options);
|
42 | });
|
43 | }, [removeGlobalListener]);
|
44 |
|
45 | // eslint-disable-next-line arrow-body-style
|
46 | useEffect(() => {
|
47 | return removeAllGlobalListeners;
|
48 | }, [removeAllGlobalListeners]);
|
49 |
|
50 | return {addGlobalListener, removeGlobalListener, removeAllGlobalListeners};
|
51 | }
|
52 |
|
\ | No newline at end of file |