-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseSet.ts
More file actions
61 lines (49 loc) · 1.43 KB
/
useSet.ts
File metadata and controls
61 lines (49 loc) · 1.43 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
import { useCallback, useMemo, useReducer, useRef } from 'react';
export interface StableActions<K> {
add: (key: K) => void;
remove: (key: K) => void;
toggle: (key: K) => void;
reset: () => void;
clear: () => void;
}
export interface Actions<K> extends StableActions<K> {
has: (key: K) => boolean;
}
const useSet = <K>(initial: Iterable<K> = []): [Set<K>, Actions<K>] => {
const initialSet = useMemo(() => new Set<K>(initial), []);
const ref = useRef<Set<K>>(new Set(initialSet));
const [, force] = useReducer((c: number) => c + 1, 0);
const add = useCallback((item: K) => {
if (!ref.current.has(item)) {
ref.current.add(item);
force();
}
}, []);
const remove = useCallback((item: K) => {
if (ref.current.delete(item)) {
force();
}
}, []);
const toggle = useCallback((item: K) => {
if (ref.current.has(item)) ref.current.delete(item);
else ref.current.add(item);
force();
}, []);
const reset = useCallback(() => {
ref.current = new Set(initialSet);
force();
}, [initialSet]);
const clear = useCallback(() => {
if (ref.current.size) {
ref.current.clear();
force();
}
}, []);
const has = useCallback((item: K) => ref.current.has(item), []);
const utils = useMemo<Actions<K>>(
() => ({ add, remove, toggle, reset, clear, has }),
[add, remove, toggle, reset, clear, has]
);
return [ref.current, utils];
};
export default useSet;