151 lines
4.5 KiB
TypeScript
151 lines
4.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. 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 {ChangeDetectorRef, OnDestroy, Pipe, WrappedValue} from '@angular/core';
|
|
import {EventEmitter, Observable} from '../facade/async';
|
|
import {isBlank, isPresent} from '../facade/lang';
|
|
import {isPromise} from '../private_import_core';
|
|
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
|
|
|
interface SubscriptionStrategy {
|
|
createSubscription(async: any, updateLatestValue: any): any;
|
|
dispose(subscription: any): void;
|
|
onDestroy(subscription: any): void;
|
|
}
|
|
|
|
class ObservableStrategy implements SubscriptionStrategy {
|
|
createSubscription(async: any, updateLatestValue: any): any {
|
|
return async.subscribe({next: updateLatestValue, error: (e: any) => { throw e; }});
|
|
}
|
|
|
|
dispose(subscription: any): void { subscription.unsubscribe(); }
|
|
|
|
onDestroy(subscription: any): void { subscription.unsubscribe(); }
|
|
}
|
|
|
|
class PromiseStrategy implements SubscriptionStrategy {
|
|
createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any): any {
|
|
return async.then(updateLatestValue, e => { throw e; });
|
|
}
|
|
|
|
dispose(subscription: any): void {}
|
|
|
|
onDestroy(subscription: any): void {}
|
|
}
|
|
|
|
var _promiseStrategy = new PromiseStrategy();
|
|
var _observableStrategy = new ObservableStrategy();
|
|
var __unused: Promise<any>; // avoid unused import when Promise union types are erased
|
|
|
|
/**
|
|
* @ngModule CommonModule
|
|
* @whatItDoes Unwraps a value from an asynchronous primitive.
|
|
* @howToUse `observable_or_promise_expression | async`
|
|
* @description
|
|
* The `async` pipe subscribes to an `Observable` or `Promise` and returns the latest value it has
|
|
* emitted. When a new value is emitted, the `async` pipe marks the component to be checked for
|
|
* changes. When the component gets destroyed, the `async` pipe unsubscribes automatically to avoid
|
|
* potential memory leaks.
|
|
*
|
|
*
|
|
* ## Examples
|
|
*
|
|
* This example binds a `Promise` to the view. Clicking the `Resolve` button resolves the
|
|
* promise.
|
|
*
|
|
* {@example common/pipes/ts/async_pipe.ts region='AsyncPipePromise'}
|
|
*
|
|
* It's also possible to use `async` with Observables. The example below binds the `time` Observable
|
|
* to the view. The Observable continuesly updates the view with the current time.
|
|
*
|
|
* {@example common/pipes/ts/async_pipe.ts region='AsyncPipeObservable'}
|
|
*
|
|
* @stable
|
|
*/
|
|
@Pipe({name: 'async', pure: false})
|
|
export class AsyncPipe implements OnDestroy {
|
|
/** @internal */
|
|
_latestValue: Object = null;
|
|
/** @internal */
|
|
_latestReturnedValue: Object = null;
|
|
|
|
/** @internal */
|
|
_subscription: Object = null;
|
|
/** @internal */
|
|
_obj: Observable<any>|Promise<any>|EventEmitter<any> = null;
|
|
/** @internal */
|
|
_ref: ChangeDetectorRef;
|
|
private _strategy: SubscriptionStrategy = null;
|
|
|
|
constructor(_ref: ChangeDetectorRef) { this._ref = _ref; }
|
|
|
|
ngOnDestroy(): void {
|
|
if (isPresent(this._subscription)) {
|
|
this._dispose();
|
|
}
|
|
}
|
|
|
|
transform(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
|
if (isBlank(this._obj)) {
|
|
if (isPresent(obj)) {
|
|
this._subscribe(obj);
|
|
}
|
|
this._latestReturnedValue = this._latestValue;
|
|
return this._latestValue;
|
|
}
|
|
|
|
if (obj !== this._obj) {
|
|
this._dispose();
|
|
return this.transform(obj);
|
|
}
|
|
|
|
if (this._latestValue === this._latestReturnedValue) {
|
|
return this._latestReturnedValue;
|
|
} else {
|
|
this._latestReturnedValue = this._latestValue;
|
|
return WrappedValue.wrap(this._latestValue);
|
|
}
|
|
}
|
|
|
|
/** @internal */
|
|
_subscribe(obj: Observable<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));
|
|
}
|
|
|
|
/** @internal */
|
|
_selectStrategy(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
|
if (isPromise(obj)) {
|
|
return _promiseStrategy;
|
|
} else if ((<any>obj).subscribe) {
|
|
return _observableStrategy;
|
|
} else {
|
|
throw new InvalidPipeArgumentError(AsyncPipe, obj);
|
|
}
|
|
}
|
|
|
|
/** @internal */
|
|
_dispose(): void {
|
|
this._strategy.dispose(this._subscription);
|
|
this._latestValue = null;
|
|
this._latestReturnedValue = null;
|
|
this._subscription = null;
|
|
this._obj = null;
|
|
}
|
|
|
|
/** @internal */
|
|
_updateLatestValue(async: any, value: Object) {
|
|
if (async === this._obj) {
|
|
this._latestValue = value;
|
|
this._ref.markForCheck();
|
|
}
|
|
}
|
|
}
|