extra-react-store 0.2.7 → 0.2.9
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/lib/use-selector.js +8 -8
- package/lib/use-selector.js.map +1 -1
- package/package.json +1 -1
- package/src/use-selector.ts +21 -18
package/lib/use-selector.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
2
|
-
import { useIIFE,
|
|
1
|
+
import { useCallback, useRef, useSyncExternalStore } from 'react';
|
|
2
|
+
import { useIIFE, useStrictContext } from 'extra-react-hooks';
|
|
3
3
|
import { isReferenceEqual } from 'extra-utils';
|
|
4
4
|
export function useSelector(context, selector, isEqual = isReferenceEqual) {
|
|
5
|
-
const forceUpdate = useForceUpdate();
|
|
6
5
|
const store = useStrictContext(context);
|
|
7
6
|
const updateRequireRef = useRef(true);
|
|
8
7
|
useIIFE(() => {
|
|
@@ -15,21 +14,22 @@ export function useSelector(context, selector, isEqual = isReferenceEqual) {
|
|
|
15
14
|
valueRef.current = selector(store.getState());
|
|
16
15
|
updateRequireRef.current = false;
|
|
17
16
|
}
|
|
18
|
-
|
|
17
|
+
const getSnapshot = useCallback(() => valueRef.current, [valueRef]);
|
|
18
|
+
return useSyncExternalStore(useCallback((onStoreChange) => {
|
|
19
19
|
return store.subscribe(state => {
|
|
20
20
|
try {
|
|
21
21
|
const newValue = selector(state);
|
|
22
22
|
if (!isEqual(valueRef.current, newValue)) {
|
|
23
23
|
valueRef.current = newValue;
|
|
24
|
-
|
|
24
|
+
updateRequireRef.current = false;
|
|
25
|
+
onStoreChange();
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
catch {
|
|
28
29
|
updateRequireRef.current = true;
|
|
29
|
-
|
|
30
|
+
onStoreChange();
|
|
30
31
|
}
|
|
31
32
|
});
|
|
32
|
-
}, [store, selector, isEqual,
|
|
33
|
-
return valueRef.current;
|
|
33
|
+
}, [store, selector, isEqual, updateRequireRef, valueRef]), getSnapshot, getSnapshot);
|
|
34
34
|
}
|
|
35
35
|
//# sourceMappingURL=use-selector.js.map
|
package/lib/use-selector.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"use-selector.js","sourceRoot":"","sources":["../src/use-selector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAA;AACjE,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAE7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,UAAU,WAAW,CACzB,OAA4B,EAC5B,QAAiC,EACjC,UAA2C,gBAAgB;IAE3D,MAAM,KAAK,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAA;IAEvC,MAAM,gBAAgB,GAAG,MAAM,CAAU,IAAI,CAAC,CAAA;IAC9C,OAAO,CAAC,GAAG,EAAE;QACX,OAAO,GAAG,EAAE;YACV,gBAAgB,CAAC,OAAO,GAAG,IAAI,CAAA;QACjC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IAEd,MAAM,QAAQ,GAAG,MAAM,CAAQ,SAAS,CAAC,CAAA;IACzC,IAAI,gBAAgB,CAAC,OAAO,EAAE,CAAC;QAC7B,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC7C,gBAAgB,CAAC,OAAO,GAAG,KAAK,CAAA;IAClC,CAAC;IAED,MAAM,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;IACpE,OAAO,oBAAoB,CACzB,WAAW,CAAC,CAAC,aAAyB,EAAE,EAAE;QACxC,OAAO,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE;YAC7B,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;gBAEhC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAQ,EAAE,QAAQ,CAAC,EAAE,CAAC;oBAC1C,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAA;oBAC3B,gBAAgB,CAAC,OAAO,GAAG,KAAK,CAAA;oBAChC,aAAa,EAAE,CAAA;gBACjB,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,gBAAgB,CAAC,OAAO,GAAG,IAAI,CAAA;gBAC/B,aAAa,EAAE,CAAA;YACjB,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,CAAC,CAAC,EAC1D,WAAW,EACX,WAAW,CACZ,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/use-selector.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useRef } from 'react'
|
|
2
|
-
import { useIIFE,
|
|
1
|
+
import { useCallback, useRef, useSyncExternalStore } from 'react'
|
|
2
|
+
import { useIIFE, useStrictContext } from 'extra-react-hooks'
|
|
3
3
|
import { StoreContext } from './types.js'
|
|
4
4
|
import { isReferenceEqual } from 'extra-utils'
|
|
5
5
|
|
|
@@ -8,7 +8,6 @@ export function useSelector<State, Value>(
|
|
|
8
8
|
, selector: (state: State) => Value
|
|
9
9
|
, isEqual: (a: Value, b: Value) => boolean = isReferenceEqual
|
|
10
10
|
): Value {
|
|
11
|
-
const forceUpdate = useForceUpdate()
|
|
12
11
|
const store = useStrictContext(context)
|
|
13
12
|
|
|
14
13
|
const updateRequireRef = useRef<boolean>(true)
|
|
@@ -24,21 +23,25 @@ export function useSelector<State, Value>(
|
|
|
24
23
|
updateRequireRef.current = false
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
const getSnapshot = useCallback(() => valueRef.current!, [valueRef])
|
|
27
|
+
return useSyncExternalStore(
|
|
28
|
+
useCallback((onStoreChange: () => void) => {
|
|
29
|
+
return store.subscribe(state => {
|
|
30
|
+
try {
|
|
31
|
+
const newValue = selector(state)
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
if (!isEqual(valueRef.current!, newValue)) {
|
|
34
|
+
valueRef.current = newValue
|
|
35
|
+
updateRequireRef.current = false
|
|
36
|
+
onStoreChange()
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
updateRequireRef.current = true
|
|
40
|
+
onStoreChange()
|
|
35
41
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef])
|
|
42
|
-
|
|
43
|
-
return valueRef.current!
|
|
42
|
+
})
|
|
43
|
+
}, [store, selector, isEqual, updateRequireRef, valueRef])
|
|
44
|
+
, getSnapshot
|
|
45
|
+
, getSnapshot
|
|
46
|
+
)
|
|
44
47
|
}
|