yuustate 3.10.1 → 3.10.2

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.
Files changed (3) hide show
  1. package/esm/react.mjs +16 -3
  2. package/package.json +1 -1
  3. package/react.js +16 -3
package/esm/react.mjs CHANGED
@@ -51,7 +51,7 @@ const useStoreStateProxy = (storeState) => {
51
51
  };
52
52
  const useStoreStateWithInitializer = (store, initialState) => {
53
53
  const initiatedAt = useRef(new WeakMap([[store, 0]]));
54
- useEffect(() => {
54
+ useIsomorphicLayoutEffect(() => {
55
55
  if (initialState === void 0 || initiatedAt.current.get(store)) return;
56
56
  store.setState(initialState);
57
57
  initiatedAt.current.set(store, Date.now());
@@ -63,8 +63,21 @@ const useStoreStateWithInitializer = (store, initialState) => {
63
63
  const useStoreState = (store, options = {}) => {
64
64
  const [state] = useStoreStateWithInitializer(store, options.initialState);
65
65
  const [trackedState, usedPathsRef] = useStoreStateProxy(state);
66
- const snapshotRef = useRef(store.getState());
67
- const getSnapshot = () => snapshotRef.current;
66
+ const snapshotRef = useRef(state);
67
+ const getSnapshot = () => {
68
+ const prevState = snapshotRef.current;
69
+ const nextState = store.getState();
70
+ const paths = compressPaths(usedPathsRef.current);
71
+ for (const path of paths) {
72
+ const prevVal = getValueByPath(prevState, path);
73
+ const nextVal = getValueByPath(nextState, path);
74
+ if (!Object.is(prevVal, nextVal)) {
75
+ snapshotRef.current = nextState;
76
+ break;
77
+ }
78
+ }
79
+ return snapshotRef.current;
80
+ };
68
81
  useSyncExternalStore(
69
82
  useCallback(
70
83
  (onStoreChange) => {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "yuustate",
3
3
  "description": "Lightweight unified state management for sync and async data.",
4
4
  "private": false,
5
- "version": "3.10.1",
5
+ "version": "3.10.2",
6
6
  "keywords": [
7
7
  "utilities",
8
8
  "store",
package/react.js CHANGED
@@ -53,7 +53,7 @@ const useStoreStateProxy = (storeState) => {
53
53
  };
54
54
  const useStoreStateWithInitializer = (store, initialState) => {
55
55
  const initiatedAt = react.useRef(new WeakMap([[store, 0]]));
56
- react.useEffect(() => {
56
+ useIsomorphicLayoutEffect(() => {
57
57
  if (initialState === void 0 || initiatedAt.current.get(store)) return;
58
58
  store.setState(initialState);
59
59
  initiatedAt.current.set(store, Date.now());
@@ -65,8 +65,21 @@ const useStoreStateWithInitializer = (store, initialState) => {
65
65
  const useStoreState = (store, options = {}) => {
66
66
  const [state] = useStoreStateWithInitializer(store, options.initialState);
67
67
  const [trackedState, usedPathsRef] = useStoreStateProxy(state);
68
- const snapshotRef = react.useRef(store.getState());
69
- const getSnapshot = () => snapshotRef.current;
68
+ const snapshotRef = react.useRef(state);
69
+ const getSnapshot = () => {
70
+ const prevState = snapshotRef.current;
71
+ const nextState = store.getState();
72
+ const paths = compressPaths(usedPathsRef.current);
73
+ for (const path of paths) {
74
+ const prevVal = getValueByPath(prevState, path);
75
+ const nextVal = getValueByPath(nextState, path);
76
+ if (!Object.is(prevVal, nextVal)) {
77
+ snapshotRef.current = nextState;
78
+ break;
79
+ }
80
+ }
81
+ return snapshotRef.current;
82
+ };
70
83
  react.useSyncExternalStore(
71
84
  react.useCallback(
72
85
  (onStoreChange) => {