@mmstack/primitives 20.5.7 → 20.5.8
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/README.md +52 -0
- package/fesm2022/mmstack-primitives.mjs +305 -54
- package/fesm2022/mmstack-primitives.mjs.map +1 -1
- package/index.d.ts +135 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,6 +69,14 @@ const upper = derived(user, {
|
|
|
69
69
|
|
|
70
70
|
When the source is a `MutableSignal`, the derived signal is also a `MutableSignal` — `derived(state, 'items').mutate(arr => { arr.push(...); return arr })` propagates correctly.
|
|
71
71
|
|
|
72
|
+
Pass `vivify` on the key/index form to create a missing container when writing through a `null`/`undefined` source — instead of throwing (mutable / array) or dropping the write. Choose `'object'`, `'array'`, `'auto'` (an array for index keys, an object otherwise), or a `() => container` factory; it defaults to off.
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const user = signal<{ name: string } | null>(null);
|
|
76
|
+
derived(user, 'name', { vivify: 'object' }).set('Ada');
|
|
77
|
+
// user() === { name: 'Ada' }
|
|
78
|
+
```
|
|
79
|
+
|
|
72
80
|
### `store` / `mutableStore`
|
|
73
81
|
|
|
74
82
|
Proxies an object (or signal of an object) into a tree of `WritableSignal`s — one per property, lazily created and cached via `WeakRef`. Arrays expose indices as signals plus a `.length` signal and `Symbol.iterator`. Mutability propagates: if the root is a `MutableSignal`, every child is too.
|
|
@@ -92,8 +100,52 @@ settings.notifications.mutate((n) => {
|
|
|
92
100
|
});
|
|
93
101
|
```
|
|
94
102
|
|
|
103
|
+
**Autovivification (opt-in).** By default, a write through a `null`/`undefined` path is dropped. Pass `vivify` to create the missing intermediate containers instead:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const form = store(
|
|
107
|
+
{ user: null as { address?: { city: string } } | null },
|
|
108
|
+
{ vivify: 'auto' },
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
form.user.address.city.set('NYC');
|
|
112
|
+
// form() === { user: { address: { city: 'NYC' } } }
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Each level's shape is resolved from what's known: a value that is currently an object/array re-creates as that same shape (resolved per path and cached, so it survives the value later being nulled), while genuinely-unknown levels follow your option — `'auto'` (an array for index keys, an object otherwise), `'object'`, `'array'`, or a `() => container` factory. `false` (the default) keeps writes through `null` as no-ops. Adding a key that simply wasn't present on an existing object always works and needs no `vivify`.
|
|
116
|
+
|
|
95
117
|
Top-level array support isn't exposed yet — use `indexArray` / `keyArray` for those.
|
|
96
118
|
|
|
119
|
+
### `extend` (scoped overlay)
|
|
120
|
+
|
|
121
|
+
`store.extend(seed)` (on any store kind) creates a **scoped overlay** — a child store that **shares** the parent's signals for inherited keys (the same `WritableSignal`: writes go through to the parent and parent changes flow down) while keeping the seed and any new keys in a **local layer** that never propagates upward. No diffing, no syncing — local keys simply aren't wired to the parent.
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const app = store({ user: { name: 'Alice' }, theme: 'dark' });
|
|
125
|
+
|
|
126
|
+
const scope = app.extend({ draft: '' }); // inherits user + theme, adds a local draft
|
|
127
|
+
|
|
128
|
+
scope.user === app.user; // true — the same signal (shared, two-way)
|
|
129
|
+
scope.user.name.set('Bob'); // writes through to the parent
|
|
130
|
+
scope.draft.set('hello'); // local only — `app` never gains `draft`
|
|
131
|
+
scope(); // { user: { name: 'Bob' }, theme: 'dark', draft: 'hello' }
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Resolution per key is **local → parent → local**: a seed key (or one set on the scope before it exists on the parent) is local and _shadows_ the parent — and keeps shadowing even if the parent later grows that key; a key that exists only on the parent writes through to it; a brand-new key lands locally. `scope()` is the merged view (local shadowing), and `Object.keys(scope)` / `key in scope` are the union of both layers. `extend` composes — `a.extend(x).extend(y)` chains parents.
|
|
135
|
+
|
|
136
|
+
The seed may also be a **signal** of the matching kind, so an existing (externally-owned, reactive) signal becomes the local layer:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const draft = signal({ title: '' });
|
|
140
|
+
const scope = app.extend(draft); // writes to scope.title flow out to `draft`, and back in
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
A few release notes:
|
|
144
|
+
|
|
145
|
+
- The local layer is a plain store (vivify off). Inherited paths vivify when the _parent_ was created with `vivify`; to autovivify local keys, seed with a vivify-enabled store — `app.extend(store(seed, { vivify: 'auto' }))`.
|
|
146
|
+
- Reserved names — `extend`, `asReadonlyStore`, and the signal methods (`set` / `update` / `mutate` / `inline` / `asReadonly`) — shadow same-named data keys, as on any store.
|
|
147
|
+
- `scope.asReadonlyStore()` returns a read-only **snapshot view** of the merge (reactive reads, no writes); it does not share sub-store identity.
|
|
148
|
+
|
|
97
149
|
### `toWritable`
|
|
98
150
|
|
|
99
151
|
Turn any read-only `Signal<T>` into a `WritableSignal<T>` by providing custom `set` / `update` implementations. Powers `derived` internally; use it directly when you have a `computed` you want to expose as writable.
|
|
@@ -401,37 +401,158 @@ function isMutable(value) {
|
|
|
401
401
|
return 'mutate' in value && typeof value.mutate === 'function';
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
+
/**
|
|
405
|
+
* @internal
|
|
406
|
+
* Type guard for an array-index-like property key: a non-empty string that parses to a finite
|
|
407
|
+
* number (e.g. `'0'`, `'42'`). Used to choose array-vs-object shape during autovivification and
|
|
408
|
+
* deep store proxying.
|
|
409
|
+
*/
|
|
410
|
+
function isIndexProp(prop) {
|
|
411
|
+
return typeof prop === 'string' && prop.trim() !== '' && !isNaN(+prop);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Container resolvers used by createVivify: each returns the current value when present and
|
|
415
|
+
// only creates a new container when it is null/undefined.
|
|
416
|
+
function identity(x) {
|
|
417
|
+
return x;
|
|
418
|
+
}
|
|
419
|
+
function createArray(cur) {
|
|
420
|
+
if (cur === null || cur === undefined)
|
|
421
|
+
return [];
|
|
422
|
+
return cur;
|
|
423
|
+
}
|
|
424
|
+
function createObject(cur) {
|
|
425
|
+
if (cur === null || cur === undefined)
|
|
426
|
+
return {};
|
|
427
|
+
return cur;
|
|
428
|
+
}
|
|
429
|
+
function createAuto(cur, key) {
|
|
430
|
+
if (cur === null || cur === undefined) {
|
|
431
|
+
return typeof key === 'number' || isIndexProp(key)
|
|
432
|
+
? []
|
|
433
|
+
: {};
|
|
434
|
+
}
|
|
435
|
+
return cur;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* @internal
|
|
439
|
+
* Resolves a {@link Vivify} option into a {@link VivifyFn}. The returned function leaves a
|
|
440
|
+
* present value untouched and only creates a new container — object, array, or factory result —
|
|
441
|
+
* when the current value is `null`/`undefined`.
|
|
442
|
+
*/
|
|
443
|
+
function createVivify(option) {
|
|
444
|
+
switch (option) {
|
|
445
|
+
case false:
|
|
446
|
+
return identity;
|
|
447
|
+
case 'array':
|
|
448
|
+
return createArray;
|
|
449
|
+
case 'object':
|
|
450
|
+
return createObject;
|
|
451
|
+
case 'auto':
|
|
452
|
+
case true:
|
|
453
|
+
return createAuto;
|
|
454
|
+
default:
|
|
455
|
+
return typeof option === 'function'
|
|
456
|
+
? (cur) => cur === null || cur === undefined ? option() : cur
|
|
457
|
+
: identity;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function createMutableArrayUpdater(source, index, vivifyFn) {
|
|
462
|
+
return (next) => source.mutate((cur) => {
|
|
463
|
+
const vivified = vivifyFn(cur, index);
|
|
464
|
+
if (vivified === null || vivified === undefined)
|
|
465
|
+
return vivified;
|
|
466
|
+
vivified[index] = next;
|
|
467
|
+
return vivified;
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
function createImmutableArrayUpdater(source, index, vivifyFn) {
|
|
471
|
+
return (next) => source.update((cur) => {
|
|
472
|
+
const vivified = vivifyFn(cur, index)?.slice();
|
|
473
|
+
if (vivified === null || vivified === undefined)
|
|
474
|
+
return vivified;
|
|
475
|
+
vivified[index] = next;
|
|
476
|
+
return vivified;
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
function createMutableObjectUpdater(source, key, vivifyFn) {
|
|
480
|
+
return (next) => source.mutate((cur) => {
|
|
481
|
+
const vivified = vivifyFn(cur, key);
|
|
482
|
+
if (vivified === null || vivified === undefined)
|
|
483
|
+
return vivified;
|
|
484
|
+
vivified[key] = next;
|
|
485
|
+
return vivified;
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
function createImmutableObjectUpdater(source, key, vivifyFn) {
|
|
489
|
+
return (next) => source.update((cur) => {
|
|
490
|
+
const vivified = vivifyFn(cur, key);
|
|
491
|
+
if (vivified === null || vivified === undefined)
|
|
492
|
+
return vivified;
|
|
493
|
+
return { ...vivified, [key]: next };
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
function createUpdater(source, key, vivify) {
|
|
497
|
+
const sample = untracked(source);
|
|
498
|
+
// fast path for when vivification is off
|
|
499
|
+
if (!vivify) {
|
|
500
|
+
if (Array.isArray(sample) && typeof key === 'number') {
|
|
501
|
+
const idx = key;
|
|
502
|
+
return isMutable(source)
|
|
503
|
+
? (next) => source.mutate((cur) => {
|
|
504
|
+
cur[idx] = next;
|
|
505
|
+
return cur;
|
|
506
|
+
})
|
|
507
|
+
: (next) => source.update((cur) => {
|
|
508
|
+
const copy = cur.slice();
|
|
509
|
+
copy[idx] = next;
|
|
510
|
+
return copy;
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
return isMutable(source)
|
|
514
|
+
? (next) => source.mutate((cur) => {
|
|
515
|
+
cur[key] = next;
|
|
516
|
+
return cur;
|
|
517
|
+
})
|
|
518
|
+
: (next) => source.update((cur) => ({
|
|
519
|
+
...cur,
|
|
520
|
+
[key]: next,
|
|
521
|
+
}));
|
|
522
|
+
}
|
|
523
|
+
const present = sample !== null && sample !== undefined;
|
|
524
|
+
const keyIsIndex = typeof key === 'number' || isIndexProp(key);
|
|
525
|
+
let vivifyOpt = vivify;
|
|
526
|
+
if (vivifyOpt === 'auto' || vivifyOpt === true) {
|
|
527
|
+
vivifyOpt = ((present ? Array.isArray(sample) : keyIsIndex) ? 'array' : 'object');
|
|
528
|
+
}
|
|
529
|
+
const vivifyFn = createVivify(vivifyOpt);
|
|
530
|
+
// Route to the array updater whenever the container is (or will be vivified as) an
|
|
531
|
+
// array, so the updater and the created container agree on shape for a nullish source.
|
|
532
|
+
const isArray = vivifyOpt === 'array'
|
|
533
|
+
? keyIsIndex
|
|
534
|
+
: vivifyOpt === 'object'
|
|
535
|
+
? false
|
|
536
|
+
: Array.isArray(sample) && typeof key === 'number';
|
|
537
|
+
if (isArray)
|
|
538
|
+
return isMutable(source)
|
|
539
|
+
? createMutableArrayUpdater(source, key, vivifyFn)
|
|
540
|
+
: createImmutableArrayUpdater(source, key, vivifyFn);
|
|
541
|
+
return isMutable(source)
|
|
542
|
+
? createMutableObjectUpdater(source, key, vivifyFn)
|
|
543
|
+
: createImmutableObjectUpdater(source, key, vivifyFn);
|
|
544
|
+
}
|
|
404
545
|
function derived(source, optOrKey, opt) {
|
|
405
|
-
const
|
|
406
|
-
|
|
546
|
+
const vivify = typeof optOrKey === 'object' ? false : (opt?.vivify ?? false);
|
|
547
|
+
// With vivification the source may legitimately be null/undefined
|
|
548
|
+
const from = typeof optOrKey === 'object'
|
|
549
|
+
? optOrKey.from
|
|
550
|
+
: vivify
|
|
551
|
+
? (v) => v?.[optOrKey]
|
|
552
|
+
: (v) => v[optOrKey];
|
|
407
553
|
const onChange = typeof optOrKey === 'object'
|
|
408
554
|
? optOrKey.onChange
|
|
409
|
-
:
|
|
410
|
-
? isMutable(source)
|
|
411
|
-
? (next) => {
|
|
412
|
-
source.mutate((cur) => {
|
|
413
|
-
cur[optOrKey] = next;
|
|
414
|
-
return cur;
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
: (next) => {
|
|
418
|
-
source.update((cur) => {
|
|
419
|
-
const newArray = [...cur];
|
|
420
|
-
newArray[optOrKey] = next;
|
|
421
|
-
return newArray;
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
: isMutable(source)
|
|
425
|
-
? (next) => {
|
|
426
|
-
source.mutate((cur) => {
|
|
427
|
-
cur[optOrKey] =
|
|
428
|
-
next;
|
|
429
|
-
return cur;
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
: (next) => {
|
|
433
|
-
source.update((cur) => ({ ...cur, [optOrKey]: next }));
|
|
434
|
-
};
|
|
555
|
+
: createUpdater(source, optOrKey, vivify);
|
|
435
556
|
const rest = typeof optOrKey === 'object' ? { ...optOrKey, ...opt } : opt;
|
|
436
557
|
const baseEqual = rest?.equal ?? Object.is;
|
|
437
558
|
let cnt = 0;
|
|
@@ -2285,6 +2406,12 @@ function signalFromEvent(target, eventName, initial, projectOrOpt, maybeOpt) {
|
|
|
2285
2406
|
}
|
|
2286
2407
|
|
|
2287
2408
|
const IS_STORE = Symbol('MMSTACK::IS_STORE');
|
|
2409
|
+
const SCOPE_PARENT = Symbol('MMSTACK::SCOPE_PARENT');
|
|
2410
|
+
/**
|
|
2411
|
+
* @internal
|
|
2412
|
+
* Test-only handle on the proxy cache (deliberately NOT re-exported from the public barrel).
|
|
2413
|
+
* Maps a store's backing signal to its lazily-built child proxies, each held via a `WeakRef`.
|
|
2414
|
+
*/
|
|
2288
2415
|
const PROXY_CACHE = new WeakMap();
|
|
2289
2416
|
const SIGNAL_FN_PROP = new Set([
|
|
2290
2417
|
'set',
|
|
@@ -2293,6 +2420,11 @@ const SIGNAL_FN_PROP = new Set([
|
|
|
2293
2420
|
'inline',
|
|
2294
2421
|
'asReadonly',
|
|
2295
2422
|
]);
|
|
2423
|
+
/**
|
|
2424
|
+
* @internal
|
|
2425
|
+
* Test-only handle on the finalization registry (deliberately NOT re-exported from the public
|
|
2426
|
+
* barrel). Prunes a cache entry once its proxy is reclaimed by the GC.
|
|
2427
|
+
*/
|
|
2296
2428
|
const PROXY_CLEANUP = new FinalizationRegistry(({ target, prop }) => {
|
|
2297
2429
|
const storeCache = PROXY_CACHE.get(target);
|
|
2298
2430
|
if (storeCache)
|
|
@@ -2307,20 +2439,35 @@ function isStore(value) {
|
|
|
2307
2439
|
value !== null &&
|
|
2308
2440
|
value[IS_STORE] === true);
|
|
2309
2441
|
}
|
|
2310
|
-
function isIndexProp(prop) {
|
|
2311
|
-
return typeof prop === 'string' && prop.trim() !== '' && !isNaN(+prop);
|
|
2312
|
-
}
|
|
2313
2442
|
function isRecord(value) {
|
|
2314
2443
|
if (value === null || typeof value !== 'object')
|
|
2315
2444
|
return false;
|
|
2316
2445
|
const proto = Object.getPrototypeOf(value);
|
|
2317
2446
|
return proto === Object.prototype || proto === null;
|
|
2318
2447
|
}
|
|
2448
|
+
/**
|
|
2449
|
+
* @internal
|
|
2450
|
+
* Resolves the vivify shape for a node from its current value: a present record/array is a
|
|
2451
|
+
* certainty we keep (cached in the derivation, so it survives the value being nulled); an
|
|
2452
|
+
* unknown value (`null`/`undefined`) defers to the caller's option. Off stays off.
|
|
2453
|
+
*/
|
|
2454
|
+
function resolveVivify(sample, option) {
|
|
2455
|
+
if (!option)
|
|
2456
|
+
return false;
|
|
2457
|
+
if (Array.isArray(sample))
|
|
2458
|
+
return 'array';
|
|
2459
|
+
if (isRecord(sample))
|
|
2460
|
+
return 'object';
|
|
2461
|
+
return 'auto';
|
|
2462
|
+
}
|
|
2463
|
+
function hasOwnKey(value, key) {
|
|
2464
|
+
return value != null && Object.hasOwn(value, key);
|
|
2465
|
+
}
|
|
2319
2466
|
/**
|
|
2320
2467
|
* @internal
|
|
2321
2468
|
* Makes an array store
|
|
2322
2469
|
*/
|
|
2323
|
-
function toArrayStore(source, injector) {
|
|
2470
|
+
function toArrayStore(source, injector, vivify) {
|
|
2324
2471
|
if (isStore(source))
|
|
2325
2472
|
return source;
|
|
2326
2473
|
const isMutableSource = isMutable(source);
|
|
@@ -2400,31 +2547,39 @@ function toArrayStore(source, injector) {
|
|
|
2400
2547
|
const value = untracked(target);
|
|
2401
2548
|
const valueIsArray = Array.isArray(value);
|
|
2402
2549
|
const valueIsRecord = isRecord(value);
|
|
2550
|
+
const nodeVivify = resolveVivify(value, vivify);
|
|
2551
|
+
const vivifyFn = createVivify(nodeVivify);
|
|
2403
2552
|
const equalFn = (valueIsRecord || valueIsArray) &&
|
|
2404
2553
|
isMutableSource &&
|
|
2405
2554
|
typeof value[idx] === 'object'
|
|
2406
2555
|
? () => false
|
|
2407
2556
|
: undefined;
|
|
2408
2557
|
const computation = valueIsRecord
|
|
2409
|
-
? derived(target, idx, {
|
|
2558
|
+
? derived(target, idx, {
|
|
2559
|
+
equal: equalFn,
|
|
2560
|
+
vivify: nodeVivify,
|
|
2561
|
+
})
|
|
2410
2562
|
: derived(target, {
|
|
2411
2563
|
from: (v) => v?.[idx],
|
|
2412
2564
|
onChange: (newValue) => target.update((v) => {
|
|
2413
|
-
|
|
2414
|
-
|
|
2565
|
+
const container = vivifyFn(v, idx);
|
|
2566
|
+
if (container === null || container === undefined)
|
|
2567
|
+
return container;
|
|
2415
2568
|
try {
|
|
2416
|
-
|
|
2569
|
+
container[idx] = newValue;
|
|
2417
2570
|
}
|
|
2418
2571
|
catch (e) {
|
|
2419
2572
|
if (isDevMode())
|
|
2420
2573
|
console.error(`[store] Failed to set property "${String(idx)}"`, e);
|
|
2421
2574
|
}
|
|
2422
|
-
return
|
|
2575
|
+
return container;
|
|
2423
2576
|
}),
|
|
2424
2577
|
});
|
|
2425
|
-
const
|
|
2426
|
-
|
|
2427
|
-
|
|
2578
|
+
const childSample = untracked(computation);
|
|
2579
|
+
const childVivify = resolveVivify(childSample, vivify);
|
|
2580
|
+
const proxy = Array.isArray(childSample)
|
|
2581
|
+
? toArrayStore(computation, injector, childVivify)
|
|
2582
|
+
: toStore(computation, injector, childVivify);
|
|
2428
2583
|
const ref = new WeakRef(proxy);
|
|
2429
2584
|
storeCache.set(idx, ref);
|
|
2430
2585
|
PROXY_CLEANUP.register(proxy, { target, prop: idx }, ref);
|
|
@@ -2441,7 +2596,7 @@ function toArrayStore(source, injector) {
|
|
|
2441
2596
|
* const state = store({ user: { name: 'John' } });
|
|
2442
2597
|
* const nameSignal = state.user.name; // WritableSignal<string>
|
|
2443
2598
|
*/
|
|
2444
|
-
function toStore(source, injector) {
|
|
2599
|
+
function toStore(source, injector, vivify = false) {
|
|
2445
2600
|
if (isStore(source))
|
|
2446
2601
|
return source;
|
|
2447
2602
|
if (!injector)
|
|
@@ -2451,7 +2606,8 @@ function toStore(source, injector) {
|
|
|
2451
2606
|
: toWritable(source, () => {
|
|
2452
2607
|
// noop
|
|
2453
2608
|
});
|
|
2454
|
-
const
|
|
2609
|
+
const isWritableSource = isWritableSignal(source);
|
|
2610
|
+
const isMutableSource = isWritableSource && isMutable(writableSource);
|
|
2455
2611
|
const s = new Proxy(writableSource, {
|
|
2456
2612
|
has(_, prop) {
|
|
2457
2613
|
return Reflect.has(untracked(source), prop);
|
|
@@ -2479,10 +2635,16 @@ function toStore(source, injector) {
|
|
|
2479
2635
|
return true;
|
|
2480
2636
|
if (prop === 'asReadonlyStore')
|
|
2481
2637
|
return () => {
|
|
2482
|
-
if (!
|
|
2638
|
+
if (!isWritableSource)
|
|
2483
2639
|
return s;
|
|
2484
|
-
return untracked(() => toStore(source.asReadonly(), injector));
|
|
2640
|
+
return untracked(() => toStore(source.asReadonly(), injector, vivify));
|
|
2485
2641
|
};
|
|
2642
|
+
if (prop === 'extend')
|
|
2643
|
+
return (seed) => scopedStore(s, seed, isMutableSource
|
|
2644
|
+
? 'mutable'
|
|
2645
|
+
: isWritableSource
|
|
2646
|
+
? 'writable'
|
|
2647
|
+
: 'readonly', injector);
|
|
2486
2648
|
if (typeof prop === 'symbol' || SIGNAL_FN_PROP.has(prop))
|
|
2487
2649
|
return target[prop];
|
|
2488
2650
|
let storeCache = PROXY_CACHE.get(target);
|
|
@@ -2501,31 +2663,36 @@ function toStore(source, injector) {
|
|
|
2501
2663
|
const value = untracked(target);
|
|
2502
2664
|
const valueIsRecord = isRecord(value);
|
|
2503
2665
|
const valueIsArray = Array.isArray(value);
|
|
2666
|
+
const nodeVivify = resolveVivify(value, vivify);
|
|
2667
|
+
const vivifyFn = createVivify(nodeVivify);
|
|
2504
2668
|
const equalFn = (valueIsRecord || valueIsArray) &&
|
|
2505
2669
|
isMutableSource &&
|
|
2506
2670
|
typeof value[prop] === 'object'
|
|
2507
2671
|
? () => false
|
|
2508
2672
|
: undefined;
|
|
2509
2673
|
const computation = valueIsRecord
|
|
2510
|
-
? derived(target, prop, { equal: equalFn })
|
|
2674
|
+
? derived(target, prop, { equal: equalFn, vivify: nodeVivify })
|
|
2511
2675
|
: derived(target, {
|
|
2512
2676
|
from: (v) => v?.[prop],
|
|
2513
2677
|
onChange: (newValue) => target.update((v) => {
|
|
2514
|
-
|
|
2515
|
-
|
|
2678
|
+
const container = vivifyFn(v, prop);
|
|
2679
|
+
if (container === null || container === undefined)
|
|
2680
|
+
return container;
|
|
2516
2681
|
try {
|
|
2517
|
-
|
|
2682
|
+
container[prop] = newValue;
|
|
2518
2683
|
}
|
|
2519
2684
|
catch (e) {
|
|
2520
2685
|
if (isDevMode())
|
|
2521
2686
|
console.error(`[store] Failed to set property "${String(prop)}"`, e);
|
|
2522
2687
|
}
|
|
2523
|
-
return
|
|
2688
|
+
return container;
|
|
2524
2689
|
}),
|
|
2525
2690
|
});
|
|
2526
|
-
const
|
|
2527
|
-
|
|
2528
|
-
|
|
2691
|
+
const childSample = untracked(computation);
|
|
2692
|
+
const childVivify = resolveVivify(childSample, vivify);
|
|
2693
|
+
const proxy = Array.isArray(childSample)
|
|
2694
|
+
? toArrayStore(computation, injector, childVivify)
|
|
2695
|
+
: toStore(computation, injector, childVivify);
|
|
2529
2696
|
const ref = new WeakRef(proxy);
|
|
2530
2697
|
storeCache.set(prop, ref);
|
|
2531
2698
|
PROXY_CLEANUP.register(proxy, { target, prop }, ref);
|
|
@@ -2534,19 +2701,103 @@ function toStore(source, injector) {
|
|
|
2534
2701
|
});
|
|
2535
2702
|
return s;
|
|
2536
2703
|
}
|
|
2704
|
+
/**
|
|
2705
|
+
* @internal
|
|
2706
|
+
* Backs `store.extend(...)`. Builds a scoped overlay over `parent`: the local layer (the seed
|
|
2707
|
+
* plus any keys created later) is its own signal and `parent` is its own signal, so the getter
|
|
2708
|
+
* routes each key by consulting BOTH — local first, then parent, else local (so a write to an
|
|
2709
|
+
* as-yet-unknown key lands locally). Inherited keys return the parent's own sub-store (shared
|
|
2710
|
+
* identity + two-way), while local keys never propagate upward. A merged `computed` is derived
|
|
2711
|
+
* only for whole-object reads / `has` / iteration — never for routing.
|
|
2712
|
+
*/
|
|
2713
|
+
function scopedStore(parent, seed, kind, injector) {
|
|
2714
|
+
const local = isSignal(seed)
|
|
2715
|
+
? toStore(seed, injector)
|
|
2716
|
+
: kind === 'mutable'
|
|
2717
|
+
? mutableStore(seed, { injector })
|
|
2718
|
+
: kind === 'readonly'
|
|
2719
|
+
? store(seed, { injector }).asReadonlyStore()
|
|
2720
|
+
: store(seed, { injector });
|
|
2721
|
+
const localValue = () => untracked(local);
|
|
2722
|
+
const parentValue = () => untracked(parent);
|
|
2723
|
+
const view = computed(() => ({
|
|
2724
|
+
...parent(),
|
|
2725
|
+
...local(),
|
|
2726
|
+
}), ...(ngDevMode ? [{ debugName: "view" }] : []));
|
|
2727
|
+
const splitSet = (next) => {
|
|
2728
|
+
const lv = localValue();
|
|
2729
|
+
const pv = parentValue();
|
|
2730
|
+
for (const key of Reflect.ownKeys(next)) {
|
|
2731
|
+
const layer = hasOwnKey(lv, key)
|
|
2732
|
+
? local
|
|
2733
|
+
: hasOwnKey(pv, key)
|
|
2734
|
+
? parent
|
|
2735
|
+
: local;
|
|
2736
|
+
layer[key].set(next[key]);
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
const base = toWritable(view, kind === 'readonly' ? () => undefined : splitSet, undefined, { pure: false });
|
|
2740
|
+
if (kind === 'mutable') {
|
|
2741
|
+
base.mutate = (updater) => splitSet(updater(untracked(view)));
|
|
2742
|
+
base.inline = (updater) => base.mutate((prev) => {
|
|
2743
|
+
updater(prev);
|
|
2744
|
+
return prev;
|
|
2745
|
+
});
|
|
2746
|
+
}
|
|
2747
|
+
const scope = new Proxy(base, {
|
|
2748
|
+
get(target, prop) {
|
|
2749
|
+
if (prop === IS_STORE)
|
|
2750
|
+
return true;
|
|
2751
|
+
if (prop === SCOPE_PARENT)
|
|
2752
|
+
return parent;
|
|
2753
|
+
if (prop === 'extend')
|
|
2754
|
+
return (childSeed) => scopedStore(scope, childSeed, kind, injector);
|
|
2755
|
+
if (prop === 'asReadonlyStore')
|
|
2756
|
+
return () => toStore(computed(() => ({ ...parent(), ...local() })), injector);
|
|
2757
|
+
if (typeof prop === 'symbol' || SIGNAL_FN_PROP.has(prop))
|
|
2758
|
+
return target[prop];
|
|
2759
|
+
// Route by consulting both signals: local first, then parent, else local (new → local).
|
|
2760
|
+
if (hasOwnKey(localValue(), prop))
|
|
2761
|
+
return local[prop];
|
|
2762
|
+
if (hasOwnKey(parentValue(), prop))
|
|
2763
|
+
return parent[prop];
|
|
2764
|
+
return local[prop];
|
|
2765
|
+
},
|
|
2766
|
+
has(_, prop) {
|
|
2767
|
+
return hasOwnKey(localValue(), prop) || hasOwnKey(parentValue(), prop);
|
|
2768
|
+
},
|
|
2769
|
+
ownKeys() {
|
|
2770
|
+
return [
|
|
2771
|
+
...new Set([
|
|
2772
|
+
...Reflect.ownKeys(parentValue()),
|
|
2773
|
+
...Reflect.ownKeys(localValue()),
|
|
2774
|
+
]),
|
|
2775
|
+
];
|
|
2776
|
+
},
|
|
2777
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
2778
|
+
if (hasOwnKey(localValue(), prop) || hasOwnKey(parentValue(), prop))
|
|
2779
|
+
return { enumerable: true, configurable: true };
|
|
2780
|
+
return undefined;
|
|
2781
|
+
},
|
|
2782
|
+
getPrototypeOf() {
|
|
2783
|
+
return Object.prototype;
|
|
2784
|
+
},
|
|
2785
|
+
});
|
|
2786
|
+
return scope;
|
|
2787
|
+
}
|
|
2537
2788
|
/**
|
|
2538
2789
|
* Creates a WritableSignalStore from a value.
|
|
2539
2790
|
* @see {@link toStore}
|
|
2540
2791
|
*/
|
|
2541
2792
|
function store(value, opt) {
|
|
2542
|
-
return toStore(signal(value, opt), opt?.injector);
|
|
2793
|
+
return toStore(signal(value, opt), opt?.injector, opt?.vivify ?? false);
|
|
2543
2794
|
}
|
|
2544
2795
|
/**
|
|
2545
2796
|
* Creates a MutableSignalStore from a value.
|
|
2546
2797
|
* @see {@link toStore}
|
|
2547
2798
|
*/
|
|
2548
2799
|
function mutableStore(value, opt) {
|
|
2549
|
-
return toStore(mutable(value, opt), opt?.injector);
|
|
2800
|
+
return toStore(mutable(value, opt), opt?.injector, opt?.vivify ?? false);
|
|
2550
2801
|
}
|
|
2551
2802
|
|
|
2552
2803
|
// Internal dummy store for server-side rendering
|