Skip to content
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

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

feat: enhance createNotifier to accept other signal deps #535

Harpush opened this issue Dec 21, 2024 · 6 comments
Assignees
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.

@eneajaho
Copy link
Collaborator

Hi @Harpush
We can enhance createNotifier.

const notifier = createNotifier({deps: [signalA, signalB]});

This way, we won't have to create a new utility.

What do you think?

@eneajaho eneajaho changed the title Add createLinkedNotifier feat: enhance createNotifier to accept other signal deps Jan 28, 2025
@Harpush
Copy link
Author

Harpush commented Jan 28, 2025

Hi @Harpush We can enhance createNotifier.

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.

@eneajaho
Copy link
Collaborator

We don't need v19 for this.

The listen function can be:

listen: () => {
  if (deps) {
    for (let dep of deps) dep();
  }

  return sourceSignal.asReadonly();
}

@Harpush
Copy link
Author

Harpush commented Jan 28, 2025

I would expect each dependency change to increase the source signal. Linked signal fits here nicely.
I guess pre 19 we can emulate linked signal.

# for free to join this conversation on GitHub. Already have an account? # 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