/** * @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.io/license */ import { assertInInjectionContext, computed, DestroyRef, inject, signal } from '@angular/core'; import { RuntimeError } from '../../src/errors'; import { untracked } from '../../src/signals'; export function toSignal(source, options) { const requiresCleanup = !options?.manualCleanup; requiresCleanup && !options?.injector && assertInInjectionContext(toSignal); const cleanupRef = requiresCleanup ? options?.injector?.get(DestroyRef) ?? inject(DestroyRef) : null; // 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; if (options?.requireSync) { // Initially the signal is in a `NoValue` state. state = signal({ kind: 0 /* StateKind.NoValue */ }); } else { // If an initial value was passed, use it. Otherwise, use `undefined` as the initial value. state = signal({ kind: 1 /* StateKind.Value */, value: options?.initialValue }); } untracked(() => { const sub = source.subscribe({ next: value => state.set({ kind: 1 /* StateKind.Value */, value }), error: error => state.set({ kind: 2 /* StateKind.Error */, error }), // Completion of the Observable is meaningless to the signal. Signals don't have a concept of // "complete". }); if (ngDevMode && options?.requireSync && state().kind === 0 /* StateKind.NoValue */) { throw new RuntimeError(601 /* RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT */, '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.'); } // Unsubscribe when the current context is destroyed, if requested. cleanupRef?.onDestroy(sub.unsubscribe.bind(sub)); }); // The actual returned signal is a `computed` of the `State` signal, which maps the various states // to either values or errors. return computed(() => { const current = state(); switch (current.kind) { case 1 /* StateKind.Value */: return current.value; case 2 /* StateKind.Error */: throw current.error; case 0 /* 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 RuntimeError(601 /* RuntimeErrorCode.REQUIRE_SYNC_WITHOUT_SYNC_EMIT */, '`toSignal()` called with `requireSync` but `Observable` did not emit synchronously.'); } }); } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIj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