Skip to content

Commit

Permalink
fix(AsyncPipe): update types to support RxJS 7
Browse files Browse the repository at this point in the history
This updates the accepted types to ensure TypeScript doesn't get confused by deprecated overloads specified in RxJS 7. See the comment added in the code for additional details.
  • Loading branch information
benlesh committed Apr 13, 2021
1 parent 30f3c50 commit 29665eb
Showing 1 changed file with 20 additions and 48 deletions.
68 changes: 20 additions & 48 deletions packages/common/src/pipes/async_pipe.ts
Expand Up @@ -6,33 +6,20 @@
* found in the LICENSE file at https://angular.io/license
*/

import {
ChangeDetectorRef,
EventEmitter,
OnDestroy,
Pipe,
PipeTransform,
ɵisPromise,
ɵisSubscribable,
} from "@angular/core";
import { Observable, Subscribable, Unsubscribable } from "rxjs";

import { invalidPipeArgumentError } from "./invalid_pipe_argument_error";
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, ɵisPromise, ɵisSubscribable,} from '@angular/core';
import {Observable, Subscribable, Unsubscribable} from 'rxjs';

import {invalidPipeArgumentError} from './invalid_pipe_argument_error';

interface SubscriptionStrategy {
createSubscription(
async: Subscribable<any> | Promise<any>,
updateLatestValue: any
): Unsubscribable | Promise<any>;
dispose(subscription: Unsubscribable | Promise<any>): void;
onDestroy(subscription: Unsubscribable | Promise<any>): void;
createSubscription(async: Subscribable<any>|Promise<any>, updateLatestValue: any): Unsubscribable
|Promise<any>;
dispose(subscription: Unsubscribable|Promise<any>): void;
onDestroy(subscription: Unsubscribable|Promise<any>): void;
}

class SubscribableStrategy implements SubscriptionStrategy {
createSubscription(
async: Subscribable<any>,
updateLatestValue: any
): Unsubscribable {
createSubscription(async: Subscribable<any>, updateLatestValue: any): Unsubscribable {
return async.subscribe({
next: updateLatestValue,
error: (e: any) => {
Expand All @@ -51,10 +38,7 @@ class SubscribableStrategy implements SubscriptionStrategy {
}

class PromiseStrategy implements SubscriptionStrategy {
createSubscription(
async: Promise<any>,
updateLatestValue: (v: any) => any
): Promise<any> {
createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any): Promise<any> {
return async.then(updateLatestValue, (e) => {
throw e;
});
Expand Down Expand Up @@ -95,16 +79,12 @@ const _subscribableStrategy = new SubscribableStrategy();
*
* @publicApi
*/
@Pipe({ name: "async", pure: false })
@Pipe({name: 'async', pure: false})
export class AsyncPipe implements OnDestroy, PipeTransform {
private _latestValue: any = null;

private _subscription: Unsubscribable | Promise<any> | null = null;
private _obj:
| Subscribable<any>
| Promise<any>
| EventEmitter<any>
| null = null;
private _subscription: Unsubscribable|Promise<any>|null = null;
private _obj:|Subscribable<any>|Promise<any>|EventEmitter<any>|null = null;
private _strategy: SubscriptionStrategy = null!;

constructor(private _ref: ChangeDetectorRef) {}
Expand All @@ -119,12 +99,10 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
// 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;
transform<T>(obj: null | undefined): null;
transform<T>(
obj: Observable<T> | Subscribable<T> | Promise<T> | null | undefined
): T | null;
transform<T>(obj: Subscribable<T> | Promise<T> | null | undefined): T | null {
transform<T>(obj: Observable<T>|Subscribable<T>|Promise<T>): T|null;
transform<T>(obj: null|undefined): null;
transform<T>(obj: Observable<T>|Subscribable<T>|Promise<T>|null|undefined): T|null;
transform<T>(obj: Subscribable<T>|Promise<T>|null|undefined): T|null {
if (!this._obj) {
if (obj) {
this._subscribe(obj);
Expand All @@ -140,20 +118,14 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
return this._latestValue;
}

private _subscribe(
obj: Subscribable<any> | Promise<any> | EventEmitter<any>
): void {
private _subscribe(obj: Subscribable<any>|Promise<any>|EventEmitter<any>): void {
this._obj = obj;
this._strategy = this._selectStrategy(obj);
this._subscription = this._strategy.createSubscription(
obj,
(value: Object) => this._updateLatestValue(obj, value)
);
obj, (value: Object) => this._updateLatestValue(obj, value));
}

private _selectStrategy(
obj: Subscribable<any> | Promise<any> | EventEmitter<any>
): any {
private _selectStrategy(obj: Subscribable<any>|Promise<any>|EventEmitter<any>): any {
if (ɵisPromise(obj)) {
return _promiseStrategy;
}
Expand Down

0 comments on commit 29665eb

Please sign in to comment.