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

fix(core): allow toSignal calls in reactive context #51831

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 16 additions & 14 deletions packages/core/rxjs-interop/src/to_signal.ts
Expand Up @@ -175,21 +175,23 @@ export function toSignal<T, U = undefined>(
state = signal<State<T|U>>({kind: StateKind.Value, value: options?.initialValue as U});
}

const sub = source.subscribe({
next: value => state.set({kind: StateKind.Value, value}),
error: error => state.set({kind: StateKind.Error, error}),
// Completion of the Observable is meaningless to the signal. Signals don't have a concept of
// "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.');
}
untracked(() => {
const sub = source.subscribe({
next: value => state.set({kind: StateKind.Value, value}),
error: error => state.set({kind: 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 === 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, if requested.
cleanupRef?.onDestroy(sub.unsubscribe.bind(sub));
// 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.
Expand Down
15 changes: 14 additions & 1 deletion packages/core/rxjs-interop/test/to_signal_spec.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {DestroyRef, EnvironmentInjector, Injector, runInInjectionContext} from '@angular/core';
import {computed, EnvironmentInjector, Injector, runInInjectionContext} from '@angular/core';
import {toSignal} from '@angular/core/rxjs-interop';
import {BehaviorSubject, ReplaySubject, Subject} from 'rxjs';

Expand Down Expand Up @@ -109,6 +109,19 @@ describe('toSignal()', () => {
expect(counter()).toBe(1);
});

it('should allow toSignal creation in a reactive context - issue 51027', () => {
const counter$ = new BehaviorSubject(1);

const injector = Injector.create([]);

const doubleCounter = computed(() => {
const counter = toSignal(counter$, {requireSync: true, injector});
return counter() * 2;
});

expect(doubleCounter()).toBe(2);
});

describe('with no initial value', () => {
it('should return `undefined` if read before a value is emitted', test(() => {
const counter$ = new Subject<number>();
Expand Down
11 changes: 10 additions & 1 deletion packages/core/test/signals/computed_spec.ts
Expand Up @@ -167,7 +167,16 @@ describe('computed', () => {
expect(watchCount).toEqual(2);
});

it('should disallow writing to signals within computeds', () => {
it('should allow signal creation within computed', () => {
const doubleCounter = computed(() => {
const counter = signal(1);
return counter() * 2;
});

expect(doubleCounter()).toBe(2);
});

it('should disallow writing to signals within computed', () => {
const source = signal(0);
const illegal = computed(() => {
source.set(1);
Expand Down