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