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