@mmstack/primitives 21.1.2 → 21.2.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.
package/README.md
CHANGED
|
@@ -418,6 +418,8 @@ export class UserProfile {
|
|
|
418
418
|
|
|
419
419
|
This is also the pattern for coordinating resources registered _above_ a boundary (e.g. an app-builder page whose connectors register at a higher injector): the outer `provideTransitionScope()` is the shared scope, and any number of `<mm-unscoped-suspense>` boundaries observe it.
|
|
420
420
|
|
|
421
|
+
**Forwarding scope (advanced).** `provideForwardingTransitionScope()` provides a scope that can be **re-pointed at a different target at runtime** via `setTarget(scope | null)` — reads follow the current target, while `add`/`remove` pin to the target a resource was registered under (so re-pointing never strands a registration). It's the building block for a coordinator that hosts several independent sub-scopes and switches which one it observes — e.g. a router outlet that, per navigation, points at the incoming route's own scope (read it from any injector with `getTransitionScope(injector)`). Most apps reach for `provideTransitionScope()`; this is for that one extra level of control.
|
|
422
|
+
|
|
421
423
|
### `injectStartTransition`
|
|
422
424
|
|
|
423
425
|
The analog of React's `useTransition`. `startTransition(fn)` runs your state mutations (which commit immediately); any resource that reloads as a result **holds its value and reveals together once everything settles** — so a multi-resource update lands as one consistent frame instead of a torn mix of new and stale. The returned handle gives you a unified `pending` signal and a `done` promise for imperative coordination (disable a button, await completion).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { isDevMode, inject, Injector, untracked, effect, DestroyRef, linkedSignal, InjectionToken, TemplateRef, ViewContainerRef, input, computed, Directive, signal,
|
|
2
|
+
import { isDevMode, inject, Injector, untracked, effect, DestroyRef, linkedSignal, InjectionToken, TemplateRef, ViewContainerRef, PLATFORM_ID, input, computed, Directive, signal, runInInjectionContext, afterNextRender, Component, isWritableSignal as isWritableSignal$2, isSignal, ElementRef, Injectable } from '@angular/core';
|
|
3
3
|
import { isPlatformServer } from '@angular/common';
|
|
4
4
|
import { SIGNAL } from '@angular/core/primitives/signals';
|
|
5
5
|
|
|
@@ -80,7 +80,7 @@ function popFrame() {
|
|
|
80
80
|
* ]);
|
|
81
81
|
*
|
|
82
82
|
* // The fine-grained mapped list
|
|
83
|
-
* const mappedUsers =
|
|
83
|
+
* const mappedUsers = indexArray(
|
|
84
84
|
* users,
|
|
85
85
|
* (userSignal, index) => {
|
|
86
86
|
* // 1. Create a fine-grained SIDE EFFECT for *this item*
|
|
@@ -101,7 +101,7 @@ function popFrame() {
|
|
|
101
101
|
* };
|
|
102
102
|
* },
|
|
103
103
|
* {
|
|
104
|
-
* // 3. Tell
|
|
104
|
+
* // 3. Tell indexArray HOW to clean up when an item is removed, this needs to be manual as it's not a nestedEffect itself
|
|
105
105
|
* onDestroy: (mappedItem) => {
|
|
106
106
|
* mappedItem.destroyEffect();
|
|
107
107
|
* }
|
|
@@ -246,6 +246,7 @@ class MmActivity {
|
|
|
246
246
|
tpl = inject(TemplateRef);
|
|
247
247
|
vcr = inject(ViewContainerRef);
|
|
248
248
|
parent = inject(Injector);
|
|
249
|
+
onServer = isPlatformServer(inject(PLATFORM_ID, { optional: true }) ?? 'browser');
|
|
249
250
|
/** When false, keep the content mounted but hidden + CD-detached. */
|
|
250
251
|
visible = input.required({ ...(ngDevMode ? { debugName: "visible" } : /* istanbul ignore next */ {}), alias: 'mmActivity' });
|
|
251
252
|
/** Paused == not visible — handed to the kept subtree as PAUSED_CONTEXT. */
|
|
@@ -268,6 +269,8 @@ class MmActivity {
|
|
|
268
269
|
}),
|
|
269
270
|
});
|
|
270
271
|
}
|
|
272
|
+
if (this.onServer)
|
|
273
|
+
return;
|
|
271
274
|
for (const node of this.view.rootNodes) {
|
|
272
275
|
// covers HTML and SVG roots; text/comment roots can't be styled — their CD is still
|
|
273
276
|
// detached, but prefer an element root for true visual hiding
|
|
@@ -561,6 +564,47 @@ function injectTransitionScope() {
|
|
|
561
564
|
}
|
|
562
565
|
return scope;
|
|
563
566
|
}
|
|
567
|
+
function createForwardingScope() {
|
|
568
|
+
const own = createTransitionScope();
|
|
569
|
+
const target = signal(null, ...(ngDevMode ? [{ debugName: "target" }] : /* istanbul ignore next */ []));
|
|
570
|
+
const eff = () => target() ?? own;
|
|
571
|
+
const owners = new Map();
|
|
572
|
+
return {
|
|
573
|
+
setTarget: (t) => target.set(t),
|
|
574
|
+
resources: computed(() => eff().resources()),
|
|
575
|
+
pending: computed(() => eff().pending()),
|
|
576
|
+
suspended: (type) => eff().suspended(type),
|
|
577
|
+
add: (ref, opt) => {
|
|
578
|
+
const t = untracked(target) ?? own;
|
|
579
|
+
owners.set(ref, t);
|
|
580
|
+
t.add(ref, opt);
|
|
581
|
+
},
|
|
582
|
+
remove: (ref) => {
|
|
583
|
+
const t = owners.get(ref) ?? untracked(target) ?? own;
|
|
584
|
+
t.remove(ref);
|
|
585
|
+
owners.delete(ref);
|
|
586
|
+
},
|
|
587
|
+
commit: (value) => linkedSignal({
|
|
588
|
+
source: () => ({ v: value(), settled: !eff().pending() }),
|
|
589
|
+
computation: (curr, prev) => curr.settled || prev === undefined ? curr.v : prev.value,
|
|
590
|
+
}),
|
|
591
|
+
holding: computed(() => eff().holding()),
|
|
592
|
+
beginHold: () => (untracked(target) ?? own).beginHold(),
|
|
593
|
+
endHold: () => (untracked(target) ?? own).endHold(),
|
|
594
|
+
hold: (value) => linkedSignal({
|
|
595
|
+
source: () => ({ v: value(), held: eff().holding() }),
|
|
596
|
+
computation: (curr, prev) => prev !== undefined && curr.held ? prev.value : curr.v,
|
|
597
|
+
}),
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
/** Provide a forwarding transition scope at a boundary (used by the transition outlet). */
|
|
601
|
+
function provideForwardingTransitionScope() {
|
|
602
|
+
return { provide: TRANSITION_SCOPE, useFactory: createForwardingScope };
|
|
603
|
+
}
|
|
604
|
+
/** Read the transition scope reachable from `injector`, or null if none is provided there. */
|
|
605
|
+
function getTransitionScope(injector) {
|
|
606
|
+
return injector.get(TRANSITION_SCOPE, null);
|
|
607
|
+
}
|
|
564
608
|
/**
|
|
565
609
|
* Returns a register function bound to the nearest transition scope: it adds a resource
|
|
566
610
|
* to the scope and removes it when the caller's injection context is destroyed. Pass any
|
|
@@ -598,6 +642,7 @@ function registerResource(res, opt) {
|
|
|
598
642
|
function injectStartTransition() {
|
|
599
643
|
const scope = injectTransitionScope();
|
|
600
644
|
const injector = inject(Injector);
|
|
645
|
+
const onServer = isPlatformServer(inject(PLATFORM_ID, { optional: true }) ?? 'browser');
|
|
601
646
|
return (fn) => {
|
|
602
647
|
untracked(fn);
|
|
603
648
|
let sawPending = false;
|
|
@@ -612,6 +657,13 @@ function injectStartTransition() {
|
|
|
612
657
|
resolve();
|
|
613
658
|
}
|
|
614
659
|
}, { ...(ngDevMode ? { debugName: "watcher" } : /* istanbul ignore next */ {}), injector });
|
|
660
|
+
if (onServer) {
|
|
661
|
+
if (!untracked(scope.pending)) {
|
|
662
|
+
watcher.destroy();
|
|
663
|
+
resolve();
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
615
667
|
// no-async fallback: once the reactive system has processed the writes (afterNextRender),
|
|
616
668
|
// if nothing ever went in flight, the transition is already complete.
|
|
617
669
|
afterNextRender(() => {
|
|
@@ -752,6 +804,7 @@ function runInTransaction(txn, fn) {
|
|
|
752
804
|
function injectStartTransaction() {
|
|
753
805
|
const scope = injectTransitionScope();
|
|
754
806
|
const injector = inject(Injector);
|
|
807
|
+
const onServer = isPlatformServer(inject(PLATFORM_ID, { optional: true }) ?? 'browser');
|
|
755
808
|
return (fn) => {
|
|
756
809
|
const txn = createTransaction();
|
|
757
810
|
// Hold BEFORE the writes, so the display freezes at pre-transaction values.
|
|
@@ -793,11 +846,17 @@ function injectStartTransaction() {
|
|
|
793
846
|
if (sawPending && !p)
|
|
794
847
|
finish(false);
|
|
795
848
|
}, { injector });
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
if (!sawPending && !untracked(scope.pending))
|
|
849
|
+
if (onServer) {
|
|
850
|
+
if (!untracked(scope.pending))
|
|
799
851
|
finish(false);
|
|
800
|
-
}
|
|
852
|
+
}
|
|
853
|
+
else {
|
|
854
|
+
// no-async fallback: if nothing ever went in flight, settle once the writes are processed.
|
|
855
|
+
afterNextRender(() => {
|
|
856
|
+
if (!sawPending && !untracked(scope.pending))
|
|
857
|
+
finish(false);
|
|
858
|
+
}, { injector });
|
|
859
|
+
}
|
|
801
860
|
return {
|
|
802
861
|
pending: scope.pending,
|
|
803
862
|
done,
|
|
@@ -4247,5 +4306,5 @@ function withHistory(sourceOrValue, opt) {
|
|
|
4247
4306
|
* Generated bundle index. Do not edit.
|
|
4248
4307
|
*/
|
|
4249
4308
|
|
|
4250
|
-
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 };
|
|
4309
|
+
export { MmActivity, SuspenseBoundary, SuspenseBoundaryBase, UnscopedSuspenseBoundary, activeTransaction, batteryStatus, chunked, clipboard, combineWith, createForwardingScope, createTransaction, createTransitionScope, debounce, debounced, derived, distinct, elementSize, elementVisibility, filter, filterWith, focusWithin, forkStore, geolocation, getTransitionScope, 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, provideForwardingTransitionScope, providePaused, provideTransitionScope, registerResource, resolvePause, scan, scrollPosition, select, sensor, sensors, signalFromEvent, startWith, store, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toStore, toWritable, until, windowSize, withHistory };
|
|
4251
4310
|
//# sourceMappingURL=mmstack-primitives.mjs.map
|