|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {assertInInjectionContext, computed, DestroyRef, inject, signal, Signal, WritableSignal} from '@angular/core'; |
| 10 | +import {Observable} from 'rxjs'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Get the current value of an `Observable` as a reactive `Signal`. |
| 14 | + * |
| 15 | + * `fromObservable` returns a `Signal` which provides synchronous reactive access to values produced |
| 16 | + * by the given `Observable`, by subscribing to that `Observable`. The returned `Signal` will always |
| 17 | + * have the most recent value emitted by the subscription, and will throw an error if the |
| 18 | + * `Observable` errors. |
| 19 | + * |
| 20 | + * The subscription will last for the lifetime of the current injection context. That is, if |
| 21 | + * `fromObservable` is called from a component context, the subscription will be cleaned up when the |
| 22 | + * component is destroyed. When called outside of a component, the current `EnvironmentInjector`'s |
| 23 | + * lifetime will be used (which is typically the lifetime of the application itself). |
| 24 | + * |
| 25 | + * If the `Observable` does not produce a value before the `Signal` is read, the `Signal` will throw |
| 26 | + * an error. To avoid this, use a synchronous `Observable` (potentially created with the `startWith` |
| 27 | + * operator) or pass an initial value to `fromObservable` as the second argument. |
| 28 | + * |
| 29 | + * `fromObservable` must be called in an injection context. |
| 30 | + */ |
| 31 | +export function fromObservable<T>(source: Observable<T>): Signal<T>; |
| 32 | + |
| 33 | +/** |
| 34 | + * Get the current value of an `Observable` as a reactive `Signal`. |
| 35 | + * |
| 36 | + * `fromObservable` returns a `Signal` which provides synchronous reactive access to values produced |
| 37 | + * by the given `Observable`, by subscribing to that `Observable`. The returned `Signal` will always |
| 38 | + * have the most recent value emitted by the subscription, and will throw an error if the |
| 39 | + * `Observable` errors. |
| 40 | + * |
| 41 | + * The subscription will last for the lifetime of the current injection context. That is, if |
| 42 | + * `fromObservable` is called from a component context, the subscription will be cleaned up when the |
| 43 | + * component is destroyed. When called outside of a component, the current `EnvironmentInjector`'s |
| 44 | + * lifetime will be used (which is typically the lifetime of the application itself). |
| 45 | + * |
| 46 | + * Before the `Observable` emits its first value, the `Signal` will return the configured |
| 47 | + * `initialValue`. If the `Observable` is known to produce a value before the `Signal` will be read, |
| 48 | + * `initialValue` does not need to be passed. |
| 49 | + * |
| 50 | + * `fromObservable` must be called in an injection context. |
| 51 | + * |
| 52 | + * @developerPreview |
| 53 | + */ |
| 54 | +export function fromObservable<T, U extends T|null|undefined>( |
| 55 | + // fromObservable(Observable<Animal>) -> Signal<Cat> |
| 56 | + source: Observable<T>, initialValue: U): Signal<T|U>; |
| 57 | +export function fromObservable<T, U = never>(source: Observable<T>, initialValue?: U): Signal<T|U> { |
| 58 | + assertInInjectionContext(fromObservable); |
| 59 | + |
| 60 | + // Note: T is the Observable value type, and U is the initial value type. They don't have to be |
| 61 | + // the same - the returned signal gives values of type `T`. |
| 62 | + let state: WritableSignal<State<T|U>>; |
| 63 | + if (initialValue === undefined && arguments.length !== 2) { |
| 64 | + // No initial value was passed, so initially the signal is in a `NoValue` state and will throw |
| 65 | + // if accessed. |
| 66 | + state = signal({kind: StateKind.NoValue}); |
| 67 | + } else { |
| 68 | + // An initial value was passed, so use it. |
| 69 | + state = signal<State<T|U>>({kind: StateKind.Value, value: initialValue!}); |
| 70 | + } |
| 71 | + |
| 72 | + const sub = source.subscribe({ |
| 73 | + next: value => state.set({kind: StateKind.Value, value}), |
| 74 | + error: error => state.set({kind: StateKind.Error, error}), |
| 75 | + // Completion of the Observable is meaningless to the signal. Signals don't have a concept of |
| 76 | + // "complete". |
| 77 | + }); |
| 78 | + |
| 79 | + // Unsubscribe when the current context is destroyed. |
| 80 | + inject(DestroyRef).onDestroy(sub.unsubscribe.bind(sub)); |
| 81 | + |
| 82 | + // The actual returned signal is a `computed` of the `State` signal, which maps the various states |
| 83 | + // to either values or errors. |
| 84 | + return computed(() => { |
| 85 | + const current = state(); |
| 86 | + switch (current.kind) { |
| 87 | + case StateKind.Value: |
| 88 | + return current.value; |
| 89 | + case StateKind.Error: |
| 90 | + throw current.error; |
| 91 | + case StateKind.NoValue: |
| 92 | + // TODO(alxhub): use a RuntimeError when we finalize the error semantics |
| 93 | + throw new Error(`fromObservable() signal read before the Observable emitted`); |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +const enum StateKind { |
| 99 | + NoValue, |
| 100 | + Value, |
| 101 | + Error, |
| 102 | +} |
| 103 | + |
| 104 | +interface NoValueState { |
| 105 | + kind: StateKind.NoValue; |
| 106 | +} |
| 107 | + |
| 108 | +interface ValueState<T> { |
| 109 | + kind: StateKind.Value; |
| 110 | + value: T; |
| 111 | +} |
| 112 | + |
| 113 | +interface ErrorState { |
| 114 | + kind: StateKind.Error; |
| 115 | + error: unknown; |
| 116 | +} |
| 117 | + |
| 118 | +type State<T> = NoValueState|ValueState<T>|ErrorState; |
0 commit comments