Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions goldens/public-api/core/rxjs-interop/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Injector } from '@angular/core';
import { MonoTypeOperatorFunction } from 'rxjs';
import { Observable } from 'rxjs';
import { Signal } from '@angular/core';
import { Subscribable } from 'rxjs';

// @public
export function takeUntilDestroyed<T>(destroyRef?: DestroyRef): MonoTypeOperatorFunction<T>;
Expand All @@ -22,21 +23,21 @@ export interface ToObservableOptions {
}

// @public
export function toSignal<T>(source: Observable<T>): Signal<T | undefined>;
export function toSignal<T>(source: Observable<T> | Subscribable<T>): Signal<T | undefined>;

// @public
export function toSignal<T>(source: Observable<T>, options?: ToSignalOptions<undefined> & {
export function toSignal<T>(source: Observable<T> | Subscribable<T>, options?: ToSignalOptions<undefined> & {
requireSync?: false;
}): Signal<T | undefined>;

// @public
export function toSignal<T, U extends T | null | undefined>(source: Observable<T>, options: ToSignalOptions<U> & {
export function toSignal<T, U extends T | null | undefined>(source: Observable<T> | Subscribable<T>, options: ToSignalOptions<U> & {
initialValue: U;
requireSync?: false;
}): Signal<T | U>;

// @public
export function toSignal<T>(source: Observable<T>, options: ToSignalOptions<undefined> & {
export function toSignal<T>(source: Observable<T> | Subscribable<T>, options: ToSignalOptions<undefined> & {
requireSync: true;
}): Signal<T>;

Expand Down
13 changes: 7 additions & 6 deletions packages/core/rxjs-interop/src/to_signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {assertInInjectionContext, computed, DestroyRef, inject, Injector, signal, Signal, WritableSignal} from '@angular/core';
import {Observable} from 'rxjs';
import {Observable, Subscribable} from 'rxjs';

import {RuntimeError, RuntimeErrorCode} from '../../src/errors';
import {untracked} from '../../src/signals';
Expand Down Expand Up @@ -73,7 +73,7 @@ export interface ToSignalOptions<T> {
* option can be specified instead, which disables the automatic subscription teardown. No injection
* context is needed in this configuration as well.
*/
export function toSignal<T>(source: Observable<T>): Signal<T|undefined>;
export function toSignal<T>(source: Observable<T>|Subscribable<T>): Signal<T|undefined>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Observable implements Subscribable, is there a reason you're keeping Observable in the signature ?

@divdavem divdavem May 5, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I saw this code in PipeAsync:

  // NOTE(@benlesh): Because Observable has deprecated a few call patterns for `subscribe`,
  // TypeScript has a hard time matching Observable to Subscribable, for more information
  // see https://github.com/microsoft/TypeScript/issues/43643


  transform<T>(obj: Observable<T>|Subscribable<T>|Promise<T>): T|null;

It looks like Typescript has a bug sometimes.


/**
* Get the current value of an `Observable` as a reactive `Signal`.
Expand All @@ -99,7 +99,7 @@ export function toSignal<T>(source: Observable<T>): Signal<T|undefined>;
* @developerPreview
*/
export function toSignal<T>(
source: Observable<T>,
source: Observable<T>|Subscribable<T>,
options?: ToSignalOptions<undefined>&{requireSync?: false}): Signal<T|undefined>;


Expand Down Expand Up @@ -127,7 +127,7 @@ export function toSignal<T>(
* @developerPreview
*/
export function toSignal<T, U extends T|null|undefined>(
source: Observable<T>,
source: Observable<T>|Subscribable<T>,
options: ToSignalOptions<U>&{initialValue: U, requireSync?: false}): Signal<T|U>;

/**
Expand All @@ -154,9 +154,10 @@ export function toSignal<T, U extends T|null|undefined>(
* @developerPreview
*/
export function toSignal<T>(
source: Observable<T>, options: ToSignalOptions<undefined>&{requireSync: true}): Signal<T>;
source: Observable<T>|Subscribable<T>,
options: ToSignalOptions<undefined>&{requireSync: true}): Signal<T>;
export function toSignal<T, U = undefined>(
source: Observable<T>, options?: ToSignalOptions<U>): Signal<T|U> {
source: Observable<T>|Subscribable<T>, options?: ToSignalOptions<U>): Signal<T|U> {
const requiresCleanup = !options?.manualCleanup;
requiresCleanup && !options?.injector && assertInInjectionContext(toSignal);
const cleanupRef =
Expand Down