@mmstack/primitives 19.3.10 → 19.3.11

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.
@@ -2443,6 +2443,82 @@ function isOpaque(value) {
2443
2443
  value !== null &&
2444
2444
  value[OPAQUE] === true);
2445
2445
  }
2446
+ /**
2447
+ * @internal Runtime brand carrying a store node's lazily-built leaf probe. Exported (like
2448
+ * {@link OPAQUE}) only so the `{ readonly [LEAF]: () => boolean }` brand on the store types is
2449
+ * nameable in the emitted declarations — not part of the supported surface; use {@link isLeaf}.
2450
+ */
2451
+ const LEAF = Symbol('@mmstack/primitives::store/LEAF');
2452
+ /**
2453
+ * @internal Whether a value is a terminal leaf: a concrete non-record/non-array value always is;
2454
+ * `null`/`undefined` is a leaf only when vivification is disabled (with vivify on it can still
2455
+ * materialize a container, so it stays a descendable substore).
2456
+ */
2457
+ function isLeafValue(value, vivifyEnabled) {
2458
+ if (value == null)
2459
+ return !vivifyEnabled;
2460
+ if (isOpaque(value))
2461
+ return true; // opaque always wins — even arrays
2462
+ return !Array.isArray(value) && !isRecord(value);
2463
+ }
2464
+ /**
2465
+ * @internal Constant leaf probes for nodes whose leaf-ness is statically known, so the reactive
2466
+ * `computed` can be skipped entirely.
2467
+ */
2468
+ function alwaysTrue() {
2469
+ return true;
2470
+ }
2471
+ function alwaysFalse() {
2472
+ return false;
2473
+ }
2474
+ /**
2475
+ * @internal Attaches a lazy, memoized leaf probe to a store node. The probe (`() => boolean`)
2476
+ * closes over the node's value signal and its (stable) vivify setting, building the backing
2477
+ * `computed` on first call so leaf-ness tracks the live value reactively without taxing every
2478
+ * node access. Idempotent.
2479
+ */
2480
+ function markAsLeaf(sig, value, vivifyEnabled, noUnionLeaves) {
2481
+ if (typeof sig[LEAF] !== 'function') {
2482
+ let memo;
2483
+ const probe = () => {
2484
+ if (memo)
2485
+ return memo();
2486
+ const v = untracked(value);
2487
+ memo =
2488
+ isOpaque(v) || (v == null && !vivifyEnabled) || noUnionLeaves
2489
+ ? isLeafValue(v, vivifyEnabled)
2490
+ ? alwaysTrue
2491
+ : alwaysFalse
2492
+ : computed(() => isLeafValue(value(), vivifyEnabled));
2493
+ return memo();
2494
+ };
2495
+ Object.defineProperty(sig, LEAF, {
2496
+ value: probe,
2497
+ enumerable: false,
2498
+ configurable: true,
2499
+ });
2500
+ }
2501
+ return sig;
2502
+ }
2503
+ /**
2504
+ * Reports whether a store node is currently a **leaf** — a terminal value the store does not
2505
+ * descend into (a primitive, `Date`, `RegExp`, {@link opaque} object, class instance, or a
2506
+ * `null`/`undefined` hole when vivification is off) rather than a record/array substore.
2507
+ *
2508
+ * Leaf-ness reflects the node's **live** value: the probe is reactive and memoized, so calling
2509
+ * `isLeaf` inside a `computed`/`effect` re-evaluates when the node's shape changes.
2510
+ *
2511
+ * @internal Exposed for advanced/niche interop only — not part of the supported public surface
2512
+ * and may change without a major version bump.
2513
+ *
2514
+ * @example
2515
+ * const s = store({ name: 'Ada', address: { city: 'London' } });
2516
+ * isLeaf(s.name); // true
2517
+ * isLeaf(s.address); // false — a substore
2518
+ */
2519
+ function isLeaf(value) {
2520
+ return isStore(value) && value[LEAF]?.() === true;
2521
+ }
2446
2522
  const IS_STORE = Symbol('@mmstack/primitives::store/IS_STORE');
2447
2523
  const SCOPE_PARENT = Symbol('@mmstack/primitives::store/SCOPE_PARENT');
2448
2524
  /**
@@ -2505,7 +2581,7 @@ function hasOwnKey(value, key) {
2505
2581
  * @internal
2506
2582
  * Makes an array store
2507
2583
  */
2508
- function toArrayStore(source, injector, vivify) {
2584
+ function toArrayStore(source, injector, vivify, noUnionLeaves = false) {
2509
2585
  if (isStore(source))
2510
2586
  return source;
2511
2587
  const isMutableSource = isMutable(source);
@@ -2615,9 +2691,10 @@ function toArrayStore(source, injector, vivify) {
2615
2691
  });
2616
2692
  const childSample = untracked(computation);
2617
2693
  const childVivify = resolveVivify(childSample, vivify);
2618
- const proxy = Array.isArray(childSample)
2619
- ? toArrayStore(computation, injector, childVivify)
2620
- : toStore(computation, injector, childVivify);
2694
+ const proxy = Array.isArray(childSample) && !isOpaque(childSample)
2695
+ ? toArrayStore(computation, injector, childVivify, noUnionLeaves)
2696
+ : toStore(computation, injector, childVivify, noUnionLeaves);
2697
+ markAsLeaf(proxy, computation, childVivify !== false, noUnionLeaves);
2621
2698
  const ref = new WeakRef(proxy);
2622
2699
  storeCache.set(idx, ref);
2623
2700
  PROXY_CLEANUP.register(proxy, { target, prop: idx }, ref);
@@ -2634,7 +2711,7 @@ function toArrayStore(source, injector, vivify) {
2634
2711
  * const state = store({ user: { name: 'John' } });
2635
2712
  * const nameSignal = state.user.name; // WritableSignal<string>
2636
2713
  */
2637
- function toStore(source, injector, vivify = false) {
2714
+ function toStore(source, injector, vivify = false, noUnionLeaves = false) {
2638
2715
  if (isStore(source))
2639
2716
  return source;
2640
2717
  if (!injector)
@@ -2675,7 +2752,7 @@ function toStore(source, injector, vivify = false) {
2675
2752
  return () => {
2676
2753
  if (!isWritableSource)
2677
2754
  return s;
2678
- return untracked(() => toStore(source.asReadonly(), injector, vivify));
2755
+ return untracked(() => toStore(source.asReadonly(), injector, vivify, noUnionLeaves));
2679
2756
  };
2680
2757
  if (prop === 'extend')
2681
2758
  return (seed) => scopedStore(s, seed, isMutableSource
@@ -2728,9 +2805,10 @@ function toStore(source, injector, vivify = false) {
2728
2805
  });
2729
2806
  const childSample = untracked(computation);
2730
2807
  const childVivify = resolveVivify(childSample, vivify);
2731
- const proxy = Array.isArray(childSample)
2732
- ? toArrayStore(computation, injector, childVivify)
2733
- : toStore(computation, injector, childVivify);
2808
+ const proxy = Array.isArray(childSample) && !isOpaque(childSample)
2809
+ ? toArrayStore(computation, injector, childVivify, noUnionLeaves)
2810
+ : toStore(computation, injector, childVivify, noUnionLeaves);
2811
+ markAsLeaf(proxy, computation, childVivify !== false, noUnionLeaves);
2734
2812
  const ref = new WeakRef(proxy);
2735
2813
  storeCache.set(prop, ref);
2736
2814
  PROXY_CLEANUP.register(proxy, { target, prop }, ref);
@@ -2823,14 +2901,14 @@ function scopedStore(parent, seed, kind, injector) {
2823
2901
  * @see {@link toStore}
2824
2902
  */
2825
2903
  function store(value, opt) {
2826
- return toStore(signal(value, opt), opt?.injector, opt?.vivify ?? false);
2904
+ return toStore(signal(value, opt), opt?.injector, opt?.vivify ?? false, opt?.noUnionLeaves ?? false);
2827
2905
  }
2828
2906
  /**
2829
2907
  * Creates a MutableSignalStore from a value.
2830
2908
  * @see {@link toStore}
2831
2909
  */
2832
2910
  function mutableStore(value, opt) {
2833
- return toStore(mutable(value, opt), opt?.injector, opt?.vivify ?? false);
2911
+ return toStore(mutable(value, opt), opt?.injector, opt?.vivify ?? false, opt?.noUnionLeaves ?? false);
2834
2912
  }
2835
2913
 
2836
2914
  // Internal dummy store for server-side rendering
@@ -3309,5 +3387,5 @@ function withHistory(sourceOrValue, opt) {
3309
3387
  * Generated bundle index. Do not edit.
3310
3388
  */
3311
3389
 
3312
- export { batteryStatus, chunked, clipboard, combineWith, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, filterWith, focusWithin, geolocation, idle, indexArray, isDerivation, isMutable, isOpaque, isStore, keyArray, map, mapArray, mapObject, mediaQuery, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, opaque, orientation, pageVisibility, pairwise, pipeable, piped, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
3390
+ export { batteryStatus, chunked, clipboard, combineWith, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, filterWith, focusWithin, geolocation, idle, indexArray, isDerivation, isLeaf, isMutable, isOpaque, isStore, keyArray, map, mapArray, mapObject, mediaQuery, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, opaque, orientation, pageVisibility, pairwise, pipeable, piped, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
3313
3391
  //# sourceMappingURL=mmstack-primitives.mjs.map