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

rxjs-interop: bidirectional interop #55072

Open
krulod opened this issue Mar 27, 2024 · 2 comments
Open

rxjs-interop: bidirectional interop #55072

krulod opened this issue Mar 27, 2024 · 2 comments
Labels
area: core Issues related to the framework runtime core: rxjs interop cross-cutting: signals feature Issue that requests a new feature
Milestone

Comments

@krulod
Copy link

krulod commented Mar 27, 2024

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

core

Description

Currently, toSignal and toObservable functions always return read-only primitives - Signal and Observable respectively. It would be really handy if passing a writable primitive to interop functions created a writable counterpart of passed value.

Examples:

const someSignal = signal(1)
effect(() => {
  const value = someSignal()
  console.log('signal changed:', value)
})

const someSubject = toObservable() // returned a Subject!
someSubject.next(2) // signal changed: 2
const someSubject = new Subject(1)
someSubject.subscribe(value => console.log('subject changed:', value)

const someSignal = toSignal(someSubject) // returned a WritableSignal!
someSignal.set(2) // => subject changed: 2
someSignal.update(prev => prev * 2) // => subject changed: 4

Proposed solution

Return a WritableSignal from toSignal if argument is next-able. Whenever a value is written to the signal manually, push it to the target observable.

In toObservable, return a subject if argument is a WritableSignal. Whenever a value is pushed to the subject manually, write it to the target signal.

Alternatives considered

Leave everything as is.

@alxhub
Copy link
Member

alxhub commented Mar 27, 2024

This is a neat idea. Can you elaborate on use cases you've encountered where this API could be beneficial?

@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 cross-cutting: signals labels Mar 27, 2024
@ngbot ngbot bot modified the milestone: Backlog Mar 27, 2024
@alxhub alxhub added core: rxjs interop and removed core: reactivity Work related to fine-grained reactivity in the core framework labels Mar 27, 2024
@krulod
Copy link
Author

krulod commented Mar 27, 2024

My specific use case is creation of an adapter between Angular Signals and Reatom, a signals-like library. In a similiar adapter for Vue which is already available, you can see that it creates a writable Ref-like from writable Reatom atoms.

If there is a hypothetical reatomToObservable which creates Subject-s from AtomMut-s and Observable-s from Atom-s, the discussed feature makes implementation of reatomToSignal really trivial: target => toSignal(reatomToObservable(target)).

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: rxjs interop cross-cutting: signals feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

2 participants