angular/packages/core/src/util/lang.ts
David-Emmanuel DIVERNOIS c7f4abf18a feat(common): allow any Subscribable in async pipe (#39627)
As only methods from the Subscribable interface are currently used in the
implementation of the async pipe, it makes sense to make it explicit so
that it works successfully with any other implementation instead of
only Observable.

PR Close #39627
2020-11-23 08:28:11 -08:00

37 lines
1.2 KiB
TypeScript

/**
* @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 {Observable, Subscribable} from 'rxjs';
/**
* Determine if the argument is shaped like a Promise
*/
export function isPromise<T = any>(obj: any): obj is Promise<T> {
// allow any Promise/A+ compliant thenable.
// It's up to the caller to ensure that obj.then conforms to the spec
return !!obj && typeof obj.then === 'function';
}
/**
* Determine if the argument is a Subscribable
*/
export function isSubscribable(obj: any|Subscribable<any>): obj is Subscribable<any> {
return !!obj && typeof obj.subscribe === 'function';
}
/**
* Determine if the argument is an Observable
*
* Strictly this tests that the `obj` is `Subscribable`, since `Observable`
* types need additional methods, such as `lift()`. But it is adequate for our
* needs since within the Angular framework code we only ever need to use the
* `subscribe()` method, and RxJS has mechanisms to wrap `Subscribable` objects
* into `Observable` as needed.
*/
export const isObservable =
isSubscribable as ((obj: any|Observable<any>) => obj is Observable<any>);