@mmstack/primitives 20.5.11 → 20.6.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.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { isDevMode, inject, Injector, untracked, effect, DestroyRef, linkedSignal, computed, signal, isSignal, PLATFORM_ID, ElementRef, Injectable, runInInjectionContext } from '@angular/core';
2
+ import { isDevMode, inject, Injector, untracked, effect, DestroyRef, linkedSignal, InjectionToken, TemplateRef, ViewContainerRef, input, computed, Directive, signal, PLATFORM_ID, runInInjectionContext, afterNextRender, Component, isSignal, ElementRef, Injectable } from '@angular/core';
3
3
  import { isPlatformServer } from '@angular/common';
4
4
  import { SIGNAL } from '@angular/core/primitives/signals';
5
5
 
@@ -216,6 +216,563 @@ function chunked(source, options) {
216
216
  return internal.asReadonly();
217
217
  }
218
218
 
219
+ /**
220
+ * Whether the subtree a resource/component lives in is currently PAUSED, for Activity / keep-alive.
221
+ * Provided by an Activity boundary (`MmActivity`, or the app-builder's per-branch injector) and read
222
+ * — only at instantiation — by anything that should pause its background work while paused (a resource
223
+ * returning its `paused` token, a `<video>` pausing playback, the pausable primitives, …). Absent
224
+ * unless an Activity boundary provides one — read it via `injectPaused()`, which falls back to a
225
+ * never-paused signal, so code that isn't inside an Activity boundary is unaffected.
226
+ */
227
+ const PAUSED_CONTEXT = new InjectionToken('@mmstack/primitives:paused-context');
228
+ /**
229
+ * Keep-alive (the Angular analog of React's `<Activity>` / Vue's `<keep-alive>`): the wrapped
230
+ * subtree is mounted ONCE and kept — when `[mmActivity]` is false it's hidden (`display:none`) and
231
+ * its change detection is paused, preserving state (scroll, inputs, a video's position, loaded
232
+ * data); when true it's shown and CD resumes. It is never destroyed until the directive is.
233
+ *
234
+ * It also provides {@link PAUSED_CONTEXT} to the content (= the negation of `visible`), so descendants
235
+ * can pause *effect-driven* or *Observable* work while hidden (CD-detach alone pauses pull-based/template work, not
236
+ * effects/polling). If you're using the pausable primitives this is done automatically
237
+ *
238
+ * ```html
239
+ * <section *mmActivity="tab() === 'editor'"> ...heavy stateful editor... </section>
240
+ * ```
241
+ */
242
+ class MmActivity {
243
+ tpl = inject(TemplateRef);
244
+ vcr = inject(ViewContainerRef);
245
+ parent = inject(Injector);
246
+ /** When false, keep the content mounted but hidden + CD-detached. */
247
+ visible = input.required(...(ngDevMode ? [{ debugName: "visible", alias: 'mmActivity' }] : [{ alias: 'mmActivity' }]));
248
+ /** Paused == not visible — handed to the kept subtree as PAUSED_CONTEXT. */
249
+ paused = computed(() => !this.visible(), ...(ngDevMode ? [{ debugName: "paused" }] : []));
250
+ view = null;
251
+ constructor() {
252
+ effect(() => {
253
+ const visible = this.visible();
254
+ untracked(() => this.apply(visible));
255
+ });
256
+ }
257
+ apply(visible) {
258
+ if (!this.view) {
259
+ // Created once, kept for the directive's lifetime. The content gets PAUSED_CONTEXT = !visible,
260
+ // so resources/components inside can pause their effect-driven work while hidden.
261
+ this.view = this.vcr.createEmbeddedView(this.tpl, {}, {
262
+ injector: Injector.create({
263
+ parent: this.parent,
264
+ providers: [providePaused(this.paused)],
265
+ }),
266
+ });
267
+ }
268
+ for (const node of this.view.rootNodes) {
269
+ if (node instanceof HTMLElement)
270
+ node.style.display = visible ? '' : 'none';
271
+ }
272
+ if (visible)
273
+ this.view.reattach();
274
+ else
275
+ this.view.detach();
276
+ }
277
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: MmActivity, deps: [], target: i0.ɵɵFactoryTarget.Directive });
278
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.3.17", type: MmActivity, isStandalone: true, selector: "[mmActivity]", inputs: { visible: { classPropertyName: "visible", publicName: "mmActivity", isSignal: true, isRequired: true, transformFunction: null } }, ngImport: i0 });
279
+ }
280
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: MmActivity, decorators: [{
281
+ type: Directive,
282
+ args: [{
283
+ selector: '[mmActivity]',
284
+ }]
285
+ }], ctorParameters: () => [], propDecorators: { visible: [{ type: i0.Input, args: [{ isSignal: true, alias: "mmActivity", required: true }] }] } });
286
+ // Shared never-paused signal returned outside a boundary / on the server (SSR renders the full tree,
287
+ // nothing is paused). Readonly so a consumer can't cast-and-`.set()` the shared default for everyone.
288
+ const NEVER_PAUSED = signal(false).asReadonly();
289
+ /**
290
+ * Inject the nearest paused-state signal — `true` while the surrounding subtree is paused (hidden by
291
+ * an Activity boundary). Defaults to a never-paused signal, so callers outside an Activity are
292
+ * unaffected; on the server it is always never-paused, so server-side work (e.g. connector fetches)
293
+ * isn't suppressed. This is the public way to read pause state; the underlying token is intentionally
294
+ * not exported.
295
+ */
296
+ function injectPaused() {
297
+ if (isPlatformServer(inject(PLATFORM_ID, { optional: true }) ?? 'browser'))
298
+ return NEVER_PAUSED;
299
+ return inject(PAUSED_CONTEXT, { optional: true }) ?? NEVER_PAUSED;
300
+ }
301
+ /**
302
+ * Build a provider that supplies a paused-state signal to a subtree — the public way to set up an
303
+ * Activity-style pause boundary (used by `MmActivity` and the app-builder's per-branch injectors).
304
+ */
305
+ function providePaused(source) {
306
+ return { provide: PAUSED_CONTEXT, useValue: source };
307
+ }
308
+
309
+ /**
310
+ * Structural hold-and-swap as a signal. Given a `target` (the desired value — e.g. the
311
+ * subtree/def/key you want to show) and a `ready` predicate, returns a signal that keeps
312
+ * yielding its PREVIOUS value until `ready()` is true, then swaps to the current target.
313
+ *
314
+ * This is the structural counterpart to `keepPrevious`/`commit`: where those hold a *value*
315
+ * through a reload, this holds a *structure* through a swap. The caller mounts the incoming
316
+ * structure off to the side (so its resources can settle and flip `ready`), keeps showing the
317
+ * held previous structure meanwhile, and lets the old one go once `ready` releases the swap.
318
+ *
319
+ * The very first value passes straight through (nothing to hold yet).
320
+ */
321
+ function holdUntilReady(target, ready) {
322
+ return linkedSignal({
323
+ source: () => ({ t: target(), ready: ready() }),
324
+ computation: (curr, prev) => (prev === undefined || curr.ready ? curr.t : prev.value),
325
+ });
326
+ }
327
+
328
+ /**
329
+ * Resolve a {@link PauseOption} into a pause predicate, or `null` meaning "do not pause".
330
+ * `null` tells the caller to return the bare primitive — no wrapper is created.
331
+ *
332
+ * - omitted/`true` → the ambient {@link PAUSED_CONTEXT} if an Activity boundary provides one (via
333
+ * `opt.injector` or the current injection context), else `null` (the bare primitive, no allocation).
334
+ * The default, because an explicit `pausable*` call wants to be pausable. An explicit `pause: true`
335
+ * with no boundary dev-warns; the omitted default stays quiet. SSR → `null`.
336
+ * - a function → returned as-is (covers `Signal<boolean>`; usable outside an injection context).
337
+ * SSR → `null` here too, detected via `opt.injector` if given, else a `globalThis.window` probe.
338
+ * - `false` → `null` (the explicit opt-out).
339
+ *
340
+ * Encapsulating this here keeps every pausable primitive's branching identical and in one place.
341
+ */
342
+ function resolvePause(opt) {
343
+ const explicit = opt?.pause; // distinguish explicit `true` from the omitted default
344
+ const pause = explicit ?? true; // explicit pausable* calls default to pausing
345
+ if (pause === false)
346
+ return null;
347
+ const run = (fn) => opt?.injector ? runInInjectionContext(opt.injector, fn) : fn();
348
+ const onServer = () => typeof pause === 'function' && !opt?.injector
349
+ ? typeof globalThis.window === 'undefined'
350
+ : run(() => isPlatformServer(inject(PLATFORM_ID, { optional: true }) ?? 'browser'));
351
+ if (typeof pause === 'function')
352
+ return onServer() ? null : pause;
353
+ if (onServer())
354
+ return null;
355
+ const paused = run(() => inject(PAUSED_CONTEXT, { optional: true }));
356
+ if (!paused) {
357
+ if (explicit === true && isDevMode())
358
+ console.warn('[pausable] `pause: true` but no PAUSED_CONTEXT in scope — not pausing. Provide one via an ' +
359
+ 'Activity boundary (`MmActivity` / `providePaused`), or pass a predicate / `pause: false`.');
360
+ return null;
361
+ }
362
+ return paused;
363
+ }
364
+ /**
365
+ * Like {@link nestedEffect}, but pausable. While paused the effect does NOT run its body — and,
366
+ * crucially, it reads the pause predicate FIRST, so while paused its dependency set collapses to just
367
+ * the predicate (no churn from the real deps); on resume it re-runs and re-tracks. With no `pause`
368
+ * option it defaults to the ambient `PAUSED_CONTEXT`; `pause: false` makes it a plain `nestedEffect`
369
+ * with zero added overhead.
370
+ */
371
+ function pausableEffect(effectFn, options) {
372
+ const paused = resolvePause(options);
373
+ if (!paused)
374
+ return nestedEffect(effectFn, options);
375
+ return nestedEffect((registerCleanup) => {
376
+ if (paused())
377
+ return; // read FIRST → while paused, deps collapse to just the predicate
378
+ effectFn(registerCleanup);
379
+ }, options);
380
+ }
381
+ /**
382
+ * Like `signal`, but pausable. While paused, READS hold the last value; writes still land on the
383
+ * underlying signal and surface on resume. Built on the `keepPrevious`/`hold` shape — a
384
+ * `linkedSignal` gated on the pause predicate, with `set`/`update`/`asReadonly` forwarded to the
385
+ * source signal. With no `pause` option it defaults to the ambient `PAUSED_CONTEXT`; `pause: false`
386
+ * makes it a plain `signal` — no `linkedSignal` is created.
387
+ *
388
+ * NOTE: while paused, `set(x)` followed by a read returns the *held* (pre-pause) value, not `x` — the
389
+ * write lands on the source and surfaces on resume. That is the "freeze the displayed value while
390
+ * hidden" semantics; do not rely on read-after-write while paused.
391
+ */
392
+ function pausableSignal(initialValue, options) {
393
+ const paused = resolvePause(options);
394
+ const src = signal(initialValue, options);
395
+ if (!paused)
396
+ return src;
397
+ const read = linkedSignal(...(ngDevMode ? [{ debugName: "read", source: () => ({ v: src(), paused: paused() }),
398
+ computation: (curr, prev) => prev !== undefined && curr.paused ? prev.value : curr.v,
399
+ equal: options?.equal }] : [{
400
+ source: () => ({ v: src(), paused: paused() }),
401
+ computation: (curr, prev) => prev !== undefined && curr.paused ? prev.value : curr.v,
402
+ equal: options?.equal,
403
+ }]));
404
+ read.set = src.set;
405
+ read.update = src.update;
406
+ read.asReadonly = src.asReadonly;
407
+ return read;
408
+ }
409
+ /**
410
+ * Like `computed`, but pausable. While paused it holds its last value AND does not recompute: the
411
+ * computation's dependencies are not read while paused, so a dependency change can't trigger work —
412
+ * on resume it recomputes and re-tracks. The very first read always computes, to seed a value. With
413
+ * no `pause` option it defaults to the ambient `PAUSED_CONTEXT`; `pause: false` makes it a plain
414
+ * `computed`.
415
+ */
416
+ function pausableComputed(computation, options) {
417
+ const paused = resolvePause(options);
418
+ if (!paused)
419
+ return computed(computation, options);
420
+ const HELD = Symbol('paused-hold');
421
+ const ls = linkedSignal(...(ngDevMode ? [{ debugName: "ls", source: () => (paused() ? HELD : computation()),
422
+ computation: (next, prev) => next !== HELD ? next : prev !== undefined ? prev.value : computation(),
423
+ equal: options?.equal }] : [{
424
+ source: () => (paused() ? HELD : computation()),
425
+ computation: (next, prev) => next !== HELD ? next : prev !== undefined ? prev.value : computation(),
426
+ equal: options?.equal,
427
+ }]));
428
+ return ls.asReadonly();
429
+ }
430
+
431
+ const { is } = Object;
432
+ function mutable(initial, opt) {
433
+ const baseEqual = opt?.equal ?? is;
434
+ let cnt = 0;
435
+ const equal = (a, b) => {
436
+ if (cnt > 0)
437
+ return false;
438
+ return baseEqual(a, b);
439
+ };
440
+ const sig = signal(initial, {
441
+ ...opt,
442
+ equal,
443
+ });
444
+ const internalUpdate = sig.update;
445
+ sig.mutate = (updater) => {
446
+ cnt++;
447
+ internalUpdate(updater);
448
+ cnt--;
449
+ };
450
+ sig.inline = (updater) => {
451
+ sig.mutate((prev) => {
452
+ updater(prev);
453
+ return prev;
454
+ });
455
+ };
456
+ return sig;
457
+ }
458
+ /**
459
+ * Type guard function to check if a given `WritableSignal` is a `MutableSignal`. This is useful
460
+ * for situations where you need to conditionally use the `mutate` or `inline` methods.
461
+ *
462
+ * @typeParam T - The type of the signal's value (optional, defaults to `any`).
463
+ * @param value - The `WritableSignal` to check.
464
+ * @returns `true` if the signal is a `MutableSignal`, `false` otherwise.
465
+ *
466
+ * @example
467
+ * const mySignal = signal(0);
468
+ * const myMutableSignal = mutable(0);
469
+ *
470
+ * if (isMutable(mySignal)) {
471
+ * mySignal.mutate(x => x + 1); // This would cause a type error, as mySignal is not a MutableSignal.
472
+ * }
473
+ *
474
+ * if (isMutable(myMutableSignal)) {
475
+ * myMutableSignal.mutate(x => x + 1); // This is safe.
476
+ * }
477
+ */
478
+ function isMutable(value) {
479
+ return 'mutate' in value && typeof value.mutate === 'function';
480
+ }
481
+
482
+ function createTransitionScope() {
483
+ const list = mutable([]);
484
+ const pending = computed(() => list().some(({ ref }) => {
485
+ const s = ref.status();
486
+ return s === 'loading' || s === 'reloading';
487
+ }), ...(ngDevMode ? [{ debugName: "pending" }] : []));
488
+ const holdCount = signal(0, ...(ngDevMode ? [{ debugName: "holdCount" }] : []));
489
+ const holding = computed(() => holdCount() > 0, ...(ngDevMode ? [{ debugName: "holding" }] : []));
490
+ return {
491
+ resources: computed(() => list().map((e) => e.ref)),
492
+ pending,
493
+ suspended: (type) => list().some(({ ref, suspends }) => suspends && (type === 'loading' ? ref.isLoading() : !ref.hasValue())),
494
+ add: (ref, opt) => untracked(() => list.inline((c) => c.push({ ref, suspends: opt?.suspends ?? true }))),
495
+ remove: (ref) => untracked(() => list.inline((c) => {
496
+ const i = c.findIndex((e) => e.ref === ref);
497
+ if (i !== -1)
498
+ c.splice(i, 1);
499
+ })),
500
+ commit: (value) => linkedSignal({
501
+ source: () => ({ v: value(), settled: !pending() }),
502
+ computation: (curr, prev) => curr.settled || prev === undefined ? curr.v : prev.value,
503
+ }),
504
+ holding,
505
+ beginHold: () => untracked(() => holdCount.update((c) => c + 1)),
506
+ endHold: () => untracked(() => holdCount.update((c) => (c > 0 ? c - 1 : 0))),
507
+ hold: (value) => linkedSignal({
508
+ source: () => ({ v: value(), held: holding() }),
509
+ computation: (curr, prev) => prev !== undefined && curr.held ? prev.value : curr.v,
510
+ }),
511
+ };
512
+ }
513
+ function createNoopScope() {
514
+ return {
515
+ resources: computed(() => []),
516
+ pending: computed(() => false),
517
+ suspended: () => false,
518
+ add: () => {
519
+ // noop
520
+ },
521
+ remove: () => {
522
+ // noop
523
+ },
524
+ commit: (value) => value,
525
+ holding: computed(() => false),
526
+ beginHold: () => {
527
+ // noop
528
+ },
529
+ endHold: () => {
530
+ // noop
531
+ },
532
+ hold: (value) => value,
533
+ };
534
+ }
535
+ const TRANSITION_SCOPE = new InjectionToken('@mmstack/resource:transition-scope');
536
+ /** Provide a fresh transition scope at a boundary so its subtree's resources are tracked independently. */
537
+ function provideTransitionScope() {
538
+ return { provide: TRANSITION_SCOPE, useFactory: createTransitionScope };
539
+ }
540
+ function injectTransitionScope() {
541
+ const scope = inject(TRANSITION_SCOPE, { optional: true });
542
+ if (!scope) {
543
+ if (isDevMode())
544
+ console.warn('[mmstack/resource] No transition scope in context — registration/tracking here is a no-op. ' +
545
+ 'Use a <mm-suspense> boundary or provideTransitionScope() in an ancestor.');
546
+ return createNoopScope();
547
+ }
548
+ return scope;
549
+ }
550
+ /**
551
+ * Returns a register function bound to the nearest transition scope: it adds a resource
552
+ * to the scope and removes it when the caller's injection context is destroyed. Pass any
553
+ * `ResourceRef` (a query, mutation, or plain Angular resource) through it.
554
+ */
555
+ function injectRegisterResource() {
556
+ const scope = injectTransitionScope();
557
+ const destroyRef = inject(DestroyRef);
558
+ return (res, opt) => {
559
+ scope.add(res, opt);
560
+ destroyRef.onDestroy(() => scope.remove(res));
561
+ return res;
562
+ };
563
+ }
564
+ /** Convenience: register a resource with the nearest transition scope. Must run in an injection context. */
565
+ function registerResource(res, opt) {
566
+ return injectRegisterResource()(res, opt);
567
+ }
568
+
569
+ /**
570
+ * Returns a `startTransition(fn)` bound to the nearest transition scope. `fn` runs its state
571
+ * mutations (which commit immediately); any resource that reloads as a result holds its value
572
+ * (when `coordinate`/`commit`-wrapped) and reveals together once everything settles. The
573
+ * returned handle exposes a unified `pending` + `done` for the whole operation — for imperative
574
+ * coordination (disable a control, await completion) on top of the declarative hold-and-commit.
575
+ *
576
+ * Must be called in an injection context. This is the *async* generalization (Tier 2): it adds
577
+ * no rendering cost and needs no fork — holding direct/sync readers is a separate, deferred tier.
578
+ */
579
+ function injectStartTransition() {
580
+ const scope = injectTransitionScope();
581
+ const injector = inject(Injector);
582
+ return (fn) => {
583
+ untracked(fn);
584
+ let sawPending = false;
585
+ const done = new Promise((resolve) => {
586
+ const watcher = effect(() => {
587
+ const p = scope.pending();
588
+ if (p)
589
+ sawPending = true;
590
+ // settle: requests went in flight and then drained
591
+ if (sawPending && !p) {
592
+ watcher.destroy();
593
+ resolve();
594
+ }
595
+ }, ...(ngDevMode ? [{ debugName: "watcher", injector }] : [{ injector }]));
596
+ // no-async fallback: once the reactive system has processed the writes (afterNextRender),
597
+ // if nothing ever went in flight, the transition is already complete.
598
+ afterNextRender(() => {
599
+ if (!sawPending && !untracked(scope.pending)) {
600
+ watcher.destroy();
601
+ resolve();
602
+ }
603
+ }, { injector });
604
+ });
605
+ return { pending: scope.pending, done };
606
+ };
607
+ }
608
+
609
+ /**
610
+ * Shared **suspense** (readiness) boundary behaviour: reads the *nearest* transition scope and exposes
611
+ * its `pending`/`suspended` state. This is the readiness gate — distinct from the hold-stale *swap*
612
+ * primitives (`TransitionRouterOutlet`, `ab-transition`), which are the actual "transitions". The two
613
+ * concrete components below differ only by whether they provide their own scope, so the logic (and
614
+ * template) live here once.
615
+ *
616
+ * - **First load** (`suspended()`): no value yet → show the `[placeholder]` fallback.
617
+ * - **Reload** (`pending()` but a value is held via `keepPrevious`): keep the real content mounted and
618
+ * surface a busy indicator (`aria-busy`, and an optional `[busy]` slot) instead of flashing back to
619
+ * the placeholder.
620
+ *
621
+ * `type` selects what "not ready" means: `'value'` (default) suspends only until a first value lands
622
+ * then holds through reloads; `'loading'` suspends on every in-flight load (strict suspense).
623
+ */
624
+ class SuspenseBoundaryBase {
625
+ scope = injectTransitionScope();
626
+ /** What counts as "not ready" for the first-load placeholder. Defaults to value-presence. */
627
+ type = input('value', ...(ngDevMode ? [{ debugName: "type" }] : []));
628
+ pending = this.scope.pending;
629
+ suspended = computed(() => this.scope.suspended(this.type()), ...(ngDevMode ? [{ debugName: "suspended" }] : []));
630
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: SuspenseBoundaryBase, deps: [], target: i0.ɵɵFactoryTarget.Directive });
631
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "20.3.17", type: SuspenseBoundaryBase, isStandalone: true, inputs: { type: { classPropertyName: "type", publicName: "type", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 });
632
+ }
633
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: SuspenseBoundaryBase, decorators: [{
634
+ type: Directive
635
+ }], propDecorators: { type: [{ type: i0.Input, args: [{ isSignal: true, alias: "type", required: false }] }] } });
636
+ const SUSPENSE_TEMPLATE = `
637
+ @if (suspended()) {
638
+ <ng-content select="[placeholder]"><span>Loading…</span></ng-content>
639
+ } @else {
640
+ @if (pending()) {
641
+ <ng-content select="[busy]" />
642
+ }
643
+ <ng-content />
644
+ }
645
+ `;
646
+ // `display: contents` so the boundary adds no box of its own.
647
+ const SUSPENSE_STYLES = `
648
+ :host {
649
+ display: contents;
650
+ }
651
+ `;
652
+ const SUSPENSE_HOST = {
653
+ '[attr.aria-busy]': 'pending() ? true : null',
654
+ };
655
+ /**
656
+ * Standalone suspense boundary — **provides its own scope**, so dropping a `<mm-suspense>` anywhere
657
+ * just works: the resources created in its subtree register into it without any extra
658
+ * `provideTransitionScope()`. The common case.
659
+ */
660
+ class SuspenseBoundary extends SuspenseBoundaryBase {
661
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: SuspenseBoundary, deps: null, target: i0.ɵɵFactoryTarget.Component });
662
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.17", type: SuspenseBoundary, isStandalone: true, selector: "mm-suspense", host: { properties: { "attr.aria-busy": "pending() ? true : null" } }, providers: [provideTransitionScope()], usesInheritance: true, ngImport: i0, template: "\n @if (suspended()) {\n <ng-content select=\"[placeholder]\"><span>Loading\u2026</span></ng-content>\n } @else {\n @if (pending()) {\n <ng-content select=\"[busy]\" />\n }\n <ng-content />\n }\n", isInline: true, styles: [":host{display:contents}\n"] });
663
+ }
664
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: SuspenseBoundary, decorators: [{
665
+ type: Component,
666
+ args: [{ selector: 'mm-suspense', template: SUSPENSE_TEMPLATE, host: SUSPENSE_HOST, providers: [provideTransitionScope()], styles: [":host{display:contents}\n"] }]
667
+ }] });
668
+ /**
669
+ * Unscoped suspense boundary — **reads the ambient scope** instead of providing one. For cases where
670
+ * the resources to coordinate are registered *above* the boundary (e.g. an app-builder page whose
671
+ * manifests/connectors register at a higher injector), so the boundary observes that outer scope
672
+ * rather than opening a fresh one. Pair with a `provideTransitionScope()` (or another boundary) in an
673
+ * ancestor.
674
+ */
675
+ class UnscopedSuspenseBoundary extends SuspenseBoundaryBase {
676
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: UnscopedSuspenseBoundary, deps: null, target: i0.ɵɵFactoryTarget.Component });
677
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.17", type: UnscopedSuspenseBoundary, isStandalone: true, selector: "mm-unscoped-suspense", host: { properties: { "attr.aria-busy": "pending() ? true : null" } }, usesInheritance: true, ngImport: i0, template: "\n @if (suspended()) {\n <ng-content select=\"[placeholder]\"><span>Loading\u2026</span></ng-content>\n } @else {\n @if (pending()) {\n <ng-content select=\"[busy]\" />\n }\n <ng-content />\n }\n", isInline: true, styles: [":host{display:contents}\n"] });
678
+ }
679
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.17", ngImport: i0, type: UnscopedSuspenseBoundary, decorators: [{
680
+ type: Component,
681
+ args: [{ selector: 'mm-unscoped-suspense', template: SUSPENSE_TEMPLATE, host: SUSPENSE_HOST, styles: [":host{display:contents}\n"] }]
682
+ }] });
683
+
684
+ function createTransaction() {
685
+ const log = new Map();
686
+ return {
687
+ record: (sig) => {
688
+ if (!log.has(sig))
689
+ log.set(sig, untracked(sig));
690
+ },
691
+ restore: () => untracked(() => {
692
+ for (const [sig, old] of log)
693
+ sig.set(old);
694
+ log.clear();
695
+ }),
696
+ clear: () => log.clear(),
697
+ };
698
+ }
699
+ // The currently-active transaction, set only for the synchronous duration of a `startTransaction`
700
+ // body (so stateful actions running inside it can record their writes). Module-level + sync
701
+ // set/reset is the honest shape: a transaction is call-scoped, not structural-per-injector.
702
+ let active = null;
703
+ /** The transaction in effect right now, or `null`. Stateful actions consult this to record undo. */
704
+ function activeTransaction() {
705
+ return active;
706
+ }
707
+ function runInTransaction(txn, fn) {
708
+ const prev = active;
709
+ active = txn;
710
+ try {
711
+ untracked(fn);
712
+ }
713
+ finally {
714
+ active = prev;
715
+ }
716
+ }
717
+ /**
718
+ * Returns a `startTransaction(fn)` bound to the nearest transition scope — the Tier 3 sibling of
719
+ * `injectStartTransition`. It HOLDS the scope's synchronous display reads from before `fn` runs
720
+ * (so a state write inside `fn` doesn't flash through), records those writes in an undo log, then:
721
+ * - on settle (the scope's resources go in flight and drain) → release the hold + keep the writes;
722
+ * - on `abort()` → roll the writes back and release the hold.
723
+ *
724
+ * The writes land on LIVE state immediately (so derived variables and connector requests see the
725
+ * new values and refetch); only the *display* is held, via `scope.hold`. Must run in an injection
726
+ * context.
727
+ */
728
+ function injectStartTransaction() {
729
+ const scope = injectTransitionScope();
730
+ const injector = inject(Injector);
731
+ return (fn) => {
732
+ const txn = createTransaction();
733
+ // Hold BEFORE the writes, so the display freezes at pre-transaction values.
734
+ scope.beginHold();
735
+ let finished = false;
736
+ let watcher;
737
+ const finish = (restore) => {
738
+ if (finished)
739
+ return;
740
+ finished = true;
741
+ watcher?.destroy();
742
+ if (restore)
743
+ txn.restore();
744
+ else
745
+ txn.clear();
746
+ scope.endHold();
747
+ };
748
+ runInTransaction(txn, fn);
749
+ let sawPending = false;
750
+ const done = new Promise((resolve) => {
751
+ watcher = effect(() => {
752
+ const p = scope.pending();
753
+ if (p)
754
+ sawPending = true;
755
+ if (sawPending && !p) {
756
+ finish(false);
757
+ resolve();
758
+ }
759
+ }, { injector });
760
+ // no-async fallback: if nothing ever went in flight, settle once the writes are processed.
761
+ afterNextRender(() => {
762
+ if (!sawPending && !untracked(scope.pending)) {
763
+ finish(false);
764
+ resolve();
765
+ }
766
+ }, { injector });
767
+ });
768
+ return {
769
+ pending: scope.pending,
770
+ done,
771
+ abort: () => finish(true),
772
+ };
773
+ };
774
+ }
775
+
219
776
  /**
220
777
  * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.
221
778
  * This can be useful for creating controlled write access to a signal that is otherwise read-only.
@@ -350,57 +907,6 @@ function debounce(source, opt) {
350
907
  return writable;
351
908
  }
352
909
 
353
- const { is } = Object;
354
- function mutable(initial, opt) {
355
- const baseEqual = opt?.equal ?? is;
356
- let cnt = 0;
357
- const equal = (a, b) => {
358
- if (cnt > 0)
359
- return false;
360
- return baseEqual(a, b);
361
- };
362
- const sig = signal(initial, {
363
- ...opt,
364
- equal,
365
- });
366
- const internalUpdate = sig.update;
367
- sig.mutate = (updater) => {
368
- cnt++;
369
- internalUpdate(updater);
370
- cnt--;
371
- };
372
- sig.inline = (updater) => {
373
- sig.mutate((prev) => {
374
- updater(prev);
375
- return prev;
376
- });
377
- };
378
- return sig;
379
- }
380
- /**
381
- * Type guard function to check if a given `WritableSignal` is a `MutableSignal`. This is useful
382
- * for situations where you need to conditionally use the `mutate` or `inline` methods.
383
- *
384
- * @typeParam T - The type of the signal's value (optional, defaults to `any`).
385
- * @param value - The `WritableSignal` to check.
386
- * @returns `true` if the signal is a `MutableSignal`, `false` otherwise.
387
- *
388
- * @example
389
- * const mySignal = signal(0);
390
- * const myMutableSignal = mutable(0);
391
- *
392
- * if (isMutable(mySignal)) {
393
- * mySignal.mutate(x => x + 1); // This would cause a type error, as mySignal is not a MutableSignal.
394
- * }
395
- *
396
- * if (isMutable(myMutableSignal)) {
397
- * myMutableSignal.mutate(x => x + 1); // This is safe.
398
- * }
399
- */
400
- function isMutable(value) {
401
- return 'mutate' in value && typeof value.mutate === 'function';
402
- }
403
-
404
910
  /**
405
911
  * @internal
406
912
  * Type guard for an array-index-like property key: a non-empty string that parses to a finite
@@ -650,6 +1156,29 @@ function createSetter(source) {
650
1156
  };
651
1157
  }
652
1158
 
1159
+ function keepPrevious(src, opt) {
1160
+ const persisted = linkedSignal(...(ngDevMode ? [{ debugName: "persisted", ...opt,
1161
+ source: () => src(),
1162
+ computation: (next, prev) => next === undefined && prev !== undefined ? prev.value : next }] : [{
1163
+ ...opt,
1164
+ source: () => src(),
1165
+ computation: (next, prev) => next === undefined && prev !== undefined ? prev.value : next,
1166
+ }]));
1167
+ if (isWritableSignal(src)) {
1168
+ persisted.set = src.set;
1169
+ persisted.update = src.update;
1170
+ persisted.asReadonly = src.asReadonly;
1171
+ if (isMutable(src)) {
1172
+ persisted.mutate = src.mutate;
1173
+ persisted.inline = src.inline;
1174
+ }
1175
+ if (isDerivation(src)) {
1176
+ persisted.from = src.from;
1177
+ }
1178
+ }
1179
+ return persisted;
1180
+ }
1181
+
653
1182
  /**
654
1183
  * Helper to create the derived signal for a specific index.
655
1184
  * Extracts the cast logic to keep the main loop clean.
@@ -2855,7 +3384,9 @@ function scopedStore(parent, seed, kind, injector) {
2855
3384
  layer[key].set(next[key]);
2856
3385
  }
2857
3386
  };
2858
- const base = toWritable(view, kind === 'readonly' ? () => undefined : splitSet, undefined, { pure: false });
3387
+ const base = toWritable(view, kind === 'readonly' ? () => undefined : splitSet, undefined, {
3388
+ pure: false,
3389
+ });
2859
3390
  if (kind === 'mutable') {
2860
3391
  base.mutate = (updater) => splitSet(updater(untracked(view)));
2861
3392
  base.inline = (updater) => base.mutate((prev) => {
@@ -2914,6 +3445,71 @@ function mutableStore(value, opt) {
2914
3445
  return toStore(mutable(value, opt), opt?.injector, opt?.vivify ?? false, opt?.noUnionLeaves ?? false);
2915
3446
  }
2916
3447
 
3448
+ function isPlainRecord(value) {
3449
+ if (value === null || typeof value !== 'object')
3450
+ return false;
3451
+ const proto = Object.getPrototypeOf(value);
3452
+ return proto === Object.prototype || proto === null;
3453
+ }
3454
+ /**
3455
+ * Per-path 3-way merge. Reference-equality short-circuits do the work: a subtree the fork never
3456
+ * touched satisfies `mine === ancestor` (structural sharing keeps its identity) → take the live
3457
+ * base; a subtree the base never changed satisfies `theirs === ancestor` → keep the fork's. So it
3458
+ * only deep-walks paths that BOTH sides changed, and on a leaf/array conflict the fork wins.
3459
+ * Arrays are treated atomically (no positional merge — index shifts make that unsafe); supply a
3460
+ * {@link ReconcileFn} for array-aware merging.
3461
+ *
3462
+ * CONTRACT: "unchanged" is detected by REFERENCE identity, not deep equality. `mine` must be a
3463
+ * copy-on-write derivative of `ancestor` — i.e. untouched nodes keep their reference — which the
3464
+ * fork guarantees because writes flow through `toStore` (it rebuilds only the edited path and
3465
+ * shares everything else). Feed it a structurally-equal-but-fresh-reference node for an untouched
3466
+ * path and it will treat that node as edited (recursion/leaf-value checks usually still reconcile,
3467
+ * but a fresh-ref clean node vs a base type-change resolves to the fork's stale value). Primitive
3468
+ * leaves compare by value, so equal primitives are correctly seen as unchanged.
3469
+ */
3470
+ function merge3(ancestor, mine, theirs) {
3471
+ if (Object.is(mine, theirs) || Object.is(mine, ancestor))
3472
+ return theirs; // unedited → live base
3473
+ if (Object.is(theirs, ancestor))
3474
+ return mine; // base unchanged here → keep the fork's edit
3475
+ if (isPlainRecord(mine) && isPlainRecord(theirs) && isPlainRecord(ancestor)) {
3476
+ const out = { ...theirs };
3477
+ for (const key of new Set([...Object.keys(mine), ...Object.keys(theirs)])) {
3478
+ out[key] = merge3(ancestor[key], mine[key], theirs[key]);
3479
+ }
3480
+ return out;
3481
+ }
3482
+ return mine; // leaf / array / type-mismatch conflict → local wins
3483
+ }
3484
+ function forkStore(base, opt) {
3485
+ // A mutable base mutates in place, so its value reference is stable across changes — which defeats merge3's identity-based change detection
3486
+ const mutableBase = typeof base.mutate === 'function';
3487
+ let strategy = opt?.strategy ?? (mutableBase ? 'coarse' : 'fine');
3488
+ if (mutableBase && strategy === 'fine') {
3489
+ if (isDevMode())
3490
+ console.warn("[fork] strategy 'fine' relies on reference-identity change detection, but the base is a " +
3491
+ "mutable store (in-place mutation keeps the same reference) — falling back to 'coarse'.");
3492
+ strategy = 'coarse';
3493
+ }
3494
+ const reconcile = strategy === 'coarse'
3495
+ ? (_ancestor, _mine, theirs) => theirs // re-link to the new base (whole-value reset)
3496
+ : strategy === 'fine'
3497
+ ? merge3
3498
+ : strategy;
3499
+ const merge = reconcile;
3500
+ const staged = linkedSignal(...(ngDevMode ? [{ debugName: "staged", source: () => base(),
3501
+ computation: (theirs, prev) => prev === undefined ? theirs : merge(prev.source, prev.value, theirs) }] : [{
3502
+ source: () => base(),
3503
+ computation: (theirs, prev) => prev === undefined ? theirs : merge(prev.source, prev.value, theirs),
3504
+ }]));
3505
+ const store = toStore(staged, opt?.injector, opt?.vivify, opt?.noUnionLeaves);
3506
+ return {
3507
+ store,
3508
+ commit: () => base.set(untracked(staged)),
3509
+ discard: () => staged.set(untracked(base)),
3510
+ };
3511
+ }
3512
+
2917
3513
  // Internal dummy store for server-side rendering
2918
3514
  const noopStore = {
2919
3515
  getItem: () => null,
@@ -3397,5 +3993,5 @@ function withHistory(sourceOrValue, opt) {
3397
3993
  * Generated bundle index. Do not edit.
3398
3994
  */
3399
3995
 
3400
- 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 };
3996
+ export { MmActivity, SuspenseBoundary, SuspenseBoundaryBase, UnscopedSuspenseBoundary, activeTransaction, batteryStatus, chunked, clipboard, combineWith, createTransaction, createTransitionScope, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, filterWith, focusWithin, forkStore, geolocation, holdUntilReady, idle, indexArray, injectPaused, injectRegisterResource, injectStartTransaction, injectStartTransition, injectTransitionScope, isDerivation, isLeaf, isMutable, isOpaque, isStore, keepPrevious, keyArray, map, mapArray, mapObject, mediaQuery, merge3, mousePosition, mutable, mutableStore, nestedEffect, networkStatus, opaque, orientation, pageVisibility, pairwise, pausableComputed, pausableEffect, pausableSignal, pipeable, piped, pooled, pooledArray, pooledMap, pooledSet, prefersDarkMode, prefersReducedMotion, providePaused, provideTransitionScope, registerResource, resolvePause, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
3401
3997
  //# sourceMappingURL=mmstack-primitives.mjs.map