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