@mmstack/primitives 20.4.6 → 21.0.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.
package/README.md CHANGED
@@ -22,6 +22,7 @@ This library provides the following primitives:
22
22
  - `piped` – Creates a signal with a chainable & typesafe `.pipe(...)` method, which returns a pipable computed.
23
23
  - `withHistory` - Enhances a signal with a complete undo/redo history stack.
24
24
  - `mapArray` - Maps a reactive array efficently into an array of stable derivations.
25
+ - `nestedEffect` - Creates an effect with a hierarchical lifetime, enabling fine-grained, conditional side-effects.
25
26
  - `toWritable` - Converts a read-only signal to writable using custom write logic.
26
27
  - `derived` - Creates a signal with two-way binding to a source signal.
27
28
  - `sensor` - A facade function to create various reactive sensor signals (e.g., mouse position, network status, page visibility, dark mode preference)." (This was the suggestion from before; it just reads a little smoother and more accurately reflects what the facade creates directly).
@@ -318,6 +319,95 @@ export class ListComponent {
318
319
  }
319
320
  ```
320
321
 
322
+ ### nestedEffect
323
+
324
+ Creates an effect that can be nested, similar to SolidJS's `createEffect`.
325
+
326
+ This primitive enables true hierarchical reactivity. A `nestedEffect` created within another `nestedEffect` is **automatically destroyed and recreated** when the parent re-runs.
327
+
328
+ It automatically handles injector propagation and lifetime management, allowing you to create fine-grained, conditional side-effects that only track dependencies when they are "live". This is a powerful optimization for scenarios where a "hot" signal (which changes often) should only be tracked when a "cold" signal (a condition that changes rarely) is true.
329
+
330
+ ```ts
331
+ import { Component, signal } from '@angular/core';
332
+ import { nestedEffect } from '@mmstack/primitives';
333
+
334
+ @Component({ selector: 'app-nested-demo' })
335
+ export class NestedDemoComponent {
336
+ // `coldGuard` changes rarely
337
+ readonly coldGuard = signal(false);
338
+ // `hotSignal` changes very often
339
+ readonly hotSignal = signal(0);
340
+
341
+ constructor() {
342
+ // A standard effect would track *both* signals and run
343
+ // every time `hotSignal` changes, even if `coldGuard` is false.
344
+ // effect(() => {
345
+ // if (this.coldGuard()) {
346
+ // console.log('Hot signal is:', this.hotSignal());
347
+ // }
348
+ // });
349
+
350
+ // `nestedEffect` solves this:
351
+ nestedEffect(() => {
352
+ // This outer effect ONLY tracks `coldGuard`.
353
+ // It does not track `hotSignal`.
354
+ if (this.coldGuard()) {
355
+ // This inner effect is CREATED when coldGuard is true
356
+ // and DESTROYED when it becomes false.
357
+ nestedEffect(() => {
358
+ // It only tracks `hotSignal` while it exists.
359
+ console.log('Hot signal is:', this.hotSignal());
360
+ });
361
+ }
362
+ });
363
+ }
364
+ }
365
+ ```
366
+
367
+ #### Advanced Example: Fine-grained Lists
368
+
369
+ `nestedEffect` can be composed with `mapArray` to create truly fine-grained reactive lists, where each item can manage its own side-effects (like external library integrations) that are automatically cleaned up when the item is removed.
370
+
371
+ ```ts
372
+ import { Component, signal, computed } from '@angular/core';
373
+ import { mapArray, nestedEffect } from '@mmstack/primitives';
374
+
375
+ @Component({ selector: 'app-list-demo' })
376
+ export class ListDemoComponent {
377
+ readonly users = signal([
378
+ { id: 1, name: 'Alice' },
379
+ { id: 2, name: 'Bob' },
380
+ ]);
381
+
382
+ // mapArray creates stable signals for each item
383
+ readonly mappedUsers = mapArray(
384
+ this.users,
385
+ (userSignal, index) => {
386
+ // Create a side-effect tied to THIS item's lifetime
387
+ const effectRef = nestedEffect(() => {
388
+ // This only runs if `userSignal` (this specific user) changes.
389
+ console.log(`User ${index} updated:`, userSignal().name);
390
+
391
+ // e.g., updateAGGridRow(index, userSignal());
392
+ });
393
+
394
+ // Return the data and the cleanup logic
395
+ return {
396
+ label: computed(() => `User: ${userSignal().name}`),
397
+ // This function will be called by `onDestroy`
398
+ _destroy: () => effectRef.destroy(),
399
+ };
400
+ },
401
+ {
402
+ // When mapArray removes an item, it calls `onDestroy`
403
+ onDestroy: (mappedItem) => {
404
+ mappedItem._destroy(); // Manually destroy the nested effect
405
+ },
406
+ },
407
+ );
408
+ }
409
+ ```
410
+
321
411
  ### toWritable
322
412
 
323
413
  A utility function that converts a read-only Signal into a WritableSignal by allowing you to provide custom implementations for the .set() and .update() methods. This is useful for creating controlled write access to signals that are naturally read-only (like those created by computed). This is used under the hood in derived.
@@ -1457,10 +1457,10 @@ class MessageBus {
1457
1457
  this.channel.removeEventListener('message', listener);
1458
1458
  this.listeners.delete(id);
1459
1459
  }
1460
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.1", ngImport: i0, type: MessageBus, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
1461
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.1", ngImport: i0, type: MessageBus, providedIn: 'root' });
1460
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: MessageBus, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
1461
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: MessageBus, providedIn: 'root' });
1462
1462
  }
1463
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.1", ngImport: i0, type: MessageBus, decorators: [{
1463
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.0", ngImport: i0, type: MessageBus, decorators: [{
1464
1464
  type: Injectable,
1465
1465
  args: [{
1466
1466
  providedIn: 'root',