createNotifier
Features
Section titled “Features”createNotifier provides a way to manually trigger signal re-computations by referencing the created notifier signal. It can also automatically react to changes in other signals through dependency tracking.
Common Use Cases
Section titled “Common Use Cases”- Manually triggering a “refresh” and reacting to it
- Mutating a
Mapand having a way to react to that map changing - Triggering a re-computation on some kind of event/life cycle hook
- Combining manual notifications with automatic dependency tracking
Basic Usage
Section titled “Basic Usage”Create a simple notifier:
import { createNotifier } from 'ngxtension/create-notifier';refreshNotifier = createNotifier();Trigger the signal update:
refreshNotifier.notify();React to notifications in computed or effect:
effect(() => { refreshNotifier.listen();
// whatever code you want to run whenever // refreshNotifier.notify() is called});Avoiding Initial Effect Execution
Section titled “Avoiding Initial Effect Execution”An effect runs once initially before notify() is explicitly called. Since the internal counter begins at 0, you can skip the initial run:
effect(() => { if (refreshNotifier.listen()) { // Will NOT run on init // whatever code you want to run whenever // refreshNotifier.notify() is called }});The if condition initially fails because refreshNotifier.listen() returns 0, but passes once notify() has been called.
Dependency Tracking
Section titled “Dependency Tracking”You can configure a notifier to automatically increment whenever specified signals change. This combines manual notifications with reactive dependency tracking.
Basic Dependency Tracking
Section titled “Basic Dependency Tracking”userId = signal(1);
userNotifier = createNotifier({ deps: [userId] });Now userNotifier.listen() will increment both when notify() is called and when userId changes:
effect(() => { console.log('User notifier changed:', userNotifier.listen()); // Runs when userId changes OR when notify() is called});
// Both of these will trigger the effect:userId.set(2); // Triggers via dependencyuserNotifier.notify(); // Triggers manuallyMultiple Dependencies
Section titled “Multiple Dependencies”Track multiple signals simultaneously:
userId = signal(1);tenantId = signal('tenant-a');
compositeNotifier = createNotifier({ deps: [userId, tenantId],});The notifier increments whenever any of the dependencies change.
Controlling Initial Emission
Section titled “Controlling Initial Emission”By default, notifiers with dependencies start at 1 (emitting immediately). Control this with depsEmitInitially:
// Emit immediately (default behavior)notifier = createNotifier({ deps: [someSignal], depsEmitInitially: true, // starts at 1 (default)});
// Don't emit initiallynotifier = createNotifier({ deps: [someSignal], depsEmitInitially: false, // starts at 0});When depsEmitInitially: false, the notifier starts at 0 like a dependency-free notifier, even though it tracks signals. The first increment happens only when dependencies change or notify() is called.