pocket-state 0.1.21 → 0.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/globalState/hooks.ts +26 -17
- package/src/globalState/store.ts +26 -13
package/package.json
CHANGED
package/src/globalState/hooks.ts
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
1
|
import {useCallback, useRef, useSyncExternalStore} from 'react';
|
|
2
2
|
import type {Store} from './type';
|
|
3
|
+
import {shallow} from '../utils/shallowEqual';
|
|
4
|
+
|
|
5
|
+
type EqualityFn<S> = (a: any, b: any) => boolean;
|
|
6
|
+
|
|
7
|
+
// Overloads
|
|
8
|
+
export function useStore<T>(store: Store<T>): T;
|
|
9
|
+
export function useStore<T, S>(
|
|
10
|
+
store: Store<T>,
|
|
11
|
+
selector: (state: T) => S,
|
|
12
|
+
equalityFn?: EqualityFn<S>,
|
|
13
|
+
): S;
|
|
3
14
|
|
|
4
15
|
export function useStore<T, S = T>(
|
|
5
16
|
store: Store<T>,
|
|
6
17
|
selector?: (state: T) => S,
|
|
18
|
+
equalityFn: EqualityFn<S> = shallow,
|
|
7
19
|
): S {
|
|
8
20
|
const sel = selector ?? ((s: T) => s as unknown as S);
|
|
9
21
|
|
|
10
|
-
const
|
|
11
|
-
const
|
|
22
|
+
const sliceRef = useRef<S>(sel(store.getValue()));
|
|
23
|
+
const eqFn = selector ? equalityFn : Object.is;
|
|
12
24
|
|
|
13
|
-
const subscribe = useCallback(
|
|
14
|
-
(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
lastSliceRef.current = nextSlice;
|
|
25
|
+
const subscribe = useCallback((onChange: () => void) => {
|
|
26
|
+
return store.subscribe(sel, (_prev: S, next: S) => {
|
|
27
|
+
if (!eqFn(sliceRef.current, next)) {
|
|
28
|
+
sliceRef.current = next;
|
|
18
29
|
onChange();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}, []);
|
|
23
33
|
|
|
24
34
|
const getSnapshot = useCallback(() => {
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
lastSliceRef.current = sel(s);
|
|
35
|
+
const next = sel(store.getValue());
|
|
36
|
+
if (!eqFn(sliceRef.current, next)) {
|
|
37
|
+
sliceRef.current = next;
|
|
29
38
|
}
|
|
30
|
-
return
|
|
31
|
-
}, [
|
|
39
|
+
return sliceRef.current;
|
|
40
|
+
}, []);
|
|
32
41
|
|
|
33
42
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
34
43
|
}
|
package/src/globalState/store.ts
CHANGED
|
@@ -48,27 +48,25 @@ export function createStore<T>(
|
|
|
48
48
|
}) as UseStoreGet<T>;
|
|
49
49
|
|
|
50
50
|
function subscribe(selectorOrListener: any, maybeListener?: any) {
|
|
51
|
-
let wrapped:
|
|
51
|
+
let wrapped: Listener<any>;
|
|
52
52
|
|
|
53
53
|
if (typeof maybeListener === 'function') {
|
|
54
54
|
const selector: (s: T) => any = selectorOrListener;
|
|
55
|
-
const listener: Listener<any> = maybeListener;
|
|
56
|
-
|
|
57
55
|
let prevSlice = selector(state);
|
|
58
|
-
wrapped = (
|
|
59
|
-
const
|
|
60
|
-
if (!areEqual(
|
|
61
|
-
|
|
62
|
-
prevSlice =
|
|
56
|
+
wrapped = (next: T) => {
|
|
57
|
+
const slice = selector(next);
|
|
58
|
+
if (!areEqual(slice, prevSlice)) {
|
|
59
|
+
maybeListener(prevSlice, slice);
|
|
60
|
+
prevSlice = slice;
|
|
63
61
|
}
|
|
64
62
|
};
|
|
65
63
|
} else {
|
|
66
64
|
const listener: Listener<T> = selectorOrListener;
|
|
67
|
-
let
|
|
68
|
-
wrapped = (
|
|
69
|
-
if (!areEqual(
|
|
70
|
-
listener(
|
|
71
|
-
|
|
65
|
+
let prev = state;
|
|
66
|
+
wrapped = (next: T) => {
|
|
67
|
+
if (!areEqual(next, prev)) {
|
|
68
|
+
listener(prev, next);
|
|
69
|
+
prev = next;
|
|
72
70
|
}
|
|
73
71
|
};
|
|
74
72
|
}
|
|
@@ -127,7 +125,22 @@ export function createStore<T>(
|
|
|
127
125
|
}
|
|
128
126
|
}
|
|
129
127
|
|
|
128
|
+
function resetToInitialState() {
|
|
129
|
+
const cloned = Array.isArray(initialState)
|
|
130
|
+
? initialState.slice()
|
|
131
|
+
: initialState && typeof initialState === 'object'
|
|
132
|
+
? {...initialState}
|
|
133
|
+
: initialState;
|
|
134
|
+
state = cloned as T;
|
|
135
|
+
emitState();
|
|
136
|
+
}
|
|
137
|
+
|
|
130
138
|
function reset(initialValue?: T | Partial<T>) {
|
|
139
|
+
if (initialState === undefined) {
|
|
140
|
+
resetToInitialState();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
131
144
|
const isObj = (
|
|
132
145
|
v: unknown,
|
|
133
146
|
): v is Record<string | symbol | number, unknown> =>
|