@tamagui/use-store 2.7.6 → 3.0.0-beta.637.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/cjs/comparators.cjs +27 -30
- package/dist/cjs/comparators.native.cjs +45 -0
- package/dist/cjs/comparators.native.js +31 -34
- package/dist/cjs/comparators.native.js.map +1 -1
- package/dist/cjs/configureUseStore.cjs +17 -18
- package/dist/cjs/configureUseStore.native.cjs +33 -0
- package/dist/cjs/configureUseStore.native.js +17 -17
- package/dist/cjs/configureUseStore.native.js.map +1 -1
- package/dist/cjs/constants.cjs +19 -20
- package/dist/cjs/constants.native.cjs +34 -0
- package/dist/cjs/constants.native.js +19 -19
- package/dist/cjs/constants.native.js.map +1 -1
- package/dist/cjs/decorators.cjs +19 -22
- package/dist/cjs/decorators.native.cjs +32 -0
- package/dist/cjs/decorators.native.js +19 -21
- package/dist/cjs/decorators.native.js.map +1 -1
- package/dist/cjs/helpers.cjs +39 -40
- package/dist/cjs/helpers.native.cjs +68 -0
- package/dist/cjs/helpers.native.js +42 -43
- package/dist/cjs/helpers.native.js.map +1 -1
- package/dist/cjs/index.cjs +22 -23
- package/dist/cjs/index.native.cjs +62 -0
- package/dist/cjs/index.native.js +35 -36
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/interfaces.cjs +10 -11
- package/dist/cjs/interfaces.native.cjs +19 -0
- package/dist/cjs/interfaces.native.js +10 -10
- package/dist/cjs/interfaces.native.js.map +1 -1
- package/dist/cjs/observe.cjs +132 -138
- package/dist/cjs/observe.native.cjs +221 -0
- package/dist/cjs/observe.native.js +192 -203
- package/dist/cjs/observe.native.js.map +1 -1
- package/dist/cjs/useStore.cjs +366 -403
- package/dist/cjs/useStore.native.cjs +557 -0
- package/dist/cjs/useStore.native.js +487 -536
- package/dist/cjs/useStore.native.js.map +1 -1
- package/dist/cjs/useStoreDebug.cjs +39 -45
- package/dist/cjs/useStoreDebug.native.cjs +64 -0
- package/dist/cjs/useStoreDebug.native.js +43 -48
- package/dist/cjs/useStoreDebug.native.js.map +1 -1
- package/dist/esm/comparators.mjs +13 -12
- package/dist/esm/comparators.mjs.map +1 -1
- package/dist/esm/comparators.native.js +17 -17
- package/dist/esm/comparators.native.js.map +1 -1
- package/dist/esm/configureUseStore.mjs +2 -1
- package/dist/esm/configureUseStore.mjs.map +1 -1
- package/dist/esm/configureUseStore.native.js +2 -1
- package/dist/esm/configureUseStore.native.js.map +1 -1
- package/dist/esm/constants.mjs +4 -3
- package/dist/esm/constants.mjs.map +1 -1
- package/dist/esm/constants.native.js +4 -3
- package/dist/esm/constants.native.js.map +1 -1
- package/dist/esm/decorators.mjs +5 -4
- package/dist/esm/decorators.mjs.map +1 -1
- package/dist/esm/decorators.native.js +5 -4
- package/dist/esm/decorators.native.js.map +1 -1
- package/dist/esm/helpers.mjs +20 -18
- package/dist/esm/helpers.mjs.map +1 -1
- package/dist/esm/helpers.native.js +23 -22
- package/dist/esm/helpers.native.js.map +1 -1
- package/dist/esm/index.js +18 -11
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +18 -11
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +30 -24
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/interfaces.mjs +0 -2
- package/dist/esm/interfaces.native.js +0 -2
- package/dist/esm/observe.mjs +115 -113
- package/dist/esm/observe.mjs.map +1 -1
- package/dist/esm/observe.native.js +175 -179
- package/dist/esm/observe.native.js.map +1 -1
- package/dist/esm/useStore.mjs +336 -363
- package/dist/esm/useStore.mjs.map +1 -1
- package/dist/esm/useStore.native.js +457 -497
- package/dist/esm/useStore.native.js.map +1 -1
- package/dist/esm/useStoreDebug.mjs +20 -18
- package/dist/esm/useStoreDebug.mjs.map +1 -1
- package/dist/esm/useStoreDebug.native.js +23 -21
- package/dist/esm/useStoreDebug.native.js.map +1 -1
- package/package.json +3 -3
- package/dist/esm/interfaces.mjs.map +0 -1
- package/dist/esm/interfaces.native.js.map +0 -1
package/dist/esm/useStore.mjs
CHANGED
|
@@ -4,412 +4,385 @@ import { configureOpts } from "./configureUseStore.mjs";
|
|
|
4
4
|
import { UNWRAP_PROXY, defaultOptions } from "./constants.mjs";
|
|
5
5
|
import { UNWRAP_STORE_INFO, cache, getStoreDescriptors, getStoreUid, simpleStr } from "./helpers.mjs";
|
|
6
6
|
import { DebugStores, shouldDebug, useCurrentComponent } from "./useStoreDebug.mjs";
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
const idFn = (_) => _;
|
|
8
9
|
function useStore(StoreKlass, props, options = defaultOptions) {
|
|
9
|
-
|
|
10
|
+
return useStoreFromInfo(getOrCreateStoreInfo(StoreKlass, props, options), options.selector, options);
|
|
10
11
|
}
|
|
11
12
|
function useStoreDebug(StoreKlass, props) {
|
|
12
|
-
|
|
13
|
-
debug: true
|
|
14
|
-
});
|
|
13
|
+
return useStore(StoreKlass, props, { debug: true });
|
|
15
14
|
}
|
|
16
15
|
function createStore(StoreKlass, props, options) {
|
|
17
|
-
|
|
16
|
+
return getOrCreateStoreInfo(StoreKlass, props, options)?.store;
|
|
18
17
|
}
|
|
19
18
|
function useGlobalStore(instance, debug) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
debug
|
|
26
|
-
});
|
|
19
|
+
const store = instance[UNWRAP_PROXY];
|
|
20
|
+
const uid = getStoreUid(store.constructor, store.props);
|
|
21
|
+
const info = cache.get(uid);
|
|
22
|
+
if (!info) throw new Error(`This store not created using createStore()`);
|
|
23
|
+
return useStoreFromInfo(info, void 0, { debug });
|
|
27
24
|
}
|
|
28
25
|
function useGlobalStoreSelector(instance, selector, debug) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
debug
|
|
35
|
-
});
|
|
26
|
+
const store = instance[UNWRAP_PROXY];
|
|
27
|
+
const uid = getStoreUid(store.constructor, store.props);
|
|
28
|
+
const info = cache.get(uid);
|
|
29
|
+
if (!info) throw new Error(`This store not created using createStore()`);
|
|
30
|
+
return useStoreFromInfo(info, selector, { debug });
|
|
36
31
|
}
|
|
37
32
|
function createUseStore(StoreKlass) {
|
|
38
|
-
|
|
33
|
+
return (props, options) => useStore(StoreKlass, props, options);
|
|
39
34
|
}
|
|
40
35
|
function createUseStoreSelector(StoreKlass, selector) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
});
|
|
45
|
-
};
|
|
36
|
+
return (props) => {
|
|
37
|
+
return useStore(StoreKlass, props, { selector });
|
|
38
|
+
};
|
|
46
39
|
}
|
|
47
40
|
function useStoreSelector(StoreKlass, selector, props) {
|
|
48
|
-
|
|
49
|
-
selector
|
|
50
|
-
});
|
|
41
|
+
return useStore(StoreKlass, props, { selector });
|
|
51
42
|
}
|
|
52
|
-
const storeAccessTrackers = /* @__PURE__ */new Set();
|
|
43
|
+
const storeAccessTrackers = /* @__PURE__ */ new Set();
|
|
53
44
|
function trackStoresAccess(cb) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
storeAccessTrackers.add(cb);
|
|
46
|
+
return () => {
|
|
47
|
+
storeAccessTrackers.delete(cb);
|
|
48
|
+
};
|
|
58
49
|
}
|
|
59
50
|
function getStore(StoreKlass, props) {
|
|
60
|
-
|
|
51
|
+
return getStoreInfo(StoreKlass, props)?.store;
|
|
61
52
|
}
|
|
62
53
|
function getOrCreateStore(StoreKlass, props) {
|
|
63
|
-
|
|
64
|
-
refuseCreation: false
|
|
65
|
-
})?.store;
|
|
54
|
+
return getOrCreateStoreInfo(StoreKlass, props, { refuseCreation: false })?.store;
|
|
66
55
|
}
|
|
67
56
|
function getStoreInfo(StoreKlass, props) {
|
|
68
|
-
|
|
69
|
-
refuseCreation: true
|
|
70
|
-
});
|
|
57
|
+
return getOrCreateStoreInfo(StoreKlass, props, { refuseCreation: true });
|
|
71
58
|
}
|
|
72
|
-
const onCreateListeners = /* @__PURE__ */new Set();
|
|
59
|
+
const onCreateListeners = /* @__PURE__ */ new Set();
|
|
73
60
|
function onCreateStore(cb) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
61
|
+
onCreateListeners.add(cb);
|
|
62
|
+
return () => {
|
|
63
|
+
onCreateListeners.delete(cb);
|
|
64
|
+
};
|
|
78
65
|
}
|
|
79
66
|
function getOrCreateStoreInfo(StoreKlass, props, options, propsKeyCalculated) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
67
|
+
if (!StoreKlass) return null;
|
|
68
|
+
const uid = getStoreUid(StoreKlass, propsKeyCalculated ?? props);
|
|
69
|
+
if (!options?.avoidCache && cache.has(uid)) return cache.get(uid);
|
|
70
|
+
if (options?.refuseCreation) throw new Error(`No store exists (${StoreKlass.name}) with props: ${props}`);
|
|
71
|
+
const storeInstance = new StoreKlass(props);
|
|
72
|
+
storeInstance.props = props;
|
|
73
|
+
const getters = {};
|
|
74
|
+
const actions = {};
|
|
75
|
+
const stateKeys = /* @__PURE__ */ new Set();
|
|
76
|
+
const descriptors = getStoreDescriptors(storeInstance);
|
|
77
|
+
for (const key in descriptors) {
|
|
78
|
+
const descriptor = descriptors[key];
|
|
79
|
+
if (typeof descriptor.value === "function") actions[key] = descriptor.value;
|
|
80
|
+
else if (typeof descriptor.get === "function") getters[key] = descriptor.get;
|
|
81
|
+
else if (key !== "props" && key[0] !== "_") stateKeys.add(key);
|
|
82
|
+
}
|
|
83
|
+
const keyComparators = storeInstance["_comparators"];
|
|
84
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
85
|
+
const storeInfo = {
|
|
86
|
+
uid,
|
|
87
|
+
keyComparators,
|
|
88
|
+
storeInstance,
|
|
89
|
+
getters,
|
|
90
|
+
stateKeys,
|
|
91
|
+
props,
|
|
92
|
+
actions,
|
|
93
|
+
debug: options?.debug,
|
|
94
|
+
disableTracking: false,
|
|
95
|
+
gettersState: {
|
|
96
|
+
getCache: /* @__PURE__ */ new Map(),
|
|
97
|
+
depsToGetter: /* @__PURE__ */ new Map(),
|
|
98
|
+
curGetKeys: /* @__PURE__ */ new Set(),
|
|
99
|
+
isGetting: false
|
|
100
|
+
},
|
|
101
|
+
listeners,
|
|
102
|
+
trackers: /* @__PURE__ */ new Set(),
|
|
103
|
+
version: 0,
|
|
104
|
+
subscribe: (onChanged) => {
|
|
105
|
+
listeners.add(onChanged);
|
|
106
|
+
return () => {
|
|
107
|
+
listeners.delete(onChanged);
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
triggerUpdate: () => {
|
|
111
|
+
storeInfo.version = (storeInfo.version + 1) % Number.MAX_SAFE_INTEGER;
|
|
112
|
+
for (const cb of listeners) cb();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const store = createProxiedStore(storeInfo);
|
|
116
|
+
if (process.env.NODE_ENV === "development") allStores[StoreKlass.name + uid] = store;
|
|
117
|
+
store.mount?.();
|
|
118
|
+
storeInfo.store = store;
|
|
119
|
+
const result = storeInfo;
|
|
120
|
+
cache.set(uid, result);
|
|
121
|
+
onCreateListeners.forEach((cb) => cb(result));
|
|
122
|
+
return result;
|
|
134
123
|
}
|
|
135
124
|
const allStores = {};
|
|
136
125
|
if (process.env.NODE_ENV === "development") globalThis["Store"] ||= allStores;
|
|
137
126
|
const emptyObj = {};
|
|
138
127
|
const selectKeys = (obj, keys) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
128
|
+
if (!keys.length) return emptyObj;
|
|
129
|
+
const res = {};
|
|
130
|
+
for (const key of keys) res[key] = obj[key];
|
|
131
|
+
return res;
|
|
143
132
|
};
|
|
144
133
|
let isInReaction = false;
|
|
145
|
-
const setIsInReaction = val => {
|
|
146
|
-
|
|
134
|
+
const setIsInReaction = (val) => {
|
|
135
|
+
isInReaction = val;
|
|
147
136
|
};
|
|
148
137
|
function useStoreFromInfo(info, userSelector, options) {
|
|
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
|
-
return curVal;
|
|
208
|
-
}
|
|
209
|
-
});
|
|
138
|
+
const store = info?.store;
|
|
139
|
+
const internal = React.useRef(void 0);
|
|
140
|
+
const component = useCurrentComponent();
|
|
141
|
+
if (!internal.current) internal.current = {
|
|
142
|
+
component,
|
|
143
|
+
tracked: /* @__PURE__ */ new Set(),
|
|
144
|
+
last: null,
|
|
145
|
+
lastKeys: null
|
|
146
|
+
};
|
|
147
|
+
const curInternal = internal.current;
|
|
148
|
+
const shouldPrintDebug = options?.debug;
|
|
149
|
+
const getSnapshot = React.useCallback(() => {
|
|
150
|
+
if (!info || !store) return;
|
|
151
|
+
const curInternal2 = internal.current;
|
|
152
|
+
const isTracking = curInternal2.tracked.size;
|
|
153
|
+
const keys = [...!isTracking ? info.stateKeys : curInternal2.tracked];
|
|
154
|
+
const nextKeys = `${info.version}${keys.join("")}${userSelector || ""}`;
|
|
155
|
+
const lastKeys = curInternal2.lastKeys;
|
|
156
|
+
if (nextKeys === curInternal2.lastKeys) return curInternal2.last;
|
|
157
|
+
curInternal2.lastKeys = nextKeys;
|
|
158
|
+
let snap;
|
|
159
|
+
info.disableTracking = true;
|
|
160
|
+
const last = curInternal2.last;
|
|
161
|
+
if (userSelector) snap = userSelector(store);
|
|
162
|
+
else snap = selectKeys(store, keys);
|
|
163
|
+
info.disableTracking = false;
|
|
164
|
+
const isUnchanged = !userSelector && !isTracking && last || typeof last !== "undefined" && isEqualSubsetShallow(last, snap, { keyComparators: info.keyComparators });
|
|
165
|
+
if (shouldPrintDebug) console.info("🌑 getSnapshot", {
|
|
166
|
+
storeState: selectKeys(store, Object.keys(store)),
|
|
167
|
+
userSelector,
|
|
168
|
+
info,
|
|
169
|
+
isUnchanged,
|
|
170
|
+
component,
|
|
171
|
+
keys,
|
|
172
|
+
last,
|
|
173
|
+
snap,
|
|
174
|
+
curInternal: curInternal2,
|
|
175
|
+
nextKeys,
|
|
176
|
+
lastKeys
|
|
177
|
+
});
|
|
178
|
+
if (isUnchanged) return last;
|
|
179
|
+
curInternal2.last = snap;
|
|
180
|
+
return snap;
|
|
181
|
+
}, [store]);
|
|
182
|
+
const state = React.useSyncExternalStore(info?.subscribe || idFn, getSnapshot, getSnapshot);
|
|
183
|
+
if (!info || !store || !state) return state;
|
|
184
|
+
if (userSelector) return state;
|
|
185
|
+
return new Proxy(store, { get(target, key) {
|
|
186
|
+
const curVal = Reflect.get(target, key);
|
|
187
|
+
if (isInReaction) return curVal;
|
|
188
|
+
const keyString = key;
|
|
189
|
+
if (info.stateKeys.has(keyString) || keyString in info.getters) {
|
|
190
|
+
if (shouldPrintDebug) console.info("👀 tracking", keyString);
|
|
191
|
+
curInternal.tracked.add(keyString);
|
|
192
|
+
}
|
|
193
|
+
if (Reflect.has(state, key)) return Reflect.get(state, key);
|
|
194
|
+
return curVal;
|
|
195
|
+
} });
|
|
210
196
|
}
|
|
211
|
-
let setters = /* @__PURE__ */new Set();
|
|
212
|
-
const logStack = /* @__PURE__ */new Set();
|
|
197
|
+
let setters = /* @__PURE__ */ new Set();
|
|
198
|
+
const logStack = /* @__PURE__ */ new Set();
|
|
213
199
|
function createProxiedStore(storeInfo) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
});
|
|
387
|
-
function clearGetterCache(setKey) {
|
|
388
|
-
const parentGetters = depsToGetter.get(setKey);
|
|
389
|
-
getCache.delete(setKey);
|
|
390
|
-
if (!parentGetters) return;
|
|
391
|
-
for (const gk of parentGetters) {
|
|
392
|
-
getCache.delete(gk);
|
|
393
|
-
if (depsToGetter.has(gk)) clearGetterCache(gk);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return proxiedStore;
|
|
200
|
+
const { actions, storeInstance, getters, gettersState } = storeInfo;
|
|
201
|
+
const { getCache, curGetKeys, depsToGetter } = gettersState;
|
|
202
|
+
const constr = storeInstance.constructor;
|
|
203
|
+
const shouldDebug2 = storeInfo.debug ?? DebugStores.has(constr);
|
|
204
|
+
let didSet = false;
|
|
205
|
+
const wrappedActions = {};
|
|
206
|
+
for (const key in actions) {
|
|
207
|
+
if (key === "subscribe") continue;
|
|
208
|
+
const actionFn = actions[key];
|
|
209
|
+
const isGetFn = key.startsWith("get");
|
|
210
|
+
wrappedActions[key] = function useStoreAction(...args) {
|
|
211
|
+
let res;
|
|
212
|
+
if (isGetFn || gettersState.isGetting) return Reflect.apply(actionFn, proxiedStore, args);
|
|
213
|
+
if (process.env.NODE_ENV === "development" && shouldDebug2) console.info("(debug) startAction", key);
|
|
214
|
+
res = Reflect.apply(actionFn, proxiedStore, args);
|
|
215
|
+
if (res instanceof Promise) return res.then(finishAction);
|
|
216
|
+
finishAction();
|
|
217
|
+
return res;
|
|
218
|
+
};
|
|
219
|
+
if (process.env.NODE_ENV === "development") {
|
|
220
|
+
let hashCode = function(str) {
|
|
221
|
+
let hash = 0;
|
|
222
|
+
for (let i = 0; i < str.length; i++) hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
223
|
+
return hash;
|
|
224
|
+
}, strColor = function(str) {
|
|
225
|
+
return `hsl(${hashCode(str) % 360}, 90%, 40%)`;
|
|
226
|
+
};
|
|
227
|
+
if (!key.startsWith("get") && !key.startsWith("_") && key !== "subscribe") {
|
|
228
|
+
const ogAction = wrappedActions[key];
|
|
229
|
+
wrappedActions[key] = new Proxy(ogAction, { apply(target, thisArg, args) {
|
|
230
|
+
const isDebugging = shouldDebug2 || storeInfo.debug;
|
|
231
|
+
if (!(process.env.LOG_LEVEL !== "0" && (isDebugging || configureOpts.logLevel !== "error"))) return Reflect.apply(target, thisArg, args);
|
|
232
|
+
setters = /* @__PURE__ */ new Set();
|
|
233
|
+
const curSetters = setters;
|
|
234
|
+
const isTopLevelLogger = logStack.size == 0;
|
|
235
|
+
const logs = /* @__PURE__ */ new Set();
|
|
236
|
+
logStack.add(logs);
|
|
237
|
+
let res;
|
|
238
|
+
const id = counter++;
|
|
239
|
+
try {
|
|
240
|
+
res = Reflect.apply(target, thisArg, args);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.error("Error", err);
|
|
243
|
+
throw err;
|
|
244
|
+
} finally {
|
|
245
|
+
logStack.add("end");
|
|
246
|
+
const name = constr.name;
|
|
247
|
+
const color = strColor(name);
|
|
248
|
+
const simpleArgs = args.map(simpleStr);
|
|
249
|
+
logs.add([
|
|
250
|
+
`%c \u{1F311} ${id} ${name.padStart(isTopLevelLogger ? 8 : 4)}%c.${key}(${simpleArgs.join(", ")})${isTopLevelLogger && logStack.size > 1 ? ` (+${logStack.size - 1})` : ""}`,
|
|
251
|
+
`color: ${color};`,
|
|
252
|
+
"color: black;"
|
|
253
|
+
]);
|
|
254
|
+
if (curSetters.size) curSetters.forEach(({ key: key2, value }) => {
|
|
255
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") logs.add([` SET ${key2} ${value}`, value]);
|
|
256
|
+
else logs.add([` SET ${key2}`, value]);
|
|
257
|
+
});
|
|
258
|
+
if (isTopLevelLogger) {
|
|
259
|
+
let error = null;
|
|
260
|
+
try {
|
|
261
|
+
for (const item of logStack) {
|
|
262
|
+
if (item === "end") {
|
|
263
|
+
console.groupEnd();
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const [head, ...rest] = item;
|
|
267
|
+
if (head) {
|
|
268
|
+
console.groupCollapsed(...head);
|
|
269
|
+
console.groupCollapsed("...");
|
|
270
|
+
console.info("args", args);
|
|
271
|
+
console.info("response", res);
|
|
272
|
+
console.groupCollapsed("trace");
|
|
273
|
+
console.trace();
|
|
274
|
+
console.groupEnd();
|
|
275
|
+
console.groupEnd();
|
|
276
|
+
for (const [name2, ...log] of rest) {
|
|
277
|
+
console.groupCollapsed(name2);
|
|
278
|
+
console.info(...log);
|
|
279
|
+
console.groupEnd();
|
|
280
|
+
}
|
|
281
|
+
} else console.info("Weird log", head, ...rest);
|
|
282
|
+
}
|
|
283
|
+
} catch (err) {
|
|
284
|
+
error = err;
|
|
285
|
+
}
|
|
286
|
+
for (const _ of logStack) console.groupEnd();
|
|
287
|
+
if (error) console.error(`error loggin`, error);
|
|
288
|
+
logStack.clear();
|
|
289
|
+
}
|
|
290
|
+
return res;
|
|
291
|
+
}
|
|
292
|
+
} });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const finishAction = (val) => {
|
|
297
|
+
if (process.env.NODE_ENV === "development" && shouldDebug2) console.info("(debug) finishAction", { didSet });
|
|
298
|
+
if (didSet) {
|
|
299
|
+
storeInfo.triggerUpdate();
|
|
300
|
+
didSet = false;
|
|
301
|
+
}
|
|
302
|
+
return val;
|
|
303
|
+
};
|
|
304
|
+
let isTriggering = false;
|
|
305
|
+
const proxiedStore = new Proxy(storeInstance, {
|
|
306
|
+
get(_, key) {
|
|
307
|
+
if (key in wrappedActions) return wrappedActions[key];
|
|
308
|
+
if (key in passThroughKeys) return Reflect.get(storeInstance, key);
|
|
309
|
+
if (key === UNWRAP_PROXY) return storeInstance;
|
|
310
|
+
if (key === UNWRAP_STORE_INFO) return storeInfo;
|
|
311
|
+
if (storeAccessTrackers.size) storeAccessTrackers.forEach((cb) => cb(storeInfo));
|
|
312
|
+
if (typeof key !== "string") return Reflect.get(storeInstance, key);
|
|
313
|
+
if (!storeInfo.disableTracking) {
|
|
314
|
+
if (gettersState.isGetting) gettersState.curGetKeys.add(key);
|
|
315
|
+
}
|
|
316
|
+
if (key in getters) {
|
|
317
|
+
if (getCache.has(key)) return getCache.get(key);
|
|
318
|
+
curGetKeys.clear();
|
|
319
|
+
const isSubGetter = gettersState.isGetting;
|
|
320
|
+
gettersState.isGetting = true;
|
|
321
|
+
const res = getters[key].call(proxiedStore);
|
|
322
|
+
if (!isSubGetter) gettersState.isGetting = false;
|
|
323
|
+
for (const gk of curGetKeys) {
|
|
324
|
+
if (!depsToGetter.has(gk)) depsToGetter.set(gk, /* @__PURE__ */ new Set());
|
|
325
|
+
depsToGetter.get(gk).add(key);
|
|
326
|
+
}
|
|
327
|
+
getCache.set(key, res);
|
|
328
|
+
return res;
|
|
329
|
+
}
|
|
330
|
+
return Reflect.get(storeInstance, key);
|
|
331
|
+
},
|
|
332
|
+
set(target, key, value, receiver) {
|
|
333
|
+
const cur = Reflect.get(target, key);
|
|
334
|
+
const res = Reflect.set(target, key, value, receiver);
|
|
335
|
+
if (res && cur !== value) {
|
|
336
|
+
if (typeof key === "string") {
|
|
337
|
+
clearGetterCache(key);
|
|
338
|
+
if (shouldDebug2) {
|
|
339
|
+
setters.add({
|
|
340
|
+
key,
|
|
341
|
+
value
|
|
342
|
+
});
|
|
343
|
+
if (getShouldDebug(storeInfo)) console.info("(debug) SET", res, key, value);
|
|
344
|
+
}
|
|
345
|
+
if (process.env.NODE_ENV === "development" && shouldDebug2) console.info("SET...", {
|
|
346
|
+
key,
|
|
347
|
+
value
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
if (!isTriggering) {
|
|
351
|
+
isTriggering = true;
|
|
352
|
+
waitForEventLoop(() => {
|
|
353
|
+
storeInfo.triggerUpdate();
|
|
354
|
+
isTriggering = false;
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return res;
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
function clearGetterCache(setKey) {
|
|
362
|
+
const parentGetters = depsToGetter.get(setKey);
|
|
363
|
+
getCache.delete(setKey);
|
|
364
|
+
if (!parentGetters) return;
|
|
365
|
+
for (const gk of parentGetters) {
|
|
366
|
+
getCache.delete(gk);
|
|
367
|
+
if (depsToGetter.has(gk)) clearGetterCache(gk);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return proxiedStore;
|
|
397
371
|
}
|
|
398
|
-
const waitForEventLoop = process.env.NODE_ENV === "test" || false ? cb => cb() : queueMicrotask;
|
|
372
|
+
const waitForEventLoop = process.env.NODE_ENV === "test" || false ? (cb) => cb() : queueMicrotask;
|
|
399
373
|
let counter = 0;
|
|
400
374
|
const passThroughKeys = {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
375
|
+
subscribe: true,
|
|
376
|
+
_version: true,
|
|
377
|
+
_trackers: true,
|
|
378
|
+
$$typeof: true,
|
|
379
|
+
_listeners: true,
|
|
380
|
+
_enableTracking: true
|
|
407
381
|
};
|
|
408
382
|
function getShouldDebug(storeInfo) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
};
|
|
412
|
-
return [...storeInfo.trackers].some(tracker => tracker.component && shouldDebug(tracker.component, info));
|
|
383
|
+
const info = { storeInstance: storeInfo.store };
|
|
384
|
+
return [...storeInfo.trackers].some((tracker) => tracker.component && shouldDebug(tracker.component, info));
|
|
413
385
|
}
|
|
386
|
+
|
|
414
387
|
export { allStores, createStore, createUseStore, createUseStoreSelector, getOrCreateStore, getStore, getStoreInfo, onCreateStore, setIsInReaction, trackStoresAccess, useGlobalStore, useGlobalStoreSelector, useStore, useStoreDebug, useStoreSelector };
|
|
415
388
|
//# sourceMappingURL=useStore.mjs.map
|