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