@tamagui/use-store 1.61.3 → 1.62.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.js +12 -18
- package/dist/cjs/comparators.js.map +1 -1
- package/dist/cjs/comparators.native.js +40 -0
- package/dist/cjs/comparators.native.js.map +6 -0
- package/dist/cjs/configureUseStore.js +5 -9
- package/dist/cjs/configureUseStore.js.map +1 -1
- package/dist/cjs/configureUseStore.native.js +31 -0
- package/dist/cjs/configureUseStore.native.js.map +6 -0
- package/dist/cjs/constants.js +7 -12
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/constants.native.js +31 -0
- package/dist/cjs/constants.native.js.map +6 -0
- package/dist/cjs/decorators.js +6 -11
- package/dist/cjs/decorators.js.map +1 -1
- package/dist/cjs/decorators.native.js +30 -0
- package/dist/cjs/decorators.native.js.map +6 -0
- package/dist/cjs/helpers.js +13 -29
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/helpers.native.js +66 -0
- package/dist/cjs/helpers.native.js.map +6 -0
- package/dist/cjs/index.js +6 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.native.js +46 -0
- package/dist/cjs/index.native.js.map +6 -0
- package/dist/cjs/interfaces.js +3 -6
- package/dist/cjs/interfaces.js.map +1 -1
- package/dist/cjs/interfaces.native.js +15 -0
- package/dist/cjs/interfaces.native.js.map +6 -0
- package/dist/cjs/observe.js +35 -92
- package/dist/cjs/observe.js.map +1 -1
- package/dist/cjs/observe.native.js +103 -0
- package/dist/cjs/observe.native.js.map +6 -0
- package/dist/cjs/useStore.js +114 -318
- package/dist/cjs/useStore.js.map +2 -2
- package/dist/cjs/useStore.native.js +350 -0
- package/dist/cjs/useStore.native.js.map +6 -0
- package/dist/cjs/useStoreDebug.js +14 -33
- package/dist/cjs/useStoreDebug.js.map +1 -1
- package/dist/cjs/useStoreDebug.native.js +55 -0
- package/dist/cjs/useStoreDebug.native.js.map +6 -0
- package/dist/esm/comparators.js +7 -9
- package/dist/esm/comparators.js.map +1 -1
- package/dist/esm/constants.js +2 -3
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/decorators.js +1 -2
- package/dist/esm/decorators.js.map +1 -1
- package/dist/esm/helpers.js +7 -18
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/observe.js +29 -79
- package/dist/esm/observe.js.map +1 -1
- package/dist/esm/useStore.js +108 -303
- package/dist/esm/useStore.js.map +2 -2
- package/dist/esm/useStoreDebug.js +7 -20
- package/dist/esm/useStoreDebug.js.map +1 -1
- package/package.json +4 -4
package/dist/cjs/useStore.js
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
5
|
var __export = (target, all) => {
|
|
7
6
|
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable:
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: !0 });
|
|
8
|
+
}, __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from == "object" || typeof from == "function")
|
|
12
10
|
for (let key of __getOwnPropNames(from))
|
|
13
|
-
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
11
|
+
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
12
|
return to;
|
|
17
13
|
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value:
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
19
15
|
var useStore_exports = {};
|
|
20
16
|
__export(useStore_exports, {
|
|
21
17
|
allStores: () => allStores,
|
|
@@ -35,42 +31,28 @@ __export(useStore_exports, {
|
|
|
35
31
|
useStoreSelector: () => useStoreSelector
|
|
36
32
|
});
|
|
37
33
|
module.exports = __toCommonJS(useStore_exports);
|
|
38
|
-
var import_react = require("react");
|
|
39
|
-
var import_comparators = require("./comparators");
|
|
40
|
-
var import_configureUseStore = require("./configureUseStore");
|
|
41
|
-
var import_constants = require("./constants");
|
|
42
|
-
var import_helpers = require("./helpers");
|
|
43
|
-
var import_useStoreDebug = require("./useStoreDebug");
|
|
34
|
+
var import_react = require("react"), import_comparators = require("./comparators"), import_configureUseStore = require("./configureUseStore"), import_constants = require("./constants"), import_helpers = require("./helpers"), import_useStoreDebug = require("./useStoreDebug");
|
|
44
35
|
const idFn = (_) => _;
|
|
45
36
|
function useStore(StoreKlass, props, options = import_constants.defaultOptions) {
|
|
46
|
-
const selectorCb = (0, import_react.useCallback)(options.selector || idFn, []);
|
|
47
|
-
const selector = options.selector ? selectorCb : options.selector;
|
|
48
|
-
const info = getOrCreateStoreInfo(StoreKlass, props, options);
|
|
37
|
+
const selectorCb = (0, import_react.useCallback)(options.selector || idFn, []), selector = options.selector ? selectorCb : options.selector, info = getOrCreateStoreInfo(StoreKlass, props, options);
|
|
49
38
|
return useStoreFromInfo(info, selector, options);
|
|
50
39
|
}
|
|
51
40
|
function useStoreDebug(StoreKlass, props) {
|
|
52
|
-
return useStore(StoreKlass, props, { debug:
|
|
41
|
+
return useStore(StoreKlass, props, { debug: !0 });
|
|
53
42
|
}
|
|
54
43
|
function createStore(StoreKlass, props, options) {
|
|
55
|
-
|
|
56
|
-
return (_a = getOrCreateStoreInfo(StoreKlass, props, options)) == null ? void 0 : _a.store;
|
|
44
|
+
return getOrCreateStoreInfo(StoreKlass, props, options)?.store;
|
|
57
45
|
}
|
|
58
46
|
function useGlobalStore(instance, debug) {
|
|
59
|
-
const store = instance[import_constants.UNWRAP_PROXY];
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!info) {
|
|
63
|
-
throw new Error(`This store not created using createStore()`);
|
|
64
|
-
}
|
|
47
|
+
const store = instance[import_constants.UNWRAP_PROXY], uid = (0, import_helpers.getStoreUid)(store.constructor, store.props), info = import_helpers.cache.get(uid);
|
|
48
|
+
if (!info)
|
|
49
|
+
throw new Error("This store not created using createStore()");
|
|
65
50
|
return useStoreFromInfo(info, void 0, { debug });
|
|
66
51
|
}
|
|
67
52
|
function useGlobalStoreSelector(instance, selector, debug) {
|
|
68
|
-
const store = instance[import_constants.UNWRAP_PROXY];
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (!info) {
|
|
72
|
-
throw new Error(`This store not created using createStore()`);
|
|
73
|
-
}
|
|
53
|
+
const store = instance[import_constants.UNWRAP_PROXY], uid = (0, import_helpers.getStoreUid)(store.constructor, store.props), info = import_helpers.cache.get(uid);
|
|
54
|
+
if (!info)
|
|
55
|
+
throw new Error("This store not created using createStore()");
|
|
74
56
|
return useStoreFromInfo(info, selector, { debug });
|
|
75
57
|
}
|
|
76
58
|
function createUseStore(StoreKlass) {
|
|
@@ -79,75 +61,52 @@ function createUseStore(StoreKlass) {
|
|
|
79
61
|
};
|
|
80
62
|
}
|
|
81
63
|
function createUseStoreSelector(StoreKlass, selector) {
|
|
82
|
-
return (props) => {
|
|
83
|
-
return useStore(StoreKlass, props, { selector });
|
|
84
|
-
};
|
|
64
|
+
return (props) => useStore(StoreKlass, props, { selector });
|
|
85
65
|
}
|
|
86
66
|
function useStoreSelector(StoreKlass, selector, props) {
|
|
87
67
|
return useStore(StoreKlass, props, { selector });
|
|
88
68
|
}
|
|
89
69
|
const storeAccessTrackers = /* @__PURE__ */ new Set();
|
|
90
70
|
function trackStoresAccess(cb) {
|
|
91
|
-
storeAccessTrackers.add(cb)
|
|
92
|
-
return () => {
|
|
71
|
+
return storeAccessTrackers.add(cb), () => {
|
|
93
72
|
storeAccessTrackers.delete(cb);
|
|
94
73
|
};
|
|
95
74
|
}
|
|
96
75
|
function getStore(StoreKlass, props) {
|
|
97
|
-
|
|
98
|
-
return (_a = getStoreInfo(StoreKlass, props)) == null ? void 0 : _a.store;
|
|
76
|
+
return getStoreInfo(StoreKlass, props)?.store;
|
|
99
77
|
}
|
|
100
78
|
function getOrCreateStore(StoreKlass, props) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
})) == null ? void 0 : _a.store;
|
|
79
|
+
return getOrCreateStoreInfo(StoreKlass, props, {
|
|
80
|
+
refuseCreation: !1
|
|
81
|
+
})?.store;
|
|
105
82
|
}
|
|
106
83
|
function getStoreInfo(StoreKlass, props) {
|
|
107
84
|
return getOrCreateStoreInfo(StoreKlass, props, {
|
|
108
|
-
refuseCreation:
|
|
85
|
+
refuseCreation: !0
|
|
109
86
|
});
|
|
110
87
|
}
|
|
111
88
|
const onCreateListeners = /* @__PURE__ */ new Set();
|
|
112
89
|
function onCreateStore(cb) {
|
|
113
|
-
onCreateListeners.add(cb)
|
|
114
|
-
return () => {
|
|
90
|
+
return onCreateListeners.add(cb), () => {
|
|
115
91
|
onCreateListeners.delete(cb);
|
|
116
92
|
};
|
|
117
93
|
}
|
|
118
94
|
function getOrCreateStoreInfo(StoreKlass, props, options, propsKeyCalculated) {
|
|
119
|
-
|
|
120
|
-
if (!StoreKlass) {
|
|
95
|
+
if (!StoreKlass)
|
|
121
96
|
return null;
|
|
122
|
-
}
|
|
123
97
|
const uid = (0, import_helpers.getStoreUid)(StoreKlass, propsKeyCalculated ?? props);
|
|
124
|
-
if (!
|
|
98
|
+
if (!options?.avoidCache && import_helpers.cache.has(uid))
|
|
125
99
|
return import_helpers.cache.get(uid);
|
|
126
|
-
|
|
127
|
-
if (options == null ? void 0 : options.refuseCreation) {
|
|
100
|
+
if (options?.refuseCreation)
|
|
128
101
|
throw new Error(`No store exists (${StoreKlass.name}) with props: ${props}`);
|
|
129
|
-
}
|
|
130
102
|
const storeInstance = new StoreKlass(props);
|
|
131
103
|
storeInstance.props = props;
|
|
132
|
-
const getters = {};
|
|
133
|
-
const actions = {};
|
|
134
|
-
const stateKeys = /* @__PURE__ */ new Set();
|
|
135
|
-
const descriptors = (0, import_helpers.getStoreDescriptors)(storeInstance);
|
|
104
|
+
const getters = {}, actions = {}, stateKeys = /* @__PURE__ */ new Set(), descriptors = (0, import_helpers.getStoreDescriptors)(storeInstance);
|
|
136
105
|
for (const key in descriptors) {
|
|
137
106
|
const descriptor = descriptors[key];
|
|
138
|
-
|
|
139
|
-
actions[key] = descriptor.value;
|
|
140
|
-
} else if (typeof descriptor.get === "function") {
|
|
141
|
-
getters[key] = descriptor.get;
|
|
142
|
-
} else {
|
|
143
|
-
if (key !== "props" && key[0] !== "_") {
|
|
144
|
-
stateKeys.add(key);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
107
|
+
typeof descriptor.value == "function" ? actions[key] = descriptor.value : typeof descriptor.get == "function" ? getters[key] = descriptor.get : key !== "props" && key[0] !== "_" && stateKeys.add(key);
|
|
147
108
|
}
|
|
148
|
-
const keyComparators = storeInstance
|
|
149
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
150
|
-
const storeInfo = {
|
|
109
|
+
const keyComparators = storeInstance._comparators, listeners = /* @__PURE__ */ new Set(), storeInfo = {
|
|
151
110
|
uid,
|
|
152
111
|
keyComparators,
|
|
153
112
|
storeInstance,
|
|
@@ -155,217 +114,129 @@ function getOrCreateStoreInfo(StoreKlass, props, options, propsKeyCalculated) {
|
|
|
155
114
|
stateKeys,
|
|
156
115
|
props,
|
|
157
116
|
actions,
|
|
158
|
-
debug: options
|
|
159
|
-
disableTracking:
|
|
117
|
+
debug: options?.debug,
|
|
118
|
+
disableTracking: !1,
|
|
160
119
|
gettersState: {
|
|
161
120
|
getCache: /* @__PURE__ */ new Map(),
|
|
162
121
|
depsToGetter: /* @__PURE__ */ new Map(),
|
|
163
122
|
curGetKeys: /* @__PURE__ */ new Set(),
|
|
164
|
-
isGetting:
|
|
123
|
+
isGetting: !1
|
|
165
124
|
},
|
|
166
125
|
listeners,
|
|
167
126
|
trackers: /* @__PURE__ */ new Set(),
|
|
168
127
|
version: 0,
|
|
169
|
-
subscribe: (onChanged) => {
|
|
170
|
-
listeners.
|
|
171
|
-
|
|
172
|
-
listeners.delete(onChanged);
|
|
173
|
-
};
|
|
174
|
-
},
|
|
128
|
+
subscribe: (onChanged) => (listeners.add(onChanged), () => {
|
|
129
|
+
listeners.delete(onChanged);
|
|
130
|
+
}),
|
|
175
131
|
triggerUpdate: () => {
|
|
176
132
|
storeInfo.version = (storeInfo.version + 1) % Number.MAX_SAFE_INTEGER;
|
|
177
|
-
for (const cb of listeners)
|
|
133
|
+
for (const cb of listeners)
|
|
178
134
|
cb();
|
|
179
|
-
}
|
|
180
135
|
}
|
|
181
|
-
}
|
|
182
|
-
const store = createProxiedStore(
|
|
136
|
+
}, store = createProxiedStore(
|
|
183
137
|
// we assign store right after and proxiedStore never accesses it until later on
|
|
184
138
|
storeInfo
|
|
185
139
|
);
|
|
186
|
-
|
|
187
|
-
allStores[uid] = store;
|
|
188
|
-
}
|
|
189
|
-
(_a = store.mount) == null ? void 0 : _a.call(store);
|
|
190
|
-
storeInfo.store = store;
|
|
140
|
+
process.env.NODE_ENV === "development" && (allStores[uid] = store), store.mount?.(), storeInfo.store = store;
|
|
191
141
|
const result = storeInfo;
|
|
192
|
-
import_helpers.cache.set(uid, result);
|
|
193
|
-
onCreateListeners.forEach((cb) => cb(result));
|
|
194
|
-
return result;
|
|
142
|
+
return import_helpers.cache.set(uid, result), onCreateListeners.forEach((cb) => cb(result)), result;
|
|
195
143
|
}
|
|
196
|
-
const allStores = {}
|
|
197
|
-
|
|
198
|
-
const selectKeys = (obj, keys) => {
|
|
199
|
-
if (!keys.length) {
|
|
144
|
+
const allStores = {}, emptyObj = {}, selectKeys = (obj, keys) => {
|
|
145
|
+
if (!keys.length)
|
|
200
146
|
return emptyObj;
|
|
201
|
-
}
|
|
202
147
|
const res = {};
|
|
203
|
-
for (const key of keys)
|
|
148
|
+
for (const key of keys)
|
|
204
149
|
res[key] = obj[key];
|
|
205
|
-
}
|
|
206
150
|
return res;
|
|
207
151
|
};
|
|
208
|
-
let isInReaction =
|
|
152
|
+
let isInReaction = !1;
|
|
209
153
|
const setIsInReaction = (val) => {
|
|
210
154
|
isInReaction = val;
|
|
211
155
|
};
|
|
212
156
|
function useStoreFromInfo(info, userSelector, options) {
|
|
213
|
-
const store = info
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
lastKeys: null
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
const curInternal = internal.current;
|
|
225
|
-
const shouldPrintDebug = options == null ? void 0 : options.debug;
|
|
226
|
-
const getSnapshot = (0, import_react.useCallback)(() => {
|
|
157
|
+
const store = info?.store, internal = (0, import_react.useRef)(), component = (0, import_useStoreDebug.useCurrentComponent)();
|
|
158
|
+
internal.current || (internal.current = {
|
|
159
|
+
component,
|
|
160
|
+
tracked: /* @__PURE__ */ new Set(),
|
|
161
|
+
last: null,
|
|
162
|
+
lastKeys: null
|
|
163
|
+
});
|
|
164
|
+
const curInternal = internal.current, shouldPrintDebug = options?.debug, getSnapshot = (0, import_react.useCallback)(() => {
|
|
227
165
|
if (!info || !store)
|
|
228
166
|
return;
|
|
229
|
-
const curInternal2 = internal.current;
|
|
230
|
-
|
|
231
|
-
const nextKeys = `${info.version}${keys.join("")}${userSelector || ""}`;
|
|
232
|
-
const lastKeys = curInternal2.lastKeys;
|
|
233
|
-
if (nextKeys === curInternal2.lastKeys) {
|
|
167
|
+
const curInternal2 = internal.current, keys = [...curInternal2.tracked.size ? curInternal2.tracked : info.stateKeys], nextKeys = `${info.version}${keys.join("")}${userSelector || ""}`, lastKeys = curInternal2.lastKeys;
|
|
168
|
+
if (nextKeys === curInternal2.lastKeys)
|
|
234
169
|
return curInternal2.last;
|
|
235
|
-
}
|
|
236
170
|
curInternal2.lastKeys = nextKeys;
|
|
237
171
|
let snap;
|
|
238
|
-
info.disableTracking =
|
|
172
|
+
info.disableTracking = !0;
|
|
239
173
|
const last = curInternal2.last;
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
} else {
|
|
243
|
-
snap = selectKeys(store, keys);
|
|
244
|
-
}
|
|
245
|
-
info.disableTracking = false;
|
|
246
|
-
const isUnchanged = typeof last !== "undefined" && (0, import_comparators.isEqualSubsetShallow)(last, snap, {
|
|
174
|
+
userSelector ? snap = userSelector(store) : snap = selectKeys(store, keys), info.disableTracking = !1;
|
|
175
|
+
const isUnchanged = typeof last < "u" && (0, import_comparators.isEqualSubsetShallow)(last, snap, {
|
|
247
176
|
keyComparators: info.keyComparators
|
|
248
177
|
});
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
if (isUnchanged) {
|
|
253
|
-
return last;
|
|
254
|
-
}
|
|
255
|
-
curInternal2.last = snap;
|
|
256
|
-
return snap;
|
|
257
|
-
}, [store]);
|
|
258
|
-
const state = (0, import_react.useSyncExternalStore)((info == null ? void 0 : info.subscribe) || idFn, getSnapshot, getSnapshot);
|
|
259
|
-
if (!info || !store || !state) {
|
|
260
|
-
return state;
|
|
261
|
-
}
|
|
262
|
-
if (userSelector) {
|
|
263
|
-
return state;
|
|
264
|
-
}
|
|
265
|
-
return new Proxy(store, {
|
|
178
|
+
return shouldPrintDebug && console.log("\u{1F311} getSnapshot", { storeState: selectKeys(store, Object.keys(store)), userSelector, info, isUnchanged, component, keys, last, snap, curInternal: curInternal2, nextKeys, lastKeys }), isUnchanged ? last : (curInternal2.last = snap, snap);
|
|
179
|
+
}, [store]), state = (0, import_react.useSyncExternalStore)(info?.subscribe || idFn, getSnapshot, getSnapshot);
|
|
180
|
+
return !info || !store || !state || userSelector ? state : new Proxy(store, {
|
|
266
181
|
get(target, key) {
|
|
267
182
|
const curVal = Reflect.get(target, key);
|
|
268
|
-
if (isInReaction)
|
|
183
|
+
if (isInReaction)
|
|
269
184
|
return curVal;
|
|
270
|
-
}
|
|
271
185
|
const keyString = key;
|
|
272
|
-
|
|
273
|
-
if (shouldPrintDebug) {
|
|
274
|
-
console.log("tracking", keyString);
|
|
275
|
-
}
|
|
276
|
-
curInternal.tracked.add(keyString);
|
|
277
|
-
}
|
|
278
|
-
if (Reflect.has(state, key)) {
|
|
279
|
-
return Reflect.get(state, key);
|
|
280
|
-
}
|
|
281
|
-
return curVal;
|
|
186
|
+
return (info.stateKeys.has(keyString) || keyString in info.getters) && (shouldPrintDebug && console.log("tracking", keyString), curInternal.tracked.add(keyString)), Reflect.has(state, key) ? Reflect.get(state, key) : curVal;
|
|
282
187
|
}
|
|
283
188
|
});
|
|
284
189
|
}
|
|
285
190
|
let setters = /* @__PURE__ */ new Set();
|
|
286
191
|
const logStack = /* @__PURE__ */ new Set();
|
|
287
192
|
function createProxiedStore(storeInfo) {
|
|
288
|
-
const { actions, storeInstance, getters, gettersState } = storeInfo;
|
|
289
|
-
|
|
290
|
-
const constr = storeInstance.constructor;
|
|
291
|
-
const shouldDebug2 = storeInfo.debug ?? import_useStoreDebug.DebugStores.has(constr);
|
|
292
|
-
let didSet = false;
|
|
193
|
+
const { actions, storeInstance, getters, gettersState } = storeInfo, { getCache, curGetKeys, depsToGetter } = gettersState, constr = storeInstance.constructor, shouldDebug2 = storeInfo.debug ?? import_useStoreDebug.DebugStores.has(constr);
|
|
194
|
+
let didSet = !1;
|
|
293
195
|
const wrappedActions = {};
|
|
294
196
|
for (const key in actions) {
|
|
295
|
-
if (key === "subscribe")
|
|
197
|
+
if (key === "subscribe")
|
|
296
198
|
continue;
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const isGetFn = key.startsWith("get");
|
|
300
|
-
wrappedActions[key] = function useStoreAction(...args) {
|
|
199
|
+
const actionFn = actions[key], isGetFn = key.startsWith("get");
|
|
200
|
+
if (wrappedActions[key] = function(...args) {
|
|
301
201
|
let res;
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (process.env.NODE_ENV === "development" && shouldDebug2) {
|
|
306
|
-
console.log("(debug) startAction", key);
|
|
307
|
-
}
|
|
308
|
-
res = Reflect.apply(actionFn, proxiedStore, args);
|
|
309
|
-
if (res instanceof Promise) {
|
|
310
|
-
return res.then(finishAction);
|
|
311
|
-
}
|
|
312
|
-
finishAction();
|
|
313
|
-
return res;
|
|
314
|
-
};
|
|
315
|
-
if (process.env.NODE_ENV === "development") {
|
|
316
|
-
let hashCode2 = function(str) {
|
|
202
|
+
return isGetFn || gettersState.isGetting ? Reflect.apply(actionFn, proxiedStore, args) : (process.env.NODE_ENV === "development" && shouldDebug2 && console.log("(debug) startAction", key), res = Reflect.apply(actionFn, proxiedStore, args), res instanceof Promise ? res.then(finishAction) : (finishAction(), res));
|
|
203
|
+
}, process.env.NODE_ENV === "development") {
|
|
204
|
+
let hashCode = function(str) {
|
|
317
205
|
let hash = 0;
|
|
318
|
-
for (let i = 0; i < str.length; i++)
|
|
206
|
+
for (let i = 0; i < str.length; i++)
|
|
319
207
|
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
320
|
-
}
|
|
321
208
|
return hash;
|
|
322
|
-
},
|
|
323
|
-
return `hsl(${
|
|
209
|
+
}, strColor = function(str) {
|
|
210
|
+
return `hsl(${hashCode(str) % 360}, 90%, 40%)`;
|
|
324
211
|
};
|
|
325
|
-
var hashCode = hashCode2, strColor = strColor2;
|
|
326
212
|
if (!key.startsWith("get") && !key.startsWith("_") && key !== "subscribe") {
|
|
327
213
|
const ogAction = wrappedActions[key];
|
|
328
214
|
wrappedActions[key] = new Proxy(ogAction, {
|
|
329
215
|
apply(target, thisArg, args) {
|
|
330
216
|
const isDebugging = shouldDebug2 || storeInfo.debug;
|
|
331
|
-
|
|
332
|
-
if (!shouldLog) {
|
|
217
|
+
if (!(process.env.LOG_LEVEL !== "0" && (isDebugging || import_configureUseStore.configureOpts.logLevel !== "error")))
|
|
333
218
|
return Reflect.apply(target, thisArg, args);
|
|
334
|
-
}
|
|
335
219
|
setters = /* @__PURE__ */ new Set();
|
|
336
|
-
const curSetters = setters;
|
|
337
|
-
const isTopLevelLogger = logStack.size == 0;
|
|
338
|
-
const logs = /* @__PURE__ */ new Set();
|
|
220
|
+
const curSetters = setters, isTopLevelLogger = logStack.size == 0, logs = /* @__PURE__ */ new Set();
|
|
339
221
|
logStack.add(logs);
|
|
340
222
|
let res;
|
|
341
223
|
const id = counter++;
|
|
342
224
|
try {
|
|
343
225
|
res = Reflect.apply(target, thisArg, args);
|
|
344
226
|
} catch (err) {
|
|
345
|
-
console.error("Error", err);
|
|
346
|
-
throw err;
|
|
227
|
+
throw console.error("Error", err), err;
|
|
347
228
|
} finally {
|
|
348
229
|
logStack.add("end");
|
|
349
|
-
const name = constr.name;
|
|
350
|
-
|
|
351
|
-
const simpleArgs = args.map(import_helpers.simpleStr);
|
|
352
|
-
logs.add([
|
|
230
|
+
const name = constr.name, color = strColor(name), simpleArgs = args.map(import_helpers.simpleStr);
|
|
231
|
+
if (logs.add([
|
|
353
232
|
`%c \u{1F311} ${id} ${name.padStart(
|
|
354
233
|
isTopLevelLogger ? 8 : 4
|
|
355
234
|
)}%c.${key}(${simpleArgs.join(", ")})${isTopLevelLogger && logStack.size > 1 ? ` (+${logStack.size - 1})` : ""}`,
|
|
356
235
|
`color: ${color};`,
|
|
357
236
|
"color: black;"
|
|
358
|
-
])
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
362
|
-
logs.add([` SET ${key2} ${value}`, value]);
|
|
363
|
-
} else {
|
|
364
|
-
logs.add([` SET ${key2}`, value]);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
if (isTopLevelLogger) {
|
|
237
|
+
]), curSetters.size && curSetters.forEach(({ key: key2, value }) => {
|
|
238
|
+
typeof value == "string" || typeof value == "number" || typeof value == "boolean" ? logs.add([` SET ${key2} ${value}`, value]) : logs.add([` SET ${key2}`, value]);
|
|
239
|
+
}), isTopLevelLogger) {
|
|
369
240
|
let error = null;
|
|
370
241
|
try {
|
|
371
242
|
for (const item of [...logStack]) {
|
|
@@ -375,33 +246,18 @@ function createProxiedStore(storeInfo) {
|
|
|
375
246
|
}
|
|
376
247
|
const [head, ...rest] = item;
|
|
377
248
|
if (head) {
|
|
378
|
-
console.groupCollapsed(...head);
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
console.groupCollapsed("trace");
|
|
383
|
-
console.trace();
|
|
384
|
-
console.groupEnd();
|
|
385
|
-
console.groupEnd();
|
|
386
|
-
for (const [name2, ...log] of rest) {
|
|
387
|
-
console.groupCollapsed(name2);
|
|
388
|
-
console.log(...log);
|
|
389
|
-
console.groupEnd();
|
|
390
|
-
}
|
|
391
|
-
} else {
|
|
249
|
+
console.groupCollapsed(...head), console.groupCollapsed("..."), console.log("args", args), console.log("response", res), console.groupCollapsed("trace"), console.trace(), console.groupEnd(), console.groupEnd();
|
|
250
|
+
for (const [name2, ...log] of rest)
|
|
251
|
+
console.groupCollapsed(name2), console.log(...log), console.groupEnd();
|
|
252
|
+
} else
|
|
392
253
|
console.log("Weird log", head, ...rest);
|
|
393
|
-
}
|
|
394
254
|
}
|
|
395
255
|
} catch (err) {
|
|
396
256
|
error = err;
|
|
397
257
|
}
|
|
398
|
-
for (const _ of [...logStack])
|
|
258
|
+
for (const _ of [...logStack])
|
|
399
259
|
console.groupEnd();
|
|
400
|
-
|
|
401
|
-
if (error) {
|
|
402
|
-
console.error(`error loggin`, error);
|
|
403
|
-
}
|
|
404
|
-
logStack.clear();
|
|
260
|
+
error && console.error("error loggin", error), logStack.clear();
|
|
405
261
|
}
|
|
406
262
|
return res;
|
|
407
263
|
}
|
|
@@ -410,124 +266,64 @@ function createProxiedStore(storeInfo) {
|
|
|
410
266
|
}
|
|
411
267
|
}
|
|
412
268
|
}
|
|
413
|
-
const finishAction = (val) => {
|
|
414
|
-
|
|
415
|
-
console.log("(debug) finishAction", { didSet });
|
|
416
|
-
}
|
|
417
|
-
if (didSet) {
|
|
418
|
-
storeInfo.triggerUpdate();
|
|
419
|
-
didSet = false;
|
|
420
|
-
}
|
|
421
|
-
return val;
|
|
422
|
-
};
|
|
423
|
-
let isTriggering = false;
|
|
269
|
+
const finishAction = (val) => (process.env.NODE_ENV === "development" && shouldDebug2 && console.log("(debug) finishAction", { didSet }), didSet && (storeInfo.triggerUpdate(), didSet = !1), val);
|
|
270
|
+
let isTriggering = !1;
|
|
424
271
|
const proxiedStore = new Proxy(storeInstance, {
|
|
425
272
|
// GET
|
|
426
273
|
get(_, key) {
|
|
427
|
-
if (key in wrappedActions)
|
|
274
|
+
if (key in wrappedActions)
|
|
428
275
|
return wrappedActions[key];
|
|
429
|
-
|
|
430
|
-
if (key in passThroughKeys) {
|
|
276
|
+
if (key in passThroughKeys)
|
|
431
277
|
return Reflect.get(storeInstance, key);
|
|
432
|
-
|
|
433
|
-
if (key === import_constants.UNWRAP_PROXY) {
|
|
278
|
+
if (key === import_constants.UNWRAP_PROXY)
|
|
434
279
|
return storeInstance;
|
|
435
|
-
|
|
436
|
-
if (key === import_helpers.UNWRAP_STORE_INFO) {
|
|
280
|
+
if (key === import_helpers.UNWRAP_STORE_INFO)
|
|
437
281
|
return storeInfo;
|
|
438
|
-
|
|
439
|
-
if (storeAccessTrackers.size) {
|
|
440
|
-
storeAccessTrackers.forEach((cb) => cb(storeInfo));
|
|
441
|
-
}
|
|
442
|
-
if (typeof key !== "string") {
|
|
282
|
+
if (storeAccessTrackers.size && storeAccessTrackers.forEach((cb) => cb(storeInfo)), typeof key != "string")
|
|
443
283
|
return Reflect.get(storeInstance, key);
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
if (gettersState.isGetting) {
|
|
447
|
-
gettersState.curGetKeys.add(key);
|
|
448
|
-
} else {
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
if (key in getters) {
|
|
452
|
-
if (getCache.has(key)) {
|
|
284
|
+
if (storeInfo.disableTracking || gettersState.isGetting && gettersState.curGetKeys.add(key), key in getters) {
|
|
285
|
+
if (getCache.has(key))
|
|
453
286
|
return getCache.get(key);
|
|
454
|
-
}
|
|
455
287
|
curGetKeys.clear();
|
|
456
288
|
const isSubGetter = gettersState.isGetting;
|
|
457
|
-
gettersState.isGetting =
|
|
289
|
+
gettersState.isGetting = !0;
|
|
458
290
|
const res = getters[key].call(proxiedStore);
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
if (!depsToGetter.has(gk)) {
|
|
464
|
-
depsToGetter.set(gk, /* @__PURE__ */ new Set());
|
|
465
|
-
}
|
|
466
|
-
const cur = depsToGetter.get(gk);
|
|
467
|
-
cur.add(key);
|
|
468
|
-
}
|
|
469
|
-
getCache.set(key, res);
|
|
470
|
-
return res;
|
|
291
|
+
isSubGetter || (gettersState.isGetting = !1);
|
|
292
|
+
for (const gk of curGetKeys)
|
|
293
|
+
depsToGetter.has(gk) || depsToGetter.set(gk, /* @__PURE__ */ new Set()), depsToGetter.get(gk).add(key);
|
|
294
|
+
return getCache.set(key, res), res;
|
|
471
295
|
}
|
|
472
296
|
return Reflect.get(storeInstance, key);
|
|
473
297
|
},
|
|
474
298
|
// SET
|
|
475
299
|
set(target, key, value, receiver) {
|
|
476
|
-
const cur = Reflect.get(target, key);
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
clearGetterCache(key);
|
|
481
|
-
}
|
|
482
|
-
if (shouldDebug2) {
|
|
483
|
-
setters.add({ key, value });
|
|
484
|
-
if (getShouldDebug(storeInfo)) {
|
|
485
|
-
console.log("(debug) SET", res, key, value);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
if (process.env.NODE_ENV === "development" && shouldDebug2) {
|
|
489
|
-
console.log("SET...", { key, value });
|
|
490
|
-
}
|
|
491
|
-
if (!isTriggering) {
|
|
492
|
-
isTriggering = true;
|
|
493
|
-
waitForEventLoop(() => {
|
|
494
|
-
storeInfo.triggerUpdate();
|
|
495
|
-
isTriggering = false;
|
|
496
|
-
});
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
return res;
|
|
300
|
+
const cur = Reflect.get(target, key), res = Reflect.set(target, key, value, receiver);
|
|
301
|
+
return res && cur !== value && (typeof key == "string" && clearGetterCache(key), shouldDebug2 && (setters.add({ key, value }), getShouldDebug(storeInfo) && console.log("(debug) SET", res, key, value)), process.env.NODE_ENV === "development" && shouldDebug2 && console.log("SET...", { key, value }), isTriggering || (isTriggering = !0, waitForEventLoop(() => {
|
|
302
|
+
storeInfo.triggerUpdate(), isTriggering = !1;
|
|
303
|
+
}))), res;
|
|
500
304
|
}
|
|
501
305
|
});
|
|
502
306
|
function clearGetterCache(setKey) {
|
|
503
307
|
const parentGetters = depsToGetter.get(setKey);
|
|
504
|
-
getCache.delete(setKey)
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
for (const gk of parentGetters) {
|
|
509
|
-
getCache.delete(gk);
|
|
510
|
-
if (depsToGetter.has(gk)) {
|
|
511
|
-
clearGetterCache(gk);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
308
|
+
if (getCache.delete(setKey), !!parentGetters)
|
|
309
|
+
for (const gk of parentGetters)
|
|
310
|
+
getCache.delete(gk), depsToGetter.has(gk) && clearGetterCache(gk);
|
|
514
311
|
}
|
|
515
312
|
return proxiedStore;
|
|
516
313
|
}
|
|
517
314
|
const waitForEventLoop = process.env.NODE_ENV === "test" ? (cb) => cb() : queueMicrotask;
|
|
518
315
|
let counter = 0;
|
|
519
316
|
const passThroughKeys = {
|
|
520
|
-
subscribe:
|
|
521
|
-
_version:
|
|
522
|
-
_trackers:
|
|
523
|
-
$$typeof:
|
|
524
|
-
_listeners:
|
|
525
|
-
_enableTracking:
|
|
317
|
+
subscribe: !0,
|
|
318
|
+
_version: !0,
|
|
319
|
+
_trackers: !0,
|
|
320
|
+
$$typeof: !0,
|
|
321
|
+
_listeners: !0,
|
|
322
|
+
_enableTracking: !0
|
|
526
323
|
};
|
|
527
324
|
function getShouldDebug(storeInfo) {
|
|
528
325
|
const info = { storeInstance: storeInfo.store };
|
|
529
|
-
|
|
530
|
-
return [...trackers].some(
|
|
326
|
+
return [...storeInfo.trackers].some(
|
|
531
327
|
(tracker) => tracker.component && (0, import_useStoreDebug.shouldDebug)(tracker.component, info)
|
|
532
328
|
);
|
|
533
329
|
}
|