dev-react-microstore 1.1.1 → 1.1.3
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.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
- package/src/index.ts +75 -49
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var m=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var g=(n,e)=>{for(var t in e)m(n,t,{get:e[t],enumerable:!0})},v=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of j(e))!P.call(n,c)&&c!==t&&m(n,c,{get:()=>e[c],enumerable:!(i=h(e,c))||i.enumerable});return n};var I=n=>v(m({},"__esModule",{value:!0}),n);var F={};g(F,{createStoreState:()=>R,useStoreSelector:()=>C});module.exports=I(F);var y=require("react");function R(n){let e=n,t=new Map;return{get:()=>e,set:o=>{var s;let r=[];for(let u in o){let d=u;Object.is(e[d],o[d])||r.push(d)}if(r.length!==0){for(let u in o)e[u]=o[u];for(let u of r)(s=t.get(u))==null||s.forEach(d=>d())}},subscribe:(o,r)=>{for(let s of o)t.has(s)||t.set(s,new Set),t.get(s).add(r);return()=>{var s;for(let u of o)(s=t.get(u))==null||s.delete(r)}},select:o=>{let r={},s=e;for(let u of o)r[u]=s[u];return r}}}function V(n,e){return n.length===e.length&&n.every((t,i)=>t===e[i])}function z(n,e,t){let i={};for(let{key:c,compare:p}of t){let f=e[c],o=n[c],r=f===void 0?!0:p?p(f,o):!Object.is(f,o);i[c]=r?o:f}return i}function C(n,e){let t=(0,y.useRef)({}),i=(0,y.useRef)(null),c=(0,y.useRef)(null),p=(0,y.useRef)(null),f=(0,y.useRef)(!0),o=(0,y.useRef)({});if(!i.current||!V(i.current,e)){let a=[],T=[];for(let S of e)if(typeof S=="string"){let l=S;a.push({key:l}),T.push(l)}else for(let[l,b]of Object.entries(S)){let k=l;a.push({key:k,compare:b}),T.push(k)}c.current=a,p.current=T,i.current=e}let r=c.current,s=p.current,u=()=>{let a=n.get(),T=f.current;if(!(T||r.some(({key:l,compare:b})=>{var K;let k=o.current[l],x=a[l];return k===void 0?!0:(K=b==null?void 0:b(k,x))!=null?K:!Object.is(k,x)})))return t.current;T&&(f.current=!1);for(let{key:l}of r)o.current[l]=a[l];return t.current=z(a,t.current,r),t.current},d=(()=>{let a=n.get();return s.reduce((T,S)=>(T[S]=a[S],T),{})})();return(0,y.useSyncExternalStore)(a=>n.subscribe(s,a),u,()=>d)}0&&(module.exports={createStoreState,useStoreSelector});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import{useRef as k,useSyncExternalStore as K}from"react";function v(u){let r=u,n=new Map;return{get:()=>r,set:e=>{var o;let t=[];for(let s in e){let f=s;Object.is(r[f],e[f])||t.push(f)}if(t.length!==0){for(let s in e)r[s]=e[s];for(let s of t)(o=n.get(s))==null||o.forEach(f=>f())}},subscribe:(e,t)=>{for(let o of e)n.has(o)||n.set(o,new Set),n.get(o).add(t);return()=>{var o;for(let s of e)(o=n.get(s))==null||o.delete(t)}},select:e=>{let t={},o=r;for(let s of e)t[s]=o[s];return t}}}function h(u,r){return u.length===r.length&&u.every((n,a)=>n===r[a])}function j(u,r,n){let a={};for(let{key:y,compare:S}of n){let T=r[y],e=u[y],t=T===void 0?!0:S?S(T,e):!Object.is(T,e);a[y]=t?e:T}return a}function I(u,r){let n=k({}),a=k(null),y=k(null),S=k(null),T=k(!0),e=k({});if(!a.current||!h(a.current,r)){let c=[],l=[];for(let d of r)if(typeof d=="string"){let i=d;c.push({key:i}),l.push(i)}else for(let[i,b]of Object.entries(d)){let p=i;c.push({key:p,compare:b}),l.push(p)}y.current=c,S.current=l,a.current=r}let t=y.current,o=S.current,s=()=>{let c=u.get(),l=T.current;if(!(l||t.some(({key:i,compare:b})=>{var x;let p=e.current[i],m=c[i];return p===void 0?!0:(x=b==null?void 0:b(p,m))!=null?x:!Object.is(p,m)})))return n.current;l&&(T.current=!1);for(let{key:i}of t)e.current[i]=c[i];return n.current=j(c,n.current,t),n.current},f=(()=>{let c=u.get();return o.reduce((l,d)=>(l[d]=c[d],l),{})})();return K(c=>u.subscribe(o,c),s,()=>f)}export{v as createStoreState,I as useStoreSelector};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,34 +5,26 @@ type StoreListener = () => void;
|
|
|
5
5
|
|
|
6
6
|
export function createStoreState<T extends object>(initialState: T) {
|
|
7
7
|
let state = initialState;
|
|
8
|
-
|
|
9
|
-
// Map from key to set of subscribers interested in that key
|
|
8
|
+
|
|
10
9
|
const keyListeners = new Map<keyof T, Set<StoreListener>>();
|
|
11
10
|
|
|
12
11
|
const get = () => state;
|
|
13
12
|
|
|
14
13
|
const set = (next: Partial<T>) => {
|
|
15
|
-
if (next === null || next === undefined || typeof next !== 'object' || Array.isArray(next)) {
|
|
16
|
-
console.error(`set() called with invalid value: ${next === null ? 'null' : next === undefined ? 'undefined' : typeof next}`);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let changed = false;
|
|
21
14
|
const updatedKeys: (keyof T)[] = [];
|
|
22
15
|
|
|
23
16
|
for (const key in next) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
changed = true;
|
|
28
|
-
updatedKeys.push(typedKey);
|
|
29
|
-
}
|
|
17
|
+
const typedKey = key as keyof T;
|
|
18
|
+
if (!Object.is(state[typedKey], next[typedKey])) {
|
|
19
|
+
updatedKeys.push(typedKey);
|
|
30
20
|
}
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
if (
|
|
23
|
+
if (updatedKeys.length === 0) return;
|
|
34
24
|
|
|
35
|
-
|
|
25
|
+
for (const key in next) {
|
|
26
|
+
state[key as keyof T] = next[key as keyof T]!;
|
|
27
|
+
}
|
|
36
28
|
|
|
37
29
|
for (const key of updatedKeys) {
|
|
38
30
|
keyListeners.get(key)?.forEach(listener => listener());
|
|
@@ -89,67 +81,101 @@ type NormalizedSelector<T extends object> = {
|
|
|
89
81
|
compare?: CompareFn<T[keyof T]>;
|
|
90
82
|
};
|
|
91
83
|
|
|
84
|
+
function shallowEqualSelector<T extends object>(
|
|
85
|
+
a: SelectorInput<T>,
|
|
86
|
+
b: SelectorInput<T>
|
|
87
|
+
): boolean {
|
|
88
|
+
return a.length === b.length && a.every((item, i) => item === b[i]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function pickKeys<T extends object>(
|
|
92
|
+
base: T,
|
|
93
|
+
prev: Partial<T>,
|
|
94
|
+
selectors: NormalizedSelector<T>[]
|
|
95
|
+
): Partial<T> {
|
|
96
|
+
const out: Partial<T> = {};
|
|
97
|
+
for (const { key, compare } of selectors) {
|
|
98
|
+
const prevVal = prev[key];
|
|
99
|
+
const nextVal = base[key];
|
|
100
|
+
const changed = prevVal === undefined ? true : (compare ? compare(prevVal, nextVal) : !Object.is(prevVal, nextVal));
|
|
101
|
+
out[key] = changed ? nextVal : prevVal;
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
92
106
|
export function useStoreSelector<T extends object, S extends SelectorInput<T>>(
|
|
93
107
|
store: StoreType<T>,
|
|
94
108
|
selector: S
|
|
95
109
|
): Picked<T, S> {
|
|
96
|
-
const lastState = useRef(store.get());
|
|
97
110
|
const lastSelected = useRef<Partial<T>>({});
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
111
|
+
const prevSelector = useRef<SelectorInput<T> | null>(null);
|
|
112
|
+
const normalizedRef = useRef<NormalizedSelector<T>[] | null>(null);
|
|
113
|
+
const keysRef = useRef<(keyof T)[] | null>(null);
|
|
114
|
+
const isFirstRunRef = useRef(true);
|
|
115
|
+
const lastValues = useRef<Partial<T>>({});
|
|
116
|
+
|
|
117
|
+
if (!prevSelector.current || !shallowEqualSelector(prevSelector.current, selector)) {
|
|
118
|
+
const normalized: NormalizedSelector<T>[] = [];
|
|
119
|
+
const keys: (keyof T)[] = [];
|
|
120
|
+
|
|
121
|
+
for (const item of selector) {
|
|
122
|
+
if (typeof item === 'string') {
|
|
123
|
+
const key = item as keyof T;
|
|
124
|
+
normalized.push({ key });
|
|
125
|
+
keys.push(key);
|
|
126
|
+
} else {
|
|
127
|
+
for (const [key, compare] of Object.entries(item)) {
|
|
128
|
+
const typedKey = key as keyof T;
|
|
129
|
+
normalized.push({ key: typedKey, compare: compare as CompareFn<T[keyof T]> });
|
|
130
|
+
keys.push(typedKey);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
102
133
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
134
|
+
|
|
135
|
+
normalizedRef.current = normalized;
|
|
136
|
+
keysRef.current = keys;
|
|
137
|
+
prevSelector.current = selector;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const normalized = normalizedRef.current!;
|
|
141
|
+
const keys = keysRef.current!;
|
|
108
142
|
|
|
109
143
|
const getSnapshot = () => {
|
|
110
144
|
const current = store.get();
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
const isFirstRun = !lastSelected.current || Object.keys(lastSelected.current).length === 0;
|
|
145
|
+
const isFirstRun = isFirstRunRef.current;
|
|
114
146
|
|
|
115
147
|
const changed = isFirstRun || normalized.some(({ key, compare }) => {
|
|
116
|
-
const prevVal =
|
|
148
|
+
const prevVal = lastValues.current[key];
|
|
117
149
|
const nextVal = current[key];
|
|
118
|
-
return
|
|
150
|
+
return prevVal === undefined ? true : (compare?.(prevVal, nextVal) ?? !Object.is(prevVal, nextVal));
|
|
119
151
|
});
|
|
120
152
|
|
|
121
153
|
if (!changed) {
|
|
122
154
|
return lastSelected.current as Picked<T, S>;
|
|
123
155
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const nextSelected: Partial<T> = {};
|
|
128
|
-
|
|
129
|
-
for (const { key, compare } of normalized) {
|
|
130
|
-
const prevVal = lastSelected.current[key];
|
|
131
|
-
const nextVal = current[key];
|
|
132
|
-
const hasChanged = compare
|
|
133
|
-
? compare(prevVal as T[keyof T], nextVal as T[keyof T])
|
|
134
|
-
: !Object.is(prevVal, nextVal);
|
|
135
|
-
|
|
136
|
-
nextSelected[key] = hasChanged ? nextVal : prevVal;
|
|
156
|
+
|
|
157
|
+
if (isFirstRun) {
|
|
158
|
+
isFirstRunRef.current = false;
|
|
137
159
|
}
|
|
138
160
|
|
|
139
|
-
|
|
161
|
+
for (const { key } of normalized) {
|
|
162
|
+
lastValues.current[key] = current[key];
|
|
163
|
+
}
|
|
140
164
|
|
|
141
|
-
|
|
165
|
+
lastSelected.current = pickKeys(current, lastSelected.current, normalized);
|
|
166
|
+
return lastSelected.current as Picked<T, S>;
|
|
142
167
|
};
|
|
143
168
|
|
|
144
169
|
const staticSnapshot = (() => {
|
|
145
170
|
const current = store.get();
|
|
146
|
-
return
|
|
171
|
+
return keys.reduce((acc, key) => {
|
|
147
172
|
acc[key] = current[key];
|
|
148
173
|
return acc;
|
|
149
174
|
}, {} as Partial<T>) as Picked<T, S>;
|
|
150
175
|
})();
|
|
151
|
-
|
|
176
|
+
|
|
152
177
|
const subscribe = (onStoreChange: () => void) =>
|
|
153
|
-
store.subscribe(
|
|
178
|
+
store.subscribe(keys, onStoreChange);
|
|
179
|
+
|
|
154
180
|
return useSyncExternalStore(subscribe, getSnapshot, () => staticSnapshot);
|
|
155
181
|
}
|