/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/core/rxjs-interop/src/to_observable.ts
75 строк
2 KB
SkyZeroZx
docs: Adds links to relevant guides for APIs in core package
17 ноя 2025, 19:47
17 ноя 2025, 19:47
0432e76
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import { assertInInjectionContext, DestroyRef, effect, inject, Injector, Signal, untracked, } from '../../src/core'; import {Observable, ReplaySubject} from 'rxjs'; /** * Options for `toObservable`. * * @publicApi 20.0 */ export interface ToObservableOptions { /** * The `Injector` to use when creating the underlying `effect` which watches the signal. * * If this isn't specified, the current [injection context](guide/di/dependency-injection-context) * will be used. */ injector?: Injector; } /** * Exposes the value of an Angular `Signal` as an RxJS `Observable`. * As it reflects a state, the observable will always emit the latest value upon subscription. * * The signal's value will be propagated into the `Observable`'s subscribers using an `effect`. * * `toObservable` must be called in an injection context unless an injector is provided via options. * * @see [RxJS interop with Angular signals](ecosystem/rxjs-interop) * @see [Create an RxJS Observable from a signal with toObservable](ecosystem/rxjs-interop#create-an-rxjs-observable-from-a-signal-with-toobservable) * * @publicApi 20.0 */ export function toObservable<T>(source: Signal<T>, options?: ToObservableOptions): Observable<T> { if (ngDevMode && !options?.injector) { assertInInjectionContext(toObservable); } const injector = options?.injector ?? inject(Injector); const subject = new ReplaySubject<T>(1); const watcher = effect( () => { let value: T; try { value = source(); } catch (err) { untracked(() => subject.error(err)); return; } untracked(() => subject.next(value)); }, {injector, manualCleanup: true}, ); injector.get(DestroyRef).onDestroy(() => { watcher.destroy(); subject.complete(); }); return subject.asObservable(); }