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.
@@ -1,8 +1,7 @@
1
- import { useRef } from 'react';
2
- import { useIIFE, useForceUpdate, useStrictContext } from 'extra-react-hooks';
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
- useIIFE(() => {
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
- forceUpdate();
24
+ updateRequireRef.current = false;
25
+ onStoreChange();
25
26
  }
26
27
  }
27
28
  catch {
28
29
  updateRequireRef.current = true;
29
- forceUpdate();
30
+ onStoreChange();
30
31
  }
31
32
  });
32
- }, [store, selector, isEqual, forceUpdate, valueRef, updateRequireRef]);
33
- return valueRef.current;
33
+ }, [store, selector, isEqual, updateRequireRef, valueRef]), getSnapshot, getSnapshot);
34
34
  }
35
35
  //# sourceMappingURL=use-selector.js.map
@@ -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;AAC9B,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CA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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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-react-store",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "files": [
@@ -1,5 +1,5 @@
1
- import { useRef } from 'react'
2
- import { useIIFE, useForceUpdate, useStrictContext } from 'extra-react-hooks'
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
- useIIFE(() => {
28
- return store.subscribe(state => {
29
- try {
30
- const newValue = selector(state)
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
- if (!isEqual(valueRef.current!, newValue)) {
33
- valueRef.current = newValue
34
- forceUpdate()
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
- } catch {
37
- updateRequireRef.current = true
38
- forceUpdate()
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
  }