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

signal(Observable<T>) to signal<T>() operator #55675

Open
MGX-CODING opened this issue May 4, 2024 · 5 comments
Open

signal(Observable<T>) to signal<T>() operator #55675

MGX-CODING opened this issue May 4, 2024 · 5 comments
Labels
area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework cross-cutting: signals feature Issue that requests a new feature
Milestone

Comments

@MGX-CODING
Copy link

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

Here is a quick example of the requested feature (of course it has no value in itself) :

  $control = signal(new FormControl(''));

  $valueChanges$ = computed(() => this.$control().valueChanges);

  valueChanges$ = toObservable(this.$valueChanges$).pipe(switchMap((v) => v));

  $valueChanges = toSignal(this.valueChanges$);

STACKBLITZ

Proposed solution

  $control = signal(new FormControl(''));
  $valueChanges = transmuted(() => this.$control().valueChanges);

Alternatives considered

The code of the example itself.

This is not the first time I have the need for something like this, and as far as I'm aware, there is no easy way to do something similar (please correct me if I'm wrong)

@jnizet
Copy link
Contributor

jnizet commented May 4, 2024

I personally find using toSignal() and toObservable() perfectly reasonable here since you precisely want to transform a signal to an observable and vice-versa.

The code could be simplified to

const control = signal(new FormControl(''));
const value = toSignal(toObservable(control).pipe(switchMap(c => c.valueChanges)));

@MGX-CODING
Copy link
Author

I personally find using toSignal() and toObservable() perfectly reasonable here since you precisely want to transform a signal to an observable and vice-versa.

The code could be simplified to

const control = signal(new FormControl(''));
const value = toSignal(toObservable(control).pipe(switchMap(c => c.valueChanges)));

It is indeed clearer, thank you for that, but still, don't you think it looks complicated ?

@jnizet
Copy link
Contributor

jnizet commented May 4, 2024

No. It's very explicit.

@MGX-CODING
Copy link
Author

Well on that we agree, maybe I should have said concise then ... Can this issue remain open, just to have more opinions ?

(Thanks again for the more concise way !)

@alxhub alxhub added feature Issue that requests a new feature area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework labels May 5, 2024
@ngbot ngbot bot added this to the Backlog milestone May 5, 2024
@kbrilla
Copy link

kbrilla commented May 7, 2024

@MGX-CODING mayby this will cover Your needs https://ngxtension.netlify.app/utilities/signals/derived-async/

it will look like this:

  $control = signal(new FormControl(''));
  $valueChanges = derivedAsync(() => $control().valueChanges);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime core: reactivity Work related to fine-grained reactivity in the core framework cross-cutting: signals feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

5 participants