@octanejs/zustand 0.1.71 → 0.1.72
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 +3 -3
- package/src/traditional.ts +48 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/zustand",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.72",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -47,12 +47,12 @@
|
|
|
47
47
|
"zustand": "^5.0.15"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"octane": "^0.
|
|
50
|
+
"octane": "^0.19.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@tsrx/react": "^0.4.5",
|
|
54
54
|
"esbuild": "^0.28.1",
|
|
55
|
-
"octane": "0.
|
|
55
|
+
"octane": "0.19.0",
|
|
56
56
|
"react": "19.2.7",
|
|
57
57
|
"react-dom": "19.2.7",
|
|
58
58
|
"vitest": "^4.1.10"
|
package/src/traditional.ts
CHANGED
|
@@ -21,14 +21,20 @@ import type { StateCreator, StoreApi } from 'zustand/vanilla';
|
|
|
21
21
|
|
|
22
22
|
type ExtractState<S> = S extends { getState: () => infer T } ? T : never;
|
|
23
23
|
type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'getInitialState' | 'subscribe'>;
|
|
24
|
+
type ReadSnapshot<T> = () => T;
|
|
24
25
|
|
|
25
26
|
const identity = <T>(arg: T): T => arg;
|
|
27
|
+
const EMPTY_ARGS: [] = [];
|
|
26
28
|
|
|
27
29
|
const subSlot = createSubSlot({ tagPrefix: ':wsel:' });
|
|
28
30
|
|
|
29
|
-
interface SelectionCell<
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
interface SelectionCell<TState, StateSlice> {
|
|
32
|
+
snapshot: TState;
|
|
33
|
+
serverApi: ReadonlyStoreApi<TState> | null;
|
|
34
|
+
read: ReadSnapshot<TState>;
|
|
35
|
+
selector: (state: TState) => StateSlice;
|
|
36
|
+
equalityFn: ((previous: StateSlice, next: StateSlice) => boolean) | undefined;
|
|
37
|
+
value: StateSlice;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
export function useStoreWithEqualityFn<S extends ReadonlyStoreApi<unknown>>(
|
|
@@ -60,28 +66,51 @@ export function useStoreWithEqualityFn<TState, StateSlice>(
|
|
|
60
66
|
? (userArgs[1] as (a: StateSlice, b: StateSlice) => boolean)
|
|
61
67
|
: undefined;
|
|
62
68
|
|
|
63
|
-
const cache = useRef<SelectionCell<StateSlice
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
);
|
|
67
|
-
// Returns the cached reference while the selection is "equal" → octane's
|
|
68
|
-
// useSyncExternalStore sees an Object.is-equal snapshot and bails out.
|
|
69
|
-
const select = (state: TState): StateSlice => {
|
|
70
|
-
const next = selector(state);
|
|
69
|
+
const cache = useRef<SelectionCell<TState, StateSlice> | null>(null, subSlot(slot, 'sel'));
|
|
70
|
+
const select = (read: ReadSnapshot<TState>, fromServer: boolean): StateSlice => {
|
|
71
|
+
const state = Reflect.apply(read, api, EMPTY_ARGS) as TState;
|
|
71
72
|
const c = cache.current;
|
|
72
|
-
if (c
|
|
73
|
-
const
|
|
74
|
-
|
|
73
|
+
if (c === null) {
|
|
74
|
+
const value = selector(state);
|
|
75
|
+
cache.current = {
|
|
76
|
+
snapshot: state,
|
|
77
|
+
serverApi: fromServer ? api : null,
|
|
78
|
+
read,
|
|
79
|
+
selector,
|
|
80
|
+
equalityFn,
|
|
81
|
+
value,
|
|
82
|
+
};
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
if (
|
|
86
|
+
Object.is(c.snapshot, state) &&
|
|
87
|
+
(c.read === read || (!fromServer && c.serverApi === api)) &&
|
|
88
|
+
c.selector === selector &&
|
|
89
|
+
c.equalityFn === equalityFn
|
|
90
|
+
) {
|
|
91
|
+
// Hydration can switch readers while keeping the same selection.
|
|
92
|
+
if (fromServer || c.serverApi !== null) {
|
|
93
|
+
c.serverApi = fromServer ? api : null;
|
|
94
|
+
c.read = read;
|
|
95
|
+
}
|
|
96
|
+
return c.value;
|
|
75
97
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
98
|
+
|
|
99
|
+
const next = selector(state);
|
|
100
|
+
const same = equalityFn ? equalityFn(c.value, next) : Object.is(c.value, next);
|
|
101
|
+
c.snapshot = state;
|
|
102
|
+
c.serverApi = fromServer ? api : null;
|
|
103
|
+
c.read = read;
|
|
104
|
+
c.selector = selector;
|
|
105
|
+
c.equalityFn = equalityFn;
|
|
106
|
+
if (!same) c.value = next;
|
|
107
|
+
return c.value;
|
|
79
108
|
};
|
|
80
109
|
|
|
81
110
|
return useSyncExternalStore(
|
|
82
111
|
api.subscribe,
|
|
83
|
-
() => select(api.getState
|
|
84
|
-
() => select(api.getInitialState
|
|
112
|
+
() => select(api.getState, false),
|
|
113
|
+
() => select(api.getInitialState, true),
|
|
85
114
|
slot,
|
|
86
115
|
);
|
|
87
116
|
}
|