-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: enhance createNotifier to accept other signal deps #535
Comments
Hi @Harpush Thanks. |
@eneajaho Hey something like that: const signalA = signal(1);
const signalB = signal('test');
const notifier = createLinkedNotifier([signalA, signalB]);
effect(() => {
notifier.listen();
console.log('signalA changed or signalB changed or notified imperatively!');
});
signalA.set(5); // Notified!
// Later...
signalB.set('hello'); // Notified!
// Later...
notifier.notify(); // Notified! This will allow passing notifier as parameter and invoke a change either imperatively like now or through a signal that was passed to |
Hi @Harpush const notifier = createNotifier({deps: [signalA, signalB]}); This way, we won't have to create a new utility. What do you think? |
Sounds good - although angular 19 is needed first I think. |
We don't need v19 for this. The listen function can be: listen: () => {
if (deps) {
for (let dep of deps) dep();
}
return sourceSignal.asReadonly();
} |
I would expect each dependency change to increase the source signal. Linked signal fits here nicely. |
createNotifier
is great when you want to imperatively notify an effect or linked signal.The missing part is when you have multiple signals that when any gets changed you want to notify without using an effect.
It allows better encapsulation as you can pass the notifier signal as parameter without knowing why it changed (imperatively or by linked signals).
This allows for example a function with state that needs to get reset on certain scenarios to accept a reset notifier which will reset the state using a linked signal.
That notifier might be invoked by an outside signal or imperatively - but the function only knows it got notified.
Currently you need to pass a
Signal<unknown>
for reset and also add a reset function for imperative reset. Or create a notifier and computed for notifier and other signal and pass it.API could be:
createLinkedNotifier([signalA, signalB, signalC])
and you can still notify imperatively.The text was updated successfully, but these errors were encountered: