dev-react-microstore 3.0.1 → 4.0.0
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/dist/index.d.mts +2 -7
- package/dist/index.d.ts +2 -7
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/src/index.ts +220 -238
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
type StoreListener = () => void;
|
|
2
|
-
|
|
3
|
-
asyncUpdate?: boolean;
|
|
4
|
-
debounceDelay?: number;
|
|
5
|
-
perKeyDebounce?: Partial<Record<keyof T, boolean>>;
|
|
6
|
-
};
|
|
7
|
-
declare function createStoreState<T extends object>(initialState: T, options?: StoreOptions<T>): {
|
|
2
|
+
declare function createStoreState<T extends object>(initialState: T): {
|
|
8
3
|
get: () => T;
|
|
9
|
-
set: (next: Partial<T
|
|
4
|
+
set: (next: Partial<T>, debounceDelay?: number) => void;
|
|
10
5
|
subscribe: (keys: (keyof T)[], listener: StoreListener) => (() => void);
|
|
11
6
|
select: <K extends keyof T>(keys: K[]) => Pick<T, K>;
|
|
12
7
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
type StoreListener = () => void;
|
|
2
|
-
|
|
3
|
-
asyncUpdate?: boolean;
|
|
4
|
-
debounceDelay?: number;
|
|
5
|
-
perKeyDebounce?: Partial<Record<keyof T, boolean>>;
|
|
6
|
-
};
|
|
7
|
-
declare function createStoreState<T extends object>(initialState: T, options?: StoreOptions<T>): {
|
|
2
|
+
declare function createStoreState<T extends object>(initialState: T): {
|
|
8
3
|
get: () => T;
|
|
9
|
-
set: (next: Partial<T
|
|
4
|
+
set: (next: Partial<T>, debounceDelay?: number) => void;
|
|
10
5
|
subscribe: (keys: (keyof T)[], listener: StoreListener) => (() => void);
|
|
11
6
|
select: <K extends keyof T>(keys: K[]) => Pick<T, K>;
|
|
12
7
|
};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var K=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var I=(o,e)=>{for(var n in e)K(o,n,{get:e[n],enumerable:!0})},R=(o,e,n,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let p of v(e))!j.call(o,p)&&p!==n&&K(o,p,{get:()=>e[p],enumerable:!(c=P(e,p))||c.enumerable});return o};var C=o=>R(K({},"__esModule",{value:!0}),o);var w={};I(w,{createStoreState:()=>F,useStoreSelector:()=>E});module.exports=C(w);var f=require("react");function V(o,e){let n;return(...c)=>{clearTimeout(n),n=setTimeout(()=>o(...c),e)}}function F(o){let e=o,n=new Map,c=new Map;return{get:()=>e,set:(i,a)=>{var S;if(!i)return;let r=[];for(let k in i){let t=k,l=e[t],T=i[t];l!==T&&(Object.is(l,T)||(e[t]=T,r.push(t)))}if(r.length!==0)for(let k of r)typeof a=="number"?(c.has(k)||c.set(k,V(()=>{var t;(t=n.get(k))==null||t.forEach(l=>l())},a)),c.get(k)()):(S=n.get(k))==null||S.forEach(t=>t())},subscribe:(i,a)=>{for(let r of i)n.has(r)||n.set(r,new Set),n.get(r).add(a);return()=>{var r;for(let S of i)(r=n.get(S))==null||r.delete(a)}},select:i=>{let a={},r=e;for(let S of i)a[S]=r[S];return a}}}function z(o,e){return o.length===e.length&&o.every((n,c)=>n===e[c])}function E(o,e){let n=(0,f.useRef)({}),c=(0,f.useRef)(null),p=(0,f.useRef)(null),x=(0,f.useRef)(null),h=(0,f.useRef)(!0),b=(0,f.useRef)({}),i=(0,f.useRef)(null);if(!c.current||!z(c.current,e)){let t=[],l=[];for(let T of e)if(typeof T=="string"){let d=T;t.push({key:d}),l.push(d)}else{let d=T;for(let s in d){let y=d[s],u=s;t.push({key:u,compare:y}),l.push(u)}}p.current=t,x.current=l,c.current=e,i.current=null}let a=p.current,r=x.current,S=()=>{let t=o.get();if(h.current){h.current=!1;let s={};for(let{key:y}of a){let u=t[y];b.current[y]=u,s[y]=u}return n.current=s,s}if(!(()=>{var s;for(let{key:y,compare:u}of a){let m=b.current[y],g=t[y];if(m===void 0||((s=u==null?void 0:u(m,g))!=null?s:!Object.is(m,g)))return!0}return!1})())return n.current;let d={};for(let{key:s,compare:y}of a){let u=b.current[s],m=t[s];u===void 0||(y?!y(u,m):!Object.is(u,m))?(b.current[s]=m,d[s]=m):d[s]=u}return n.current=d,d},k=(0,f.useMemo)(()=>{let t=o.get(),l={};for(let T of r)l[T]=t[T];return l},[r]);return i.current||(i.current=t=>o.subscribe(r,t)),(0,f.useSyncExternalStore)(i.current,S,()=>k)}0&&(module.exports={createStoreState,useStoreSelector});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useMemo as K,useRef as
|
|
1
|
+
import{useMemo as K,useRef as k,useSyncExternalStore as P}from"react";function v(T,s){let n;return(...a)=>{clearTimeout(n),n=setTimeout(()=>T(...a),s)}}function C(T){let s=T,n=new Map,a=new Map;return{get:()=>s,set:(c,u)=>{var d;if(!c)return;let t=[];for(let p in c){let e=p,i=s[e],l=c[e];i!==l&&(Object.is(i,l)||(s[e]=l,t.push(e)))}if(t.length!==0)for(let p of t)typeof u=="number"?(a.has(p)||a.set(p,v(()=>{var e;(e=n.get(p))==null||e.forEach(i=>i())},u)),a.get(p)()):(d=n.get(p))==null||d.forEach(e=>e())},subscribe:(c,u)=>{for(let t of c)n.has(t)||n.set(t,new Set),n.get(t).add(u);return()=>{var t;for(let d of c)(t=n.get(d))==null||t.delete(u)}},select:c=>{let u={},t=s;for(let d of c)u[d]=t[d];return u}}}function j(T,s){return T.length===s.length&&T.every((n,a)=>n===s[a])}function V(T,s){let n=k({}),a=k(null),b=k(null),x=k(null),h=k(!0),m=k({}),c=k(null);if(!a.current||!j(a.current,s)){let e=[],i=[];for(let l of s)if(typeof l=="string"){let y=l;e.push({key:y}),i.push(y)}else{let y=l;for(let o in y){let f=y[o],r=o;e.push({key:r,compare:f}),i.push(r)}}b.current=e,x.current=i,a.current=s,c.current=null}let u=b.current,t=x.current,d=()=>{let e=T.get();if(h.current){h.current=!1;let o={};for(let{key:f}of u){let r=e[f];m.current[f]=r,o[f]=r}return n.current=o,o}if(!(()=>{var o;for(let{key:f,compare:r}of u){let S=m.current[f],g=e[f];if(S===void 0||((o=r==null?void 0:r(S,g))!=null?o:!Object.is(S,g)))return!0}return!1})())return n.current;let y={};for(let{key:o,compare:f}of u){let r=m.current[o],S=e[o];r===void 0||(f?!f(r,S):!Object.is(r,S))?(m.current[o]=S,y[o]=S):y[o]=r}return n.current=y,y},p=K(()=>{let e=T.get(),i={};for(let l of t)i[l]=e[l];return i},[t]);return c.current||(c.current=e=>T.subscribe(t,e)),P(c.current,d,()=>p)}export{C as createStoreState,V as useStoreSelector};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,239 +1,221 @@
|
|
|
1
|
-
import { useMemo, useRef, useSyncExternalStore } from 'react'
|
|
2
|
-
|
|
3
|
-
type StoreListener = () => void;
|
|
4
|
-
|
|
5
|
-
function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): T {
|
|
6
|
-
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
7
|
-
return ((...args: Parameters<T>) => {
|
|
8
|
-
clearTimeout(timeoutId);
|
|
9
|
-
timeoutId = setTimeout(() => fn(...args), delay);
|
|
10
|
-
}) as T;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
type
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
:
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return result as Picked<T, S>;
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
const staticSnapshot = useMemo(() => {
|
|
225
|
-
const current = store.get();
|
|
226
|
-
const result = {} as Partial<T>;
|
|
227
|
-
for (const key of keys) {
|
|
228
|
-
result[key] = current[key];
|
|
229
|
-
}
|
|
230
|
-
return result as Picked<T, S>;
|
|
231
|
-
}, [keys]);
|
|
232
|
-
|
|
233
|
-
if (!subscribeRef.current) {
|
|
234
|
-
subscribeRef.current = (onStoreChange: () => void) =>
|
|
235
|
-
store.subscribe(keys, onStoreChange);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return useSyncExternalStore(subscribeRef.current, getSnapshot, () => staticSnapshot);
|
|
1
|
+
import { useMemo, useRef, useSyncExternalStore } from 'react'
|
|
2
|
+
|
|
3
|
+
type StoreListener = () => void;
|
|
4
|
+
|
|
5
|
+
function debounce<T extends (...args: any[]) => void>(fn: T, delay: number): T {
|
|
6
|
+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
7
|
+
return ((...args: Parameters<T>) => {
|
|
8
|
+
clearTimeout(timeoutId);
|
|
9
|
+
timeoutId = setTimeout(() => fn(...args), delay);
|
|
10
|
+
}) as T;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createStoreState<T extends object>(
|
|
14
|
+
initialState: T,
|
|
15
|
+
) {
|
|
16
|
+
let state = initialState;
|
|
17
|
+
const keyListeners = new Map<keyof T, Set<StoreListener>>();
|
|
18
|
+
const debouncedNotifiers = new Map<keyof T, () => void>();
|
|
19
|
+
|
|
20
|
+
const get = () => state;
|
|
21
|
+
|
|
22
|
+
const set = (next: Partial<T>, debounceDelay?: number) => {
|
|
23
|
+
if (!next) return;
|
|
24
|
+
|
|
25
|
+
const updatedKeys: (keyof T)[] = [];
|
|
26
|
+
for (const key in next) {
|
|
27
|
+
const typedKey = key as keyof T;
|
|
28
|
+
const currentValue = state[typedKey];
|
|
29
|
+
const nextValue = next[typedKey];
|
|
30
|
+
|
|
31
|
+
if (currentValue === nextValue) continue;
|
|
32
|
+
|
|
33
|
+
if (!Object.is(currentValue, nextValue)) {
|
|
34
|
+
state[typedKey] = nextValue!;
|
|
35
|
+
updatedKeys.push(typedKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (updatedKeys.length === 0) return;
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
for (const key of updatedKeys) {
|
|
43
|
+
|
|
44
|
+
if (typeof debounceDelay === 'number') {
|
|
45
|
+
if (!debouncedNotifiers.has(key)) {
|
|
46
|
+
debouncedNotifiers.set(key, debounce(() => {
|
|
47
|
+
keyListeners.get(key)?.forEach(listener => listener());
|
|
48
|
+
}, debounceDelay));
|
|
49
|
+
}
|
|
50
|
+
debouncedNotifiers.get(key)!();
|
|
51
|
+
} else {
|
|
52
|
+
keyListeners.get(key)?.forEach(listener => listener());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const subscribe = (keys: (keyof T)[], listener: StoreListener): (() => void) => {
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
if (!keyListeners.has(key)) {
|
|
60
|
+
keyListeners.set(key, new Set());
|
|
61
|
+
}
|
|
62
|
+
keyListeners.get(key)!.add(listener);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return () => {
|
|
66
|
+
for (const key of keys) {
|
|
67
|
+
keyListeners.get(key)?.delete(listener);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const select = <K extends keyof T>(keys: K[]): Pick<T, K> => {
|
|
73
|
+
const result = {} as Pick<T, K>;
|
|
74
|
+
const currentState = state;
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
result[key] = currentState[key];
|
|
77
|
+
}
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return { get, set, subscribe, select };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type StoreType<T extends object> = ReturnType<typeof createStoreState<T>>;
|
|
85
|
+
type PrimitiveKey<T extends object> = keyof T;
|
|
86
|
+
type CompareFn<V> = (prev: V, next: V) => boolean;
|
|
87
|
+
|
|
88
|
+
type KeySelector<T extends object> = PrimitiveKey<T>;
|
|
89
|
+
type CustomSelector<T extends object> = { [K in keyof T]?: CompareFn<T[K]> };
|
|
90
|
+
type SelectorInput<T extends object> = ReadonlyArray<KeySelector<T> | CustomSelector<T>>;
|
|
91
|
+
|
|
92
|
+
type ExtractSelectorKeys<T extends object, S extends SelectorInput<T>> = {
|
|
93
|
+
[K in S[number] extends infer Item
|
|
94
|
+
? Item extends keyof T
|
|
95
|
+
? Item
|
|
96
|
+
: keyof Item
|
|
97
|
+
: never]: T[K];
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
type Picked<T extends object, S extends SelectorInput<T>> = ExtractSelectorKeys<T, S>;
|
|
101
|
+
|
|
102
|
+
type NormalizedSelector<T extends object> = {
|
|
103
|
+
key: keyof T;
|
|
104
|
+
compare?: CompareFn<T[keyof T]>;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function shallowEqualSelector<T extends object>(
|
|
108
|
+
a: SelectorInput<T>,
|
|
109
|
+
b: SelectorInput<T>
|
|
110
|
+
): boolean {
|
|
111
|
+
return a.length === b.length && a.every((item, i) => item === b[i]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function useStoreSelector<T extends object, S extends SelectorInput<T>>(
|
|
115
|
+
store: StoreType<T>,
|
|
116
|
+
selector: S
|
|
117
|
+
): Picked<T, S> {
|
|
118
|
+
const lastSelected = useRef<Partial<T>>({});
|
|
119
|
+
const prevSelector = useRef<SelectorInput<T> | null>(null);
|
|
120
|
+
const normalizedRef = useRef<NormalizedSelector<T>[] | null>(null);
|
|
121
|
+
const keysRef = useRef<(keyof T)[] | null>(null);
|
|
122
|
+
const isFirstRunRef = useRef(true);
|
|
123
|
+
const lastValues = useRef<Partial<T>>({});
|
|
124
|
+
const subscribeRef = useRef<((onStoreChange: () => void) => () => void) | null>(null);
|
|
125
|
+
|
|
126
|
+
if (!prevSelector.current || !shallowEqualSelector(prevSelector.current, selector)) {
|
|
127
|
+
const normalized: NormalizedSelector<T>[] = [];
|
|
128
|
+
const keys: (keyof T)[] = [];
|
|
129
|
+
|
|
130
|
+
for (const item of selector) {
|
|
131
|
+
if (typeof item === 'string') {
|
|
132
|
+
const key = item as keyof T;
|
|
133
|
+
normalized.push({ key });
|
|
134
|
+
keys.push(key);
|
|
135
|
+
} else {
|
|
136
|
+
const customSelector = item as CustomSelector<T>;
|
|
137
|
+
for (const key in customSelector) {
|
|
138
|
+
const compare = customSelector[key as keyof typeof customSelector];
|
|
139
|
+
const typedKey = key as keyof T;
|
|
140
|
+
normalized.push({ key: typedKey, compare: compare as CompareFn<T[keyof T]> });
|
|
141
|
+
keys.push(typedKey);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
normalizedRef.current = normalized;
|
|
147
|
+
keysRef.current = keys;
|
|
148
|
+
prevSelector.current = selector;
|
|
149
|
+
subscribeRef.current = null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const normalized = normalizedRef.current!;
|
|
153
|
+
const keys = keysRef.current!;
|
|
154
|
+
|
|
155
|
+
const getSnapshot = () => {
|
|
156
|
+
const current = store.get();
|
|
157
|
+
const isFirstRun = isFirstRunRef.current;
|
|
158
|
+
|
|
159
|
+
if (isFirstRun) {
|
|
160
|
+
isFirstRunRef.current = false;
|
|
161
|
+
const result = {} as Partial<T>;
|
|
162
|
+
for (const { key } of normalized) {
|
|
163
|
+
const value = current[key];
|
|
164
|
+
lastValues.current[key] = value;
|
|
165
|
+
result[key] = value;
|
|
166
|
+
}
|
|
167
|
+
lastSelected.current = result;
|
|
168
|
+
return result as Picked<T, S>;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const hasChanges = () => {
|
|
172
|
+
for (const { key, compare } of normalized) {
|
|
173
|
+
const prevVal = lastValues.current[key];
|
|
174
|
+
const nextVal = current[key];
|
|
175
|
+
if (prevVal === undefined ? true : (compare?.(prevVal, nextVal) ?? !Object.is(prevVal, nextVal))) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
if (!hasChanges()) {
|
|
183
|
+
return lastSelected.current as Picked<T, S>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const result = {} as Partial<T>;
|
|
187
|
+
for (const { key, compare } of normalized) {
|
|
188
|
+
const prevVal = lastValues.current[key];
|
|
189
|
+
const nextVal = current[key];
|
|
190
|
+
|
|
191
|
+
const isFirstTime = prevVal === undefined;
|
|
192
|
+
const changed = isFirstTime || (compare ? !compare(prevVal, nextVal) : !Object.is(prevVal, nextVal));
|
|
193
|
+
|
|
194
|
+
if (changed) {
|
|
195
|
+
lastValues.current[key] = nextVal;
|
|
196
|
+
result[key] = nextVal;
|
|
197
|
+
} else {
|
|
198
|
+
result[key] = prevVal;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
lastSelected.current = result;
|
|
203
|
+
return result as Picked<T, S>;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const staticSnapshot = useMemo(() => {
|
|
207
|
+
const current = store.get();
|
|
208
|
+
const result = {} as Partial<T>;
|
|
209
|
+
for (const key of keys) {
|
|
210
|
+
result[key] = current[key];
|
|
211
|
+
}
|
|
212
|
+
return result as Picked<T, S>;
|
|
213
|
+
}, [keys]);
|
|
214
|
+
|
|
215
|
+
if (!subscribeRef.current) {
|
|
216
|
+
subscribeRef.current = (onStoreChange: () => void) =>
|
|
217
|
+
store.subscribe(keys, onStoreChange);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return useSyncExternalStore(subscribeRef.current, getSnapshot, () => staticSnapshot);
|
|
239
221
|
}
|