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