cross-state 0.25.2 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/scope.mjs CHANGED
@@ -1,63 +1,85 @@
1
- import require$$0, { useRef, useMemo, useCallback, useLayoutEffect, useDebugValue, useEffect, useContext, createContext } from "react";
2
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
3
  import { jsx } from "react/jsx-runtime";
4
4
  import { h as hash } from "./hash.mjs";
5
- var withSelector = { exports: {} };
6
- var withSelector_production_min = {};
7
- var shim = { exports: {} };
8
- var useSyncExternalStoreShim_production_min = {};
9
- /**
10
- * @license React
11
- * use-sync-external-store-shim.production.min.js
12
- *
13
- * Copyright (c) Facebook, Inc. and its affiliates.
14
- *
15
- * This source code is licensed under the MIT license found in the
16
- * LICENSE file in the root directory of this source tree.
17
- */
18
- var hasRequiredUseSyncExternalStoreShim_production_min;
19
- function requireUseSyncExternalStoreShim_production_min() {
20
- if (hasRequiredUseSyncExternalStoreShim_production_min)
21
- return useSyncExternalStoreShim_production_min;
22
- hasRequiredUseSyncExternalStoreShim_production_min = 1;
23
- var e = require$$0;
24
- function h(a, b) {
25
- return a === b && (0 !== a || 1 / a === 1 / b) || a !== a && b !== b;
26
- }
27
- var k = "function" === typeof Object.is ? Object.is : h, l = e.useState, m = e.useEffect, n = e.useLayoutEffect, p = e.useDebugValue;
28
- function q(a, b) {
29
- var d = b(), f = l({ inst: { value: d, getSnapshot: b } }), c = f[0].inst, g = f[1];
30
- n(function() {
31
- c.value = d;
32
- c.getSnapshot = b;
33
- r(c) && g({ inst: c });
34
- }, [a, d, b]);
35
- m(function() {
36
- r(c) && g({ inst: c });
37
- return a(function() {
38
- r(c) && g({ inst: c });
39
- });
40
- }, [a]);
41
- p(d);
42
- return d;
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)];
43
12
  }
44
- function r(a) {
45
- var b = a.getSnapshot;
46
- a = a.value;
47
- try {
48
- var d = b();
49
- return !k(a, d);
50
- } catch (f) {
51
- return true;
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;
22
+ }
23
+ return equals2(function_(otherValue, ...args));
24
+ });
25
+ if (revoke) {
26
+ revokations.push(revoke);
52
27
  }
28
+ return proxiedValue;
53
29
  }
54
- function t(a, b) {
55
- return b();
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;
56
36
  }
57
- var u = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? t : q;
58
- useSyncExternalStoreShim_production_min.useSyncExternalStore = void 0 !== e.useSyncExternalStore ? e.useSyncExternalStore : u;
59
- return useSyncExternalStoreShim_production_min;
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];
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);
69
+ }
70
+ });
71
+ return [
72
+ proxy,
73
+ (other) => !!other && deps.every((equals2) => equals2(other)),
74
+ () => {
75
+ revoked = true;
76
+ revokations.forEach((revoke) => revoke());
77
+ }
78
+ ];
60
79
  }
80
+ var withSelector = { exports: {} };
81
+ var withSelector_development = {};
82
+ var shim = { exports: {} };
61
83
  var useSyncExternalStoreShim_development = {};
62
84
  /**
63
85
  * @license React
@@ -189,84 +211,71 @@ function requireUseSyncExternalStoreShim_development() {
189
211
  }
190
212
  return useSyncExternalStoreShim_development;
191
213
  }
192
- var hasRequiredShim;
193
- function requireShim() {
194
- if (hasRequiredShim)
195
- return shim.exports;
196
- hasRequiredShim = 1;
197
- if (process.env.NODE_ENV === "production") {
198
- shim.exports = requireUseSyncExternalStoreShim_production_min();
199
- } else {
200
- shim.exports = requireUseSyncExternalStoreShim_development();
201
- }
202
- return shim.exports;
203
- }
214
+ var useSyncExternalStoreShim_production_min = {};
204
215
  /**
205
216
  * @license React
206
- * use-sync-external-store-shim/with-selector.production.min.js
217
+ * use-sync-external-store-shim.production.min.js
207
218
  *
208
219
  * Copyright (c) Facebook, Inc. and its affiliates.
209
220
  *
210
221
  * This source code is licensed under the MIT license found in the
211
222
  * LICENSE file in the root directory of this source tree.
212
223
  */
213
- var hasRequiredWithSelector_production_min;
214
- function requireWithSelector_production_min() {
215
- if (hasRequiredWithSelector_production_min)
216
- return withSelector_production_min;
217
- hasRequiredWithSelector_production_min = 1;
218
- var h = require$$0, n = requireShim();
219
- function p(a, b) {
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) {
220
231
  return a === b && (0 !== a || 1 / a === 1 / b) || a !== a && b !== b;
221
232
  }
222
- var q = "function" === typeof Object.is ? Object.is : p, r = n.useSyncExternalStore, t = h.useRef, u = h.useEffect, v = h.useMemo, w = h.useDebugValue;
223
- withSelector_production_min.useSyncExternalStoreWithSelector = function(a, b, e, l, g) {
224
- var c = t(null);
225
- if (null === c.current) {
226
- var f = { hasValue: false, value: null };
227
- c.current = f;
228
- } else
229
- f = c.current;
230
- c = v(function() {
231
- function a2(a3) {
232
- if (!c2) {
233
- c2 = true;
234
- d2 = a3;
235
- a3 = l(a3);
236
- if (void 0 !== g && f.hasValue) {
237
- var b2 = f.value;
238
- if (g(b2, a3))
239
- return k = b2;
240
- }
241
- return k = a3;
242
- }
243
- b2 = k;
244
- if (q(d2, a3))
245
- return b2;
246
- var e2 = l(a3);
247
- if (void 0 !== g && g(b2, e2))
248
- return b2;
249
- d2 = a3;
250
- return k = e2;
251
- }
252
- var c2 = false, d2, k, m = void 0 === e ? null : e;
253
- return [function() {
254
- return a2(b());
255
- }, null === m ? void 0 : function() {
256
- return a2(m());
257
- }];
258
- }, [b, e, l, g]);
259
- var d = r(a, c[0], c[1]);
260
- u(function() {
261
- f.hasValue = true;
262
- f.value = d;
263
- }, [d]);
264
- w(d);
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);
265
248
  return d;
266
- };
267
- return withSelector_production_min;
249
+ }
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;
258
+ }
259
+ }
260
+ function t(a, b) {
261
+ return b();
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;
266
+ }
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();
276
+ }
277
+ return shim.exports;
268
278
  }
269
- var withSelector_development = {};
270
279
  /**
271
280
  * @license React
272
281
  * use-sync-external-store-shim/with-selector.development.js
@@ -365,87 +374,78 @@ function requireWithSelector_development() {
365
374
  }
366
375
  return withSelector_development;
367
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;
395
+ }
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
+ }
368
443
  if (process.env.NODE_ENV === "production") {
369
444
  withSelector.exports = requireWithSelector_production_min();
370
445
  } else {
371
446
  withSelector.exports = requireWithSelector_development();
372
447
  }
373
448
  var withSelectorExports = withSelector.exports;
374
- const unwrapProxySymbol = /* @__PURE__ */ Symbol("unwrapProxy");
375
- function isPlainObject(value) {
376
- return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
377
- }
378
- function trackingProxy(value, equals = deepEqual) {
379
- if (!isPlainObject(value) && !Array.isArray(value)) {
380
- return [value, (other) => equals(value, other)];
381
- }
382
- value = value[unwrapProxySymbol] ?? value;
383
- const deps = new Array();
384
- const revokations = new Array();
385
- let revoked = false;
386
- function trackComplexProp(function_, ...args) {
387
- const [proxiedValue, equals2, revoke] = trackingProxy(function_(value, ...args));
388
- deps.push((otherValue) => {
389
- if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {
390
- return false;
391
- }
392
- return equals2(function_(otherValue, ...args));
393
- });
394
- if (revoke) {
395
- revokations.push(revoke);
396
- }
397
- return proxiedValue;
398
- }
399
- function trackSimpleProp(function_, ...args) {
400
- const calculatedValue = function_(value, ...args);
401
- deps.push((otherValue) => {
402
- return function_(otherValue, ...args) === calculatedValue;
403
- });
404
- return calculatedValue;
405
- }
406
- const proxy = new Proxy(value, {
407
- get(target, p, receiver) {
408
- if (p === unwrapProxySymbol) {
409
- return value;
410
- }
411
- if (revoked) {
412
- return target[p];
413
- }
414
- const { writable, configurable } = Object.getOwnPropertyDescriptor(target, p) ?? {};
415
- if (writable === false && configurable === false) {
416
- return target[p];
417
- }
418
- return trackComplexProp(Reflect.get, p, receiver);
419
- },
420
- getOwnPropertyDescriptor(target, p) {
421
- const { writable, configurable } = Object.getOwnPropertyDescriptor(target, p) ?? {};
422
- if (writable === false && configurable === false) {
423
- return Reflect.getOwnPropertyDescriptor(target, p);
424
- }
425
- return trackComplexProp(Reflect.getOwnPropertyDescriptor, p);
426
- },
427
- ownKeys() {
428
- return trackComplexProp(Reflect.ownKeys);
429
- },
430
- getPrototypeOf() {
431
- return trackSimpleProp(Reflect.getPrototypeOf);
432
- },
433
- has(_target, p) {
434
- return trackSimpleProp(Reflect.has, p);
435
- },
436
- isExtensible() {
437
- return trackSimpleProp(Reflect.isExtensible);
438
- }
439
- });
440
- return [
441
- proxy,
442
- (other) => !!other && deps.every((equals2) => equals2(other)),
443
- () => {
444
- revoked = true;
445
- revokations.forEach((revoke) => revoke());
446
- }
447
- ];
448
- }
449
449
  function useStore(store, argument1, argument2) {
450
450
  const selector = makeSelector(
451
451
  typeof argument1 === "function" || typeof argument1 === "string" ? argument1 : void 0
@@ -453,6 +453,7 @@ function useStore(store, argument1, argument2) {
453
453
  const {
454
454
  disableTrackingProxy = true,
455
455
  equals = deepEqual,
456
+ withViewTransition,
456
457
  ...options
457
458
  } = typeof argument1 === "object" ? argument1 : argument2 ?? {};
458
459
  const lastEqualsRef = useRef();
@@ -473,7 +474,31 @@ function useStore(store, argument1, argument2) {
473
474
  const subOptions = { ...options, runNow: false, passive: false };
474
475
  const subscribe = useCallback(
475
476
  (listener) => {
476
- return rootStore.subscribe(listener, subOptions);
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();
484
+ return;
485
+ }
486
+ lastObservedValue = observedValue;
487
+ let hasChanges = false;
488
+ const mutationObserver = new MutationObserver(() => {
489
+ hasChanges = true;
490
+ mutationObserver.disconnect();
491
+ });
492
+ mutationObserver.observe(document.body, { childList: true, subtree: true });
493
+ document.startViewTransition(() => {
494
+ listener();
495
+ if (!hasChanges) {
496
+ throw new Error("no change");
497
+ }
498
+ });
499
+ };
500
+ }
501
+ return rootStore.subscribe(_listener, subOptions);
477
502
  },
478
503
  [rootStore, hash(subOptions)]
479
504
  );
@@ -510,15 +535,20 @@ function useProp(store, argument1, argument2, argument3) {
510
535
  const value = useStore(store, options);
511
536
  return [value, store.set];
512
537
  }
538
+ function boundUseStore(...args) {
539
+ return useStore(this, ...args);
540
+ }
541
+ function boundUseProp(...args) {
542
+ return useProp(this, ...args);
543
+ }
513
544
  const reactMethods = {
514
- useStore(options) {
515
- return useStore(this, options);
516
- },
517
- useProp(options) {
518
- return useProp(this, options);
519
- }
545
+ useStore: boundUseStore,
546
+ useProp: boundUseProp
520
547
  };
521
- function useCache(cache, { passive, updateOnMount, ...options } = {}) {
548
+ function useCache(cache, { passive, updateOnMount, withViewTransition, ...options } = {}) {
549
+ if (withViewTransition === true) {
550
+ withViewTransition = (state) => state.value;
551
+ }
522
552
  const mappedState = useMemo(() => {
523
553
  var _a;
524
554
  const rootCache = ((_a = cache.derivedFromCache) == null ? void 0 : _a.cache) ?? cache;
@@ -545,7 +575,7 @@ function useCache(cache, { passive, updateOnMount, ...options } = {}) {
545
575
  cache.invalidate();
546
576
  }
547
577
  }, []);
548
- return useStore(mappedState, options);
578
+ return useStore(mappedState, { ...options, withViewTransition });
549
579
  }
550
580
  function getScopeContext(scope) {
551
581
  scope.context ?? (scope.context = createContext(createStore(scope.defaultValue)));