@mmstack/primitives 20.4.5 → 20.4.6
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/fesm2022/mmstack-primitives.mjs +137 -2
- package/fesm2022/mmstack-primitives.mjs.map +1 -1
- package/index.d.ts +78 -2
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { untracked, signal, inject, DestroyRef, computed, PLATFORM_ID, isSignal, effect, ElementRef, linkedSignal, isDevMode,
|
|
2
|
+
import { untracked, signal, inject, DestroyRef, computed, PLATFORM_ID, isSignal, effect, ElementRef, linkedSignal, isDevMode, Injector, Injectable, runInInjectionContext } from '@angular/core';
|
|
3
3
|
import { isPlatformServer } from '@angular/common';
|
|
4
4
|
import { SIGNAL } from '@angular/core/primitives/signals';
|
|
5
5
|
|
|
@@ -478,6 +478,141 @@ function mapArray(source, map, options) {
|
|
|
478
478
|
});
|
|
479
479
|
}
|
|
480
480
|
|
|
481
|
+
const frameStack = [];
|
|
482
|
+
function current() {
|
|
483
|
+
return frameStack.at(-1) ?? null;
|
|
484
|
+
}
|
|
485
|
+
function clearFrame(frame, userCleanups) {
|
|
486
|
+
for (const child of frame.children) {
|
|
487
|
+
try {
|
|
488
|
+
child.destroy();
|
|
489
|
+
}
|
|
490
|
+
catch (e) {
|
|
491
|
+
if (isDevMode())
|
|
492
|
+
console.error('Error destroying nested effect:', e);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
frame.children.clear();
|
|
496
|
+
for (const fn of userCleanups) {
|
|
497
|
+
try {
|
|
498
|
+
fn();
|
|
499
|
+
}
|
|
500
|
+
catch (e) {
|
|
501
|
+
if (isDevMode())
|
|
502
|
+
console.error('Error destroying nested effect:', e);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
userCleanups.length = 0;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Creates an effect that can be nested, similar to SolidJS's `createEffect`.
|
|
509
|
+
*
|
|
510
|
+
* This primitive enables true hierarchical reactivity. A `nestedEffect` created
|
|
511
|
+
* within another `nestedEffect` is automatically destroyed and recreated when
|
|
512
|
+
* the parent re-runs.
|
|
513
|
+
*
|
|
514
|
+
* It automatically handles injector propagation and lifetime management, allowing
|
|
515
|
+
* you to create fine-grained, conditional side-effects that only track
|
|
516
|
+
* dependencies when they are "live".
|
|
517
|
+
*
|
|
518
|
+
* @param effectFn The side-effect function, which receives a cleanup register function.
|
|
519
|
+
* @param options (Optional) Angular's `CreateEffectOptions`.
|
|
520
|
+
* @returns An `EffectRef` for the created effect.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```ts
|
|
524
|
+
* // Assume `coldGuard` changes rarely, but `hotSignal` changes often.
|
|
525
|
+
* const coldGuard = signal(false);
|
|
526
|
+
* const hotSignal = signal(0);
|
|
527
|
+
*
|
|
528
|
+
* nestedEffect(() => {
|
|
529
|
+
* // This outer effect only tracks `coldGuard`.
|
|
530
|
+
* if (coldGuard()) {
|
|
531
|
+
*
|
|
532
|
+
* // This inner effect is CREATED when coldGuard is true
|
|
533
|
+
* // and DESTROYED when it becomes false.
|
|
534
|
+
* nestedEffect(() => {
|
|
535
|
+
* // It only tracks `hotSignal` while it exists.
|
|
536
|
+
* console.log('Hot signal is:', hotSignal());
|
|
537
|
+
* });
|
|
538
|
+
* }
|
|
539
|
+
* // If `coldGuard` is false, this outer effect does not track `hotSignal`.
|
|
540
|
+
* });
|
|
541
|
+
* ```
|
|
542
|
+
* @example
|
|
543
|
+
* ```ts
|
|
544
|
+
* const users = signal([
|
|
545
|
+
{ id: 1, name: 'Alice' },
|
|
546
|
+
{ id: 2, name: 'Bob' }
|
|
547
|
+
]);
|
|
548
|
+
|
|
549
|
+
// The fine-grained mapped list
|
|
550
|
+
const mappedUsers = mapArray(
|
|
551
|
+
users,
|
|
552
|
+
(userSignal, index) => {
|
|
553
|
+
// 1. Create a fine-grained SIDE EFFECT for *this item*
|
|
554
|
+
// This effect's lifetime is now tied to this specific item. created once on init of this index.
|
|
555
|
+
const effectRef = nestedEffect(() => {
|
|
556
|
+
// This only runs if *this* userSignal changes,
|
|
557
|
+
// not if the whole list changes.
|
|
558
|
+
console.log(`User ${index} updated:`, userSignal().name);
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// 2. Return the data AND the cleanup logic
|
|
562
|
+
return {
|
|
563
|
+
// The mapped data
|
|
564
|
+
label: computed(() => `User: ${userSignal().name}`),
|
|
565
|
+
|
|
566
|
+
// The cleanup function
|
|
567
|
+
destroyEffect: () => effectRef.destroy()
|
|
568
|
+
};
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
// 3. Tell mapArray HOW to clean up when an item is removed, this needs to be manual as it's not a nestedEffect itself
|
|
572
|
+
onDestroy: (mappedItem) => {
|
|
573
|
+
mappedItem.destroyEffect();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
);
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
function nestedEffect(effectFn, options) {
|
|
580
|
+
const parent = current();
|
|
581
|
+
const injector = options?.injector ?? parent?.injector ?? inject(Injector);
|
|
582
|
+
const srcRef = untracked(() => {
|
|
583
|
+
return effect((cleanup) => {
|
|
584
|
+
const frame = {
|
|
585
|
+
injector,
|
|
586
|
+
children: new Set(),
|
|
587
|
+
};
|
|
588
|
+
const userCleanups = [];
|
|
589
|
+
frameStack.push(frame);
|
|
590
|
+
try {
|
|
591
|
+
effectFn((fn) => {
|
|
592
|
+
userCleanups.push(fn);
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
finally {
|
|
596
|
+
frameStack.pop();
|
|
597
|
+
}
|
|
598
|
+
return cleanup(() => clearFrame(frame, userCleanups));
|
|
599
|
+
}, {
|
|
600
|
+
...options,
|
|
601
|
+
injector,
|
|
602
|
+
manualCleanup: !!parent,
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
const ref = {
|
|
606
|
+
...srcRef,
|
|
607
|
+
destroy: () => {
|
|
608
|
+
parent?.children.delete(ref);
|
|
609
|
+
srcRef.destroy();
|
|
610
|
+
},
|
|
611
|
+
};
|
|
612
|
+
parent?.children.add(ref);
|
|
613
|
+
return ref;
|
|
614
|
+
}
|
|
615
|
+
|
|
481
616
|
/** Project with optional equality. Pure & sync. */
|
|
482
617
|
const select = (projector, opt) => (src) => computed(() => projector(src()), opt);
|
|
483
618
|
/** Combine with another signal using a projector. */
|
|
@@ -1608,5 +1743,5 @@ function withHistory(source, opt) {
|
|
|
1608
1743
|
* Generated bundle index. Do not edit.
|
|
1609
1744
|
*/
|
|
1610
1745
|
|
|
1611
|
-
export { combineWith, debounce, debounced, derived, distinct, elementVisibility, filter, isDerivation, isMutable, map, mapArray, mediaQuery, mousePosition, mutable, networkStatus, pageVisibility, pipeable, piped, prefersDarkMode, prefersReducedMotion, scrollPosition, select, sensor, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toWritable, until, windowSize, withHistory };
|
|
1746
|
+
export { combineWith, debounce, debounced, derived, distinct, elementVisibility, filter, isDerivation, isMutable, map, mapArray, mediaQuery, mousePosition, mutable, nestedEffect, networkStatus, pageVisibility, pipeable, piped, prefersDarkMode, prefersReducedMotion, scrollPosition, select, sensor, stored, tabSync, tap, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toWritable, until, windowSize, withHistory };
|
|
1612
1747
|
//# sourceMappingURL=mmstack-primitives.mjs.map
|