-
Notifications
You must be signed in to change notification settings - Fork 27k
Rxjs interop updates #49769
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 updates #49769
Changes from all commits
eb0a738
704feca
50b4c4c
ec9d734
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,66 +7,73 @@ | |
| */ | ||
|
|
||
| import {assertInInjectionContext, computed, DestroyRef, inject, signal, Signal, WritableSignal} from '@angular/core'; | ||
| import {RuntimeError, RuntimeErrorCode} from '@angular/core/src/errors'; | ||
| import {Observable} from 'rxjs'; | ||
|
|
||
| import {untracked} from '../../src/signals'; | ||
|
|
||
| /** | ||
| * Get the current value of an `Observable` as a reactive `Signal`. | ||
| * | ||
| * `fromObservable` returns a `Signal` which provides synchronous reactive access to values produced | ||
| * `toSignal` returns a `Signal` which provides synchronous reactive access to values produced | ||
| * by the given `Observable`, by subscribing to that `Observable`. The returned `Signal` will always | ||
| * have the most recent value emitted by the subscription, and will throw an error if the | ||
| * `Observable` errors. | ||
| * | ||
| * The subscription will last for the lifetime of the current injection context. That is, if | ||
| * `fromObservable` is called from a component context, the subscription will be cleaned up when the | ||
| * `toSignal` is called from a component context, the subscription will be cleaned up when the | ||
| * component is destroyed. When called outside of a component, the current `EnvironmentInjector`'s | ||
| * lifetime will be used (which is typically the lifetime of the application itself). | ||
| * | ||
| * If the `Observable` does not produce a value before the `Signal` is read, the `Signal` will throw | ||
| * an error. To avoid this, use a synchronous `Observable` (potentially created with the `startWith` | ||
| * operator) or pass an initial value to `fromObservable` as the second argument. | ||
| * operator) or pass an initial value to `toSignal` as the second argument. | ||
| * | ||
| * `fromObservable` must be called in an injection context. | ||
| * `toSignal` must be called in an injection context. | ||
| */ | ||
| export function fromObservable<T>(source: Observable<T>): Signal<T>; | ||
| export function toSignal<T>(source: Observable<T>): Signal<T|undefined>; | ||
|
|
||
| /** | ||
| * Get the current value of an `Observable` as a reactive `Signal`. | ||
| * | ||
| * `fromObservable` returns a `Signal` which provides synchronous reactive access to values produced | ||
| * `toSignal` returns a `Signal` which provides synchronous reactive access to values produced | ||
| * by the given `Observable`, by subscribing to that `Observable`. The returned `Signal` will always | ||
| * have the most recent value emitted by the subscription, and will throw an error if the | ||
| * `Observable` errors. | ||
| * | ||
| * The subscription will last for the lifetime of the current injection context. That is, if | ||
| * `fromObservable` is called from a component context, the subscription will be cleaned up when the | ||
| * `toSignal` is called from a component context, the subscription will be cleaned up when the | ||
| * component is destroyed. When called outside of a component, the current `EnvironmentInjector`'s | ||
| * lifetime will be used (which is typically the lifetime of the application itself). | ||
| * | ||
| * Before the `Observable` emits its first value, the `Signal` will return the configured | ||
| * `initialValue`. If the `Observable` is known to produce a value before the `Signal` will be read, | ||
| * `initialValue` does not need to be passed. | ||
| * | ||
| * `fromObservable` must be called in an injection context. | ||
| * `toSignal` must be called in an injection context. | ||
| * | ||
| * @developerPreview | ||
| */ | ||
| export function fromObservable<T, U extends T|null|undefined>( | ||
| // fromObservable(Observable<Animal>) -> Signal<Cat> | ||
| source: Observable<T>, initialValue: U): Signal<T|U>; | ||
| export function fromObservable<T, U = never>(source: Observable<T>, initialValue?: U): Signal<T|U> { | ||
| assertInInjectionContext(fromObservable); | ||
| export function toSignal<T, U extends T|null|undefined>( | ||
| // toSignal(Observable<Animal>, {initialValue: null}) -> Signal<Animal|null> | ||
| source: Observable<T>, options: {initialValue: U, requireSync?: false}): Signal<T|U>; | ||
| export function toSignal<T>( | ||
| // toSignal(Observable<Animal>, {requireSync: true}) -> Signal<Animal> | ||
| source: Observable<T>, options: {requireSync: true}): Signal<T>; | ||
| // toSignal(Observable<Animal>) -> Signal<Animal|undefined> | ||
| export function toSignal<T, U = undefined>( | ||
| source: Observable<T>, options?: {initialValue?: U, requireSync?: boolean}): Signal<T|U> { | ||
|
||
| assertInInjectionContext(toSignal); | ||
|
|
||
| // Note: T is the Observable value type, and U is the initial value type. They don't have to be | ||
| // the same - the returned signal gives values of type `T`. | ||
| let state: WritableSignal<State<T|U>>; | ||
| if (initialValue === undefined && arguments.length !== 2) { | ||
| // No initial value was passed, so initially the signal is in a `NoValue` state and will throw | ||
| // if accessed. | ||
| if (options?.requireSync) { | ||
| // Initially the signal is in a `NoValue` state. | ||
| state = signal({kind: StateKind.NoValue}); | ||
| } else { | ||
| // An initial value was passed, so use it. | ||
| state = signal<State<T|U>>({kind: StateKind.Value, value: initialValue!}); | ||
| // If an initial value was passed, use it. Otherwise, use `undefined` as the initial value. | ||
| state = signal<State<T|U>>({kind: StateKind.Value, value: options?.initialValue as U}); | ||
| } | ||
|
|
||
| const sub = source.subscribe({ | ||
|
|
@@ -76,6 +83,12 @@ export function fromObservable<T, U = never>(source: Observable<T>, initialValue | |
| // "complete". | ||
| }); | ||
|
|
||
| if (ngDevMode && options?.requireSync && untracked(state).kind === StateKind.NoValue) { | ||
| throw new RuntimeError( | ||
| RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT, | ||
| '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.'); | ||
| } | ||
|
|
||
| // Unsubscribe when the current context is destroyed. | ||
| inject(DestroyRef).onDestroy(sub.unsubscribe.bind(sub)); | ||
|
|
||
|
|
@@ -89,8 +102,11 @@ export function fromObservable<T, U = never>(source: Observable<T>, initialValue | |
| case StateKind.Error: | ||
| throw current.error; | ||
| case StateKind.NoValue: | ||
| // This shouldn't really happen because the error is thrown on creation. | ||
| // TODO(alxhub): use a RuntimeError when we finalize the error semantics | ||
| throw new Error(`fromObservable() signal read before the Observable emitted`); | ||
| throw new RuntimeError( | ||
| RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT, | ||
| '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pascal