Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add createLinkedNotifier #535

Open
Harpush opened this issue Dec 21, 2024 · 2 comments
Open

Add createLinkedNotifier #535

Harpush opened this issue Dec 21, 2024 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@Harpush
Copy link

Harpush commented Dec 21, 2024

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.

@eneajaho
Copy link
Collaborator

eneajaho commented Jan 5, 2025

Hi @Harpush
How would you use this exactly?
Can you give a proper code example on how it would look like.

Thanks.

@eneajaho eneajaho added enhancement New feature or request question Further information is requested labels Jan 5, 2025
@Harpush
Copy link
Author

Harpush commented Jan 10, 2025

@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 createLinkedNotifier.
I can provide initial implementation too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants
@eneajaho @Harpush and others