cross-state 0.29.0 → 0.29.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.
@@ -1,392 +1,613 @@
1
1
  "use strict";
2
2
  const store = require("./store.cjs");
3
+ const require$$0 = require("react");
4
+ const jsxRuntime = require("react/jsx-runtime");
3
5
  const hash = require("./hash.cjs");
4
- class ResourceGroup {
5
- constructor(name) {
6
- this.name = name;
7
- this.refMap = /* @__PURE__ */ new WeakMap();
8
- this.refSet = /* @__PURE__ */ new Set();
9
- store.autobind(ResourceGroup);
10
- }
11
- add(resource) {
12
- const ref = new WeakRef(resource);
13
- this.refMap.set(resource, ref);
14
- this.refSet.add(ref);
15
- }
16
- delete(resource) {
17
- const ref = this.refMap.get(resource);
18
- if (ref) {
19
- this.refMap.delete(resource);
20
- this.refSet.delete(ref);
21
- }
6
+ const unwrapProxySymbol = /* @__PURE__ */ Symbol("unwrapProxy");
7
+ function isPlainObject(value) {
8
+ return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
9
+ }
10
+ function trackingProxy(value, equals = store.deepEqual) {
11
+ if (!isPlainObject(value) && !Array.isArray(value)) {
12
+ return [value, (other) => equals(value, other)];
22
13
  }
23
- invalidateAll() {
24
- for (const ref of this.refSet) {
25
- const resource = ref.deref();
26
- if (resource) {
27
- resource.invalidateAll();
28
- } else {
29
- this.refSet.delete(ref);
14
+ value = value[unwrapProxySymbol] ?? value;
15
+ const deps = new Array();
16
+ const revokations = new Array();
17
+ let revoked = false;
18
+ function trackComplexProp(function_, ...args) {
19
+ const [proxiedValue, equals2, revoke] = trackingProxy(function_(value, ...args));
20
+ deps.push((otherValue) => {
21
+ if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {
22
+ return false;
30
23
  }
24
+ return equals2(function_(otherValue, ...args));
25
+ });
26
+ if (revoke) {
27
+ revokations.push(revoke);
31
28
  }
29
+ return proxiedValue;
30
+ }
31
+ function trackSimpleProp(function_, ...args) {
32
+ const calculatedValue = function_(value, ...args);
33
+ deps.push((otherValue) => {
34
+ return function_(otherValue, ...args) === calculatedValue;
35
+ });
36
+ return calculatedValue;
32
37
  }
33
- clearAll() {
34
- for (const ref of this.refSet) {
35
- const resource = ref.deref();
36
- if (resource) {
37
- resource.clearAll();
38
- } else {
39
- this.refSet.delete(ref);
38
+ const proxy = new Proxy(value, {
39
+ get(target, p, receiver) {
40
+ if (p === unwrapProxySymbol) {
41
+ return value;
42
+ }
43
+ if (revoked) {
44
+ return target[p];
40
45
  }
46
+ const { writable, configurable } = Object.getOwnPropertyDescriptor(target, p) ?? {};
47
+ if (writable === false && configurable === false) {
48
+ return target[p];
49
+ }
50
+ return trackComplexProp(Reflect.get, p, receiver);
51
+ },
52
+ getOwnPropertyDescriptor(target, p) {
53
+ const { writable, configurable } = Object.getOwnPropertyDescriptor(target, p) ?? {};
54
+ if (writable === false && configurable === false) {
55
+ return Reflect.getOwnPropertyDescriptor(target, p);
56
+ }
57
+ return trackComplexProp(Reflect.getOwnPropertyDescriptor, p);
58
+ },
59
+ ownKeys() {
60
+ return trackComplexProp(Reflect.ownKeys);
61
+ },
62
+ getPrototypeOf() {
63
+ return trackSimpleProp(Reflect.getPrototypeOf);
64
+ },
65
+ has(_target, p) {
66
+ return trackSimpleProp(Reflect.has, p);
67
+ },
68
+ isExtensible() {
69
+ return trackSimpleProp(Reflect.isExtensible);
41
70
  }
42
- }
43
- }
44
- const allResources = /* @__PURE__ */ new ResourceGroup();
45
- function createResourceGroup(name) {
46
- return new ResourceGroup(name);
71
+ });
72
+ return [
73
+ proxy,
74
+ (other) => !!other && deps.every((equals2) => equals2(other)),
75
+ () => {
76
+ revoked = true;
77
+ revokations.forEach((revoke) => revoke());
78
+ }
79
+ ];
47
80
  }
48
- class InstanceCache {
49
- constructor(factory, cacheTime) {
50
- this.factory = factory;
51
- this.cacheTime = cacheTime;
52
- this.cache = /* @__PURE__ */ new Map();
53
- this.interval = this.cacheTime ? setInterval(() => this.cleanup(), Math.max(this.cacheTime / 10, 1)) : void 0;
54
- }
55
- cleanup() {
56
- var _a;
57
- const cutoff = this.now() - (this.cacheTime ?? 0);
58
- for (const [key, entry] of this.cache.entries()) {
59
- if (entry.ref && entry.t <= cutoff) {
60
- delete entry.ref;
81
+ var withSelector = { exports: {} };
82
+ var withSelector_development = {};
83
+ var shim = { exports: {} };
84
+ var useSyncExternalStoreShim_development = {};
85
+ /**
86
+ * @license React
87
+ * use-sync-external-store-shim.development.js
88
+ *
89
+ * Copyright (c) Facebook, Inc. and its affiliates.
90
+ *
91
+ * This source code is licensed under the MIT license found in the
92
+ * LICENSE file in the root directory of this source tree.
93
+ */
94
+ var hasRequiredUseSyncExternalStoreShim_development;
95
+ function requireUseSyncExternalStoreShim_development() {
96
+ if (hasRequiredUseSyncExternalStoreShim_development)
97
+ return useSyncExternalStoreShim_development;
98
+ hasRequiredUseSyncExternalStoreShim_development = 1;
99
+ if (process.env.NODE_ENV !== "production") {
100
+ (function() {
101
+ if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart === "function") {
102
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
61
103
  }
62
- if (!entry.ref && !((_a = entry.weakRef) == null ? void 0 : _a.deref())) {
63
- this.cache.delete(key);
104
+ var React = require$$0;
105
+ var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
106
+ function error(format) {
107
+ {
108
+ {
109
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
110
+ args[_key2 - 1] = arguments[_key2];
111
+ }
112
+ printWarning("error", format, args);
113
+ }
114
+ }
64
115
  }
65
- }
116
+ function printWarning(level, format, args) {
117
+ {
118
+ var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
119
+ var stack = ReactDebugCurrentFrame.getStackAddendum();
120
+ if (stack !== "") {
121
+ format += "%s";
122
+ args = args.concat([stack]);
123
+ }
124
+ var argsWithFormat = args.map(function(item) {
125
+ return String(item);
126
+ });
127
+ argsWithFormat.unshift("Warning: " + format);
128
+ Function.prototype.apply.call(console[level], console, argsWithFormat);
129
+ }
130
+ }
131
+ function is(x, y) {
132
+ return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y;
133
+ }
134
+ var objectIs = typeof Object.is === "function" ? Object.is : is;
135
+ var useState = React.useState, useEffect = React.useEffect, useLayoutEffect = React.useLayoutEffect, useDebugValue = React.useDebugValue;
136
+ var didWarnOld18Alpha = false;
137
+ var didWarnUncachedGetSnapshot = false;
138
+ function useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) {
139
+ {
140
+ if (!didWarnOld18Alpha) {
141
+ if (React.startTransition !== void 0) {
142
+ didWarnOld18Alpha = true;
143
+ error("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release.");
144
+ }
145
+ }
146
+ }
147
+ var value = getSnapshot();
148
+ {
149
+ if (!didWarnUncachedGetSnapshot) {
150
+ var cachedValue = getSnapshot();
151
+ if (!objectIs(value, cachedValue)) {
152
+ error("The result of getSnapshot should be cached to avoid an infinite loop");
153
+ didWarnUncachedGetSnapshot = true;
154
+ }
155
+ }
156
+ }
157
+ var _useState = useState({
158
+ inst: {
159
+ value,
160
+ getSnapshot
161
+ }
162
+ }), inst = _useState[0].inst, forceUpdate = _useState[1];
163
+ useLayoutEffect(function() {
164
+ inst.value = value;
165
+ inst.getSnapshot = getSnapshot;
166
+ if (checkIfSnapshotChanged(inst)) {
167
+ forceUpdate({
168
+ inst
169
+ });
170
+ }
171
+ }, [subscribe, value, getSnapshot]);
172
+ useEffect(function() {
173
+ if (checkIfSnapshotChanged(inst)) {
174
+ forceUpdate({
175
+ inst
176
+ });
177
+ }
178
+ var handleStoreChange = function() {
179
+ if (checkIfSnapshotChanged(inst)) {
180
+ forceUpdate({
181
+ inst
182
+ });
183
+ }
184
+ };
185
+ return subscribe(handleStoreChange);
186
+ }, [subscribe]);
187
+ useDebugValue(value);
188
+ return value;
189
+ }
190
+ function checkIfSnapshotChanged(inst) {
191
+ var latestGetSnapshot = inst.getSnapshot;
192
+ var prevValue = inst.value;
193
+ try {
194
+ var nextValue = latestGetSnapshot();
195
+ return !objectIs(prevValue, nextValue);
196
+ } catch (error2) {
197
+ return true;
198
+ }
199
+ }
200
+ function useSyncExternalStore$1(subscribe, getSnapshot, getServerSnapshot) {
201
+ return getSnapshot();
202
+ }
203
+ var canUseDOM = !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
204
+ var isServerEnvironment = !canUseDOM;
205
+ var shim2 = isServerEnvironment ? useSyncExternalStore$1 : useSyncExternalStore;
206
+ var useSyncExternalStore$2 = React.useSyncExternalStore !== void 0 ? React.useSyncExternalStore : shim2;
207
+ useSyncExternalStoreShim_development.useSyncExternalStore = useSyncExternalStore$2;
208
+ if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop === "function") {
209
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
210
+ }
211
+ })();
66
212
  }
67
- get(...args) {
68
- var _a;
69
- const key = hash.hash(args);
70
- let entry = this.cache.get(key);
71
- let value = (entry == null ? void 0 : entry.ref) ?? ((_a = entry == null ? void 0 : entry.weakRef) == null ? void 0 : _a.deref());
72
- if (!entry || !value) {
73
- value = this.factory(...args);
74
- entry = {
75
- t: this.now(),
76
- ref: value,
77
- weakRef: new WeakRef(value)
78
- };
79
- this.cache.set(key, entry);
80
- } else {
81
- entry.t = this.now();
82
- entry.ref ?? (entry.ref = value);
83
- }
84
- return value;
213
+ return useSyncExternalStoreShim_development;
214
+ }
215
+ var useSyncExternalStoreShim_production_min = {};
216
+ /**
217
+ * @license React
218
+ * use-sync-external-store-shim.production.min.js
219
+ *
220
+ * Copyright (c) Facebook, Inc. and its affiliates.
221
+ *
222
+ * This source code is licensed under the MIT license found in the
223
+ * LICENSE file in the root directory of this source tree.
224
+ */
225
+ var hasRequiredUseSyncExternalStoreShim_production_min;
226
+ function requireUseSyncExternalStoreShim_production_min() {
227
+ if (hasRequiredUseSyncExternalStoreShim_production_min)
228
+ return useSyncExternalStoreShim_production_min;
229
+ hasRequiredUseSyncExternalStoreShim_production_min = 1;
230
+ var e = require$$0;
231
+ function h(a, b) {
232
+ return a === b && (0 !== a || 1 / a === 1 / b) || a !== a && b !== b;
85
233
  }
86
- values() {
87
- return [...this.cache.values()].map((entry) => {
88
- var _a;
89
- return entry.ref ?? ((_a = entry.weakRef) == null ? void 0 : _a.deref());
90
- }).filter((value) => !!value);
234
+ var k = "function" === typeof Object.is ? Object.is : h, l = e.useState, m = e.useEffect, n = e.useLayoutEffect, p = e.useDebugValue;
235
+ function q(a, b) {
236
+ var d = b(), f = l({ inst: { value: d, getSnapshot: b } }), c = f[0].inst, g = f[1];
237
+ n(function() {
238
+ c.value = d;
239
+ c.getSnapshot = b;
240
+ r(c) && g({ inst: c });
241
+ }, [a, d, b]);
242
+ m(function() {
243
+ r(c) && g({ inst: c });
244
+ return a(function() {
245
+ r(c) && g({ inst: c });
246
+ });
247
+ }, [a]);
248
+ p(d);
249
+ return d;
91
250
  }
92
- stop() {
93
- if (this.interval) {
94
- clearInterval(this.interval);
251
+ function r(a) {
252
+ var b = a.getSnapshot;
253
+ a = a.value;
254
+ try {
255
+ var d = b();
256
+ return !k(a, d);
257
+ } catch (f) {
258
+ return true;
95
259
  }
96
260
  }
97
- stats() {
98
- return {
99
- count: this.cache.size,
100
- withRef: [...this.cache.values()].filter((x) => !!x.ref).length,
101
- withWeakRef: [...this.cache.values()].filter((x) => {
102
- var _a;
103
- return !!((_a = x.weakRef) == null ? void 0 : _a.deref());
104
- }).length
105
- };
106
- }
107
- now() {
108
- return performance.now();
261
+ function t(a, b) {
262
+ return b();
109
263
  }
264
+ var u = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? t : q;
265
+ useSyncExternalStoreShim_production_min.useSyncExternalStore = void 0 !== e.useSyncExternalStore ? e.useSyncExternalStore : u;
266
+ return useSyncExternalStoreShim_production_min;
110
267
  }
111
- class PromiseWithState extends Promise {
112
- constructor(value, state = { status: "pending" }) {
113
- super((resolve) => resolve(value));
114
- this.state = state;
115
- value.then((value2) => {
116
- this.state = { status: "value", value: value2 };
117
- }).catch((error) => {
118
- this.state = { status: "error", error };
119
- });
120
- }
121
- static resolve(value) {
122
- return new PromiseWithState(Promise.resolve(value), {
123
- status: "value",
124
- value
125
- });
126
- }
127
- static reject(error) {
128
- return new PromiseWithState(Promise.reject(error), { status: "error", error });
268
+ var hasRequiredShim;
269
+ function requireShim() {
270
+ if (hasRequiredShim)
271
+ return shim.exports;
272
+ hasRequiredShim = 1;
273
+ if (process.env.NODE_ENV === "production") {
274
+ shim.exports = requireUseSyncExternalStoreShim_production_min();
275
+ } else {
276
+ shim.exports = requireUseSyncExternalStoreShim_development();
129
277
  }
278
+ return shim.exports;
130
279
  }
131
- class Cache extends store.Store {
132
- constructor(getter, options = {}, derivedFromCache, _call) {
133
- super(
134
- function() {
135
- let result = getter.apply(this);
136
- if (result instanceof Function) {
137
- result = result(this);
280
+ /**
281
+ * @license React
282
+ * use-sync-external-store-shim/with-selector.development.js
283
+ *
284
+ * Copyright (c) Facebook, Inc. and its affiliates.
285
+ *
286
+ * This source code is licensed under the MIT license found in the
287
+ * LICENSE file in the root directory of this source tree.
288
+ */
289
+ var hasRequiredWithSelector_development;
290
+ function requireWithSelector_development() {
291
+ if (hasRequiredWithSelector_development)
292
+ return withSelector_development;
293
+ hasRequiredWithSelector_development = 1;
294
+ if (process.env.NODE_ENV !== "production") {
295
+ (function() {
296
+ if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart === "function") {
297
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
298
+ }
299
+ var React = require$$0;
300
+ var shim2 = requireShim();
301
+ function is(x, y) {
302
+ return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y;
303
+ }
304
+ var objectIs = typeof Object.is === "function" ? Object.is : is;
305
+ var useSyncExternalStore = shim2.useSyncExternalStore;
306
+ var useRef = React.useRef, useEffect = React.useEffect, useMemo = React.useMemo, useDebugValue = React.useDebugValue;
307
+ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
308
+ var instRef = useRef(null);
309
+ var inst;
310
+ if (instRef.current === null) {
311
+ inst = {
312
+ hasValue: false,
313
+ value: null
314
+ };
315
+ instRef.current = inst;
316
+ } else {
317
+ inst = instRef.current;
138
318
  }
139
- return result;
140
- },
141
- options,
142
- void 0,
143
- _call
144
- );
145
- this.options = options;
146
- this.derivedFromCache = derivedFromCache;
147
- this.state = store.createStore({
148
- status: "pending",
149
- isStale: true,
150
- isUpdating: false
151
- });
152
- store.autobind(Cache);
153
- this.calculationHelper.options.onInvalidate = () => this.invalidate({ invalidateDependencies: false });
154
- this.watchPromise();
155
- this.watchFocus();
156
- }
157
- get({ update = "whenStale", backgroundUpdate = false } = {}) {
158
- var _a;
159
- const promise = (_a = this._value) == null ? void 0 : _a.v;
160
- const stalePromise = this.stalePromise;
161
- if (update === "whenMissing" && !promise && !stalePromise || update === "whenStale" && !promise || update === "force") {
162
- this.calculationHelper.execute();
163
- if (!promise && !stalePromise || !backgroundUpdate) {
164
- return super.get();
319
+ var _useMemo = useMemo(function() {
320
+ var hasMemo = false;
321
+ var memoizedSnapshot;
322
+ var memoizedSelection;
323
+ var memoizedSelector = function(nextSnapshot) {
324
+ if (!hasMemo) {
325
+ hasMemo = true;
326
+ memoizedSnapshot = nextSnapshot;
327
+ var _nextSelection = selector(nextSnapshot);
328
+ if (isEqual !== void 0) {
329
+ if (inst.hasValue) {
330
+ var currentSelection = inst.value;
331
+ if (isEqual(currentSelection, _nextSelection)) {
332
+ memoizedSelection = currentSelection;
333
+ return currentSelection;
334
+ }
335
+ }
336
+ }
337
+ memoizedSelection = _nextSelection;
338
+ return _nextSelection;
339
+ }
340
+ var prevSnapshot = memoizedSnapshot;
341
+ var prevSelection = memoizedSelection;
342
+ if (objectIs(prevSnapshot, nextSnapshot)) {
343
+ return prevSelection;
344
+ }
345
+ var nextSelection = selector(nextSnapshot);
346
+ if (isEqual !== void 0 && isEqual(prevSelection, nextSelection)) {
347
+ return prevSelection;
348
+ }
349
+ memoizedSnapshot = nextSnapshot;
350
+ memoizedSelection = nextSelection;
351
+ return nextSelection;
352
+ };
353
+ var maybeGetServerSnapshot = getServerSnapshot === void 0 ? null : getServerSnapshot;
354
+ var getSnapshotWithSelector = function() {
355
+ return memoizedSelector(getSnapshot());
356
+ };
357
+ var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? void 0 : function() {
358
+ return memoizedSelector(maybeGetServerSnapshot());
359
+ };
360
+ return [getSnapshotWithSelector, getServerSnapshotWithSelector];
361
+ }, [getSnapshot, getServerSnapshot, selector, isEqual]), getSelection = _useMemo[0], getServerSelection = _useMemo[1];
362
+ var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
363
+ useEffect(function() {
364
+ inst.hasValue = true;
365
+ inst.value = value;
366
+ }, [value]);
367
+ useDebugValue(value);
368
+ return value;
165
369
  }
166
- }
167
- if (!promise || stalePromise && backgroundUpdate) {
168
- return stalePromise;
169
- }
170
- return promise;
171
- }
172
- updateValue(value) {
173
- this.set(PromiseWithState.resolve(value));
370
+ withSelector_development.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
371
+ if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== "undefined" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop === "function") {
372
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
373
+ }
374
+ })();
174
375
  }
175
- updateError(error) {
176
- this.set(PromiseWithState.reject(error));
376
+ return withSelector_development;
377
+ }
378
+ var withSelector_production_min = {};
379
+ /**
380
+ * @license React
381
+ * use-sync-external-store-shim/with-selector.production.min.js
382
+ *
383
+ * Copyright (c) Facebook, Inc. and its affiliates.
384
+ *
385
+ * This source code is licensed under the MIT license found in the
386
+ * LICENSE file in the root directory of this source tree.
387
+ */
388
+ var hasRequiredWithSelector_production_min;
389
+ function requireWithSelector_production_min() {
390
+ if (hasRequiredWithSelector_production_min)
391
+ return withSelector_production_min;
392
+ hasRequiredWithSelector_production_min = 1;
393
+ var h = require$$0, n = requireShim();
394
+ function p(a, b) {
395
+ return a === b && (0 !== a || 1 / a === 1 / b) || a !== a && b !== b;
177
396
  }
178
- invalidate({ invalidateDependencies = true } = {}) {
397
+ var q = "function" === typeof Object.is ? Object.is : p, r = n.useSyncExternalStore, t = h.useRef, u = h.useEffect, v = h.useMemo, w = h.useDebugValue;
398
+ withSelector_production_min.useSyncExternalStoreWithSelector = function(a, b, e, l, g) {
399
+ var c = t(null);
400
+ if (null === c.current) {
401
+ var f = { hasValue: false, value: null };
402
+ c.current = f;
403
+ } else
404
+ f = c.current;
405
+ c = v(function() {
406
+ function a2(a3) {
407
+ if (!c2) {
408
+ c2 = true;
409
+ d2 = a3;
410
+ a3 = l(a3);
411
+ if (void 0 !== g && f.hasValue) {
412
+ var b2 = f.value;
413
+ if (g(b2, a3))
414
+ return k = b2;
415
+ }
416
+ return k = a3;
417
+ }
418
+ b2 = k;
419
+ if (q(d2, a3))
420
+ return b2;
421
+ var e2 = l(a3);
422
+ if (void 0 !== g && g(b2, e2))
423
+ return b2;
424
+ d2 = a3;
425
+ return k = e2;
426
+ }
427
+ var c2 = false, d2, k, m = void 0 === e ? null : e;
428
+ return [function() {
429
+ return a2(b());
430
+ }, null === m ? void 0 : function() {
431
+ return a2(m());
432
+ }];
433
+ }, [b, e, l, g]);
434
+ var d = r(a, c[0], c[1]);
435
+ u(function() {
436
+ f.hasValue = true;
437
+ f.value = d;
438
+ }, [d]);
439
+ w(d);
440
+ return d;
441
+ };
442
+ return withSelector_production_min;
443
+ }
444
+ if (process.env.NODE_ENV === "production") {
445
+ withSelector.exports = requireWithSelector_production_min();
446
+ } else {
447
+ withSelector.exports = requireWithSelector_development();
448
+ }
449
+ var withSelectorExports = withSelector.exports;
450
+ function useStore(store$1, argument1, argument2) {
451
+ const selector = store.makeSelector(
452
+ typeof argument1 === "function" || typeof argument1 === "string" ? argument1 : void 0
453
+ );
454
+ const {
455
+ disableTrackingProxy = true,
456
+ equals = store.deepEqual,
457
+ withViewTransition,
458
+ ...options
459
+ } = typeof argument1 === "object" ? argument1 : argument2 ?? {};
460
+ const lastEqualsRef = require$$0.useRef();
461
+ const { rootStore, mappingSelector } = require$$0.useMemo(() => {
179
462
  var _a;
180
- const { clearOnInvalidate = createCache.defaultOptions.clearOnInvalidate } = this.options;
181
- if (clearOnInvalidate) {
182
- return this.clear({ invalidateDependencies });
183
- }
184
- if (invalidateDependencies) {
185
- this.calculationHelper.invalidateDependencies();
186
- }
187
- const { status, isStale, isUpdating } = this.state.get();
188
- if (status !== "pending" && !isStale && !isUpdating) {
189
- this.stalePromise = (_a = this._value) == null ? void 0 : _a.v;
190
- }
191
- this.state.set((state) => ({
192
- ...state,
193
- isStale: true,
194
- isUpdating: false
195
- }));
196
- this.calculationHelper.stop();
197
- super.reset();
198
- }
199
- clear({ invalidateDependencies = true } = {}) {
200
- if (invalidateDependencies) {
201
- this.calculationHelper.invalidateDependencies();
202
- }
203
- this.state.set({
204
- status: "pending",
205
- isStale: true,
206
- isUpdating: false
207
- });
208
- delete this.stalePromise;
209
- this.calculationHelper.stop();
210
- super.reset();
211
- }
212
- mapValue(_selector) {
213
- const selector = store.makeSelector(_selector);
214
- const derivedFromCache = {
215
- cache: this.derivedFromCache ? this.derivedFromCache.cache : this,
216
- selectors: this.derivedFromCache ? [...this.derivedFromCache.selectors, _selector] : [_selector]
217
- };
218
- const that = this;
219
- return new Cache(
220
- async function() {
221
- const value = await this.use(that);
222
- return selector(value);
223
- },
224
- {},
225
- derivedFromCache
226
- );
227
- }
228
- watchPromise() {
229
- this.subscribe(
230
- async (promise) => {
231
- var _a, _b;
232
- if (promise instanceof PromiseWithState) {
233
- this.state.set({
234
- ...promise.state,
235
- isStale: false,
236
- isUpdating: false
237
- });
238
- delete this.stalePromise;
239
- this.setTimers();
240
- return;
463
+ const rootStore2 = ((_a = store$1.derivedFrom) == null ? void 0 : _a.store) ?? store$1;
464
+ let mappingSelector2 = (x) => x;
465
+ if (store$1.derivedFrom) {
466
+ mappingSelector2 = (value2) => {
467
+ for (const s of store$1.derivedFrom.selectors) {
468
+ value2 = store.makeSelector(s)(value2);
241
469
  }
242
- this.state.set((state) => ({
243
- ...state,
244
- isUpdating: true
245
- }));
246
- this.setTimers();
247
- try {
248
- const value = await promise;
249
- if (promise !== ((_a = this._value) == null ? void 0 : _a.v)) {
470
+ return value2;
471
+ };
472
+ }
473
+ return { rootStore: rootStore2, mappingSelector: mappingSelector2 };
474
+ }, [store$1]);
475
+ const subOptions = { ...options, runNow: false, passive: false };
476
+ const subscribe = require$$0.useCallback(
477
+ (listener) => {
478
+ let _listener = listener;
479
+ if (withViewTransition && document.startViewTransition) {
480
+ let lastObservedValue;
481
+ _listener = (value2) => {
482
+ const observedValue = withViewTransition instanceof Function ? withViewTransition(value2) : value2;
483
+ if (equals(lastObservedValue, observedValue)) {
484
+ listener();
250
485
  return;
251
486
  }
252
- this.state.set({
253
- status: "value",
254
- value,
255
- isStale: false,
256
- isUpdating: false
487
+ lastObservedValue = observedValue;
488
+ let hasChanges = false;
489
+ const mutationObserver = new MutationObserver(() => {
490
+ hasChanges = true;
491
+ mutationObserver.disconnect();
257
492
  });
258
- delete this.stalePromise;
259
- this.setTimers();
260
- } catch (error) {
261
- if (promise !== ((_b = this._value) == null ? void 0 : _b.v)) {
262
- return;
263
- }
264
- this.state.set({
265
- status: "error",
266
- error,
267
- isStale: false,
268
- isUpdating: false
493
+ mutationObserver.observe(document.body, { childList: true, subtree: true });
494
+ document.startViewTransition(() => {
495
+ listener();
496
+ if (!hasChanges) {
497
+ throw new Error("no change");
498
+ }
269
499
  });
270
- delete this.stalePromise;
271
- this.setTimers();
272
- }
273
- },
274
- { passive: true }
275
- );
276
- }
277
- setTimers() {
278
- if (this.invalidationTimer) {
279
- clearTimeout(this.invalidationTimer);
280
- }
281
- this.invalidationTimer = void 0;
282
- const state = this.state.get();
283
- let { invalidateAfter = createCache.defaultOptions.invalidateAfter } = this.options;
284
- const ref = new WeakRef(this);
285
- if (state.status === "pending") {
286
- return;
287
- }
288
- if (invalidateAfter instanceof Function) {
289
- invalidateAfter = invalidateAfter(state);
290
- }
291
- if (invalidateAfter !== null && invalidateAfter !== void 0) {
292
- this.invalidationTimer = setTimeout(
293
- () => {
294
- var _a;
295
- return (_a = ref == null ? void 0 : ref.deref()) == null ? void 0 : _a.invalidate();
296
- },
297
- store.calcDuration(invalidateAfter)
298
- );
299
- }
300
- }
301
- watchFocus() {
302
- const { invalidateOnWindowFocus = createCache.defaultOptions.invalidateOnWindowFocus } = this.options;
303
- if (!invalidateOnWindowFocus || typeof document === "undefined" || typeof document.addEventListener === "undefined") {
304
- return;
305
- }
306
- const ref = new WeakRef(this);
307
- const onFocus = () => {
308
- const that = ref == null ? void 0 : ref.deref();
309
- if (!that) {
310
- document.removeEventListener("visibilitychange", onFocus);
311
- return;
312
- }
313
- if (!document.hidden) {
314
- that.invalidate();
315
- }
316
- };
317
- document.addEventListener("visibilitychange", onFocus);
318
- }
319
- }
320
- function create(cacheFunction, options) {
321
- const { clearUnusedAfter = createCache.defaultOptions.clearUnusedAfter, resourceGroup } = options ?? {};
322
- let baseInstance;
323
- const instanceCache = new InstanceCache(
324
- (...args) => {
325
- if (args.length === 0 && baseInstance) {
326
- return baseInstance;
500
+ };
327
501
  }
328
- return new Cache(function() {
329
- return cacheFunction.apply(this, args);
330
- }, options);
502
+ return rootStore.subscribe(_listener, subOptions);
331
503
  },
332
- clearUnusedAfter ? store.calcDuration(clearUnusedAfter) : void 0
504
+ [rootStore, hash.hash(subOptions)]
333
505
  );
334
- const get = (...args) => {
335
- return instanceCache.get(...args);
336
- };
337
- const invalidateAll = () => {
338
- for (const instance of instanceCache.values()) {
339
- instance.invalidate();
340
- }
341
- };
342
- const clearAll = () => {
343
- for (const instance of instanceCache.values()) {
344
- instance.clear();
345
- }
346
- };
347
- baseInstance = Object.assign(
348
- new Cache(
349
- function() {
350
- return cacheFunction.apply(this);
351
- },
352
- options,
353
- void 0,
354
- get
355
- ),
356
- {
357
- invalidateAll,
358
- clearAll
506
+ let value = withSelectorExports.useSyncExternalStoreWithSelector(
507
+ //
508
+ subscribe,
509
+ rootStore.get,
510
+ void 0,
511
+ (x) => selector(mappingSelector(x)),
512
+ (_v, newValue) => {
513
+ var _a;
514
+ return ((_a = lastEqualsRef.current) == null ? void 0 : _a.call(lastEqualsRef, newValue)) ?? false;
359
515
  }
360
516
  );
361
- const groups = Array.isArray(resourceGroup) ? resourceGroup : resourceGroup ? [resourceGroup] : [];
362
- for (const group of groups.concat(allResources)) {
363
- group.add(baseInstance);
517
+ let lastEquals = (newValue) => equals(newValue, value);
518
+ let revoke;
519
+ if (!disableTrackingProxy) {
520
+ [value, lastEquals, revoke] = trackingProxy(value, equals);
364
521
  }
365
- get(...[]);
366
- return baseInstance;
522
+ require$$0.useLayoutEffect(() => {
523
+ lastEqualsRef.current = lastEquals;
524
+ revoke == null ? void 0 : revoke();
525
+ });
526
+ require$$0.useDebugValue(value);
527
+ return value;
367
528
  }
368
- const createCache = /* @__PURE__ */ Object.assign(create, {
369
- defaultOptions: {
370
- invalidateOnWindowFocus: true,
371
- invalidateOnActivation: true,
372
- clearUnusedAfter: { days: 1 }
529
+ function useProp(store2, argument1, argument2, argument3) {
530
+ const selector = typeof argument1 === "function" || typeof argument1 === "string" ? argument1 : void 0;
531
+ const updater = typeof argument2 === "function" ? argument2 : void 0;
532
+ const options = typeof argument1 === "object" ? argument1 : typeof argument2 === "object" ? argument2 : argument3;
533
+ if (selector) {
534
+ store2 = store2.map(selector, updater);
373
535
  }
374
- });
375
- class Scope {
376
- constructor(defaultValue) {
377
- this.defaultValue = defaultValue;
378
- store.autobind(Scope);
536
+ const value = useStore(store2, options);
537
+ return [value, store2.set];
538
+ }
539
+ function boundUseStore(...args) {
540
+ return useStore(this, ...args);
541
+ }
542
+ function boundUseProp(...args) {
543
+ return useProp(this, ...args);
544
+ }
545
+ const reactMethods = {
546
+ useStore: boundUseStore,
547
+ useProp: boundUseProp
548
+ };
549
+ function useCache(cache, { passive, updateOnMount, withViewTransition, ...options } = {}) {
550
+ if (withViewTransition === true) {
551
+ withViewTransition = (state) => state.value;
379
552
  }
553
+ const mappedState = require$$0.useMemo(() => {
554
+ var _a;
555
+ const rootCache = ((_a = cache.derivedFromCache) == null ? void 0 : _a.cache) ?? cache;
556
+ let selector = (x) => x;
557
+ if (cache.derivedFromCache) {
558
+ selector = (value) => {
559
+ for (const s of cache.derivedFromCache.selectors) {
560
+ value = store.makeSelector(s)(value);
561
+ }
562
+ return value;
563
+ };
564
+ }
565
+ return rootCache.state.map((state) => {
566
+ const value = state.status === "value" ? selector(state.value) : void 0;
567
+ return Object.assign(
568
+ [value, state.error, state.isUpdating, state.isStale],
569
+ { ...state, value }
570
+ );
571
+ });
572
+ }, [cache]);
573
+ require$$0.useEffect(() => !passive ? cache.subscribe(() => void 0) : void 0, [cache, passive]);
574
+ require$$0.useEffect(() => {
575
+ if (updateOnMount) {
576
+ cache.invalidate();
577
+ }
578
+ }, []);
579
+ return useStore(mappedState, { ...options, withViewTransition });
580
+ }
581
+ function getScopeContext(scope) {
582
+ scope.context ?? (scope.context = require$$0.createContext(store.createStore(scope.defaultValue)));
583
+ return scope.context;
584
+ }
585
+ function ScopeProvider({ scope, store: inputStore, children }) {
586
+ const context = getScopeContext(scope);
587
+ const currentStore = require$$0.useMemo(
588
+ () => inputStore ?? store.createStore(scope.defaultValue),
589
+ [scope, inputStore]
590
+ );
591
+ return /* @__PURE__ */ jsxRuntime.jsx(context.Provider, { value: currentStore, children });
592
+ }
593
+ function useScope(scope) {
594
+ const context = getScopeContext(scope);
595
+ return require$$0.useContext(context);
596
+ }
597
+ function useScopeStore(scope, options) {
598
+ const store2 = useScope(scope);
599
+ return useStore(store2, options);
380
600
  }
381
- function createScope(defaultValue) {
382
- return new Scope(defaultValue);
601
+ function useScopeProp(scope, options) {
602
+ const store2 = useScope(scope);
603
+ return useProp(store2, options);
383
604
  }
384
- exports.Cache = Cache;
385
- exports.InstanceCache = InstanceCache;
386
- exports.ResourceGroup = ResourceGroup;
387
- exports.Scope = Scope;
388
- exports.allResources = allResources;
389
- exports.createCache = createCache;
390
- exports.createResourceGroup = createResourceGroup;
391
- exports.createScope = createScope;
605
+ exports.ScopeProvider = ScopeProvider;
606
+ exports.reactMethods = reactMethods;
607
+ exports.useCache = useCache;
608
+ exports.useProp = useProp;
609
+ exports.useScope = useScope;
610
+ exports.useScopeProp = useScopeProp;
611
+ exports.useScopeStore = useScopeStore;
612
+ exports.useStore = useStore;
392
613
  //# sourceMappingURL=scope2.cjs.map