2016-06-23 09:47:54 -07:00
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
*/
|
|
|
|
|
|
2017-03-01 10:28:52 -08:00
|
|
|
|
import {ChangeDetectorRef, EventEmitter, OnDestroy, Pipe, PipeTransform, WrappedValue, ɵisObservable, ɵisPromise} from '@angular/core';
|
2018-02-27 17:06:06 -05:00
|
|
|
|
import {Observable, SubscriptionLike} from 'rxjs';
|
2017-01-27 13:19:00 -08:00
|
|
|
|
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
2016-03-12 14:22:31 +01:00
|
|
|
|
interface SubscriptionStrategy {
|
2018-02-27 17:06:06 -05:00
|
|
|
|
createSubscription(async: Observable<any>|Promise<any>, updateLatestValue: any): SubscriptionLike
|
2017-03-24 09:54:02 -07:00
|
|
|
|
|Promise<any>;
|
2018-02-27 17:06:06 -05:00
|
|
|
|
dispose(subscription: SubscriptionLike|Promise<any>): void;
|
|
|
|
|
onDestroy(subscription: SubscriptionLike|Promise<any>): void;
|
2016-03-12 14:22:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ObservableStrategy implements SubscriptionStrategy {
|
2018-02-27 17:06:06 -05:00
|
|
|
|
createSubscription(async: Observable<any>, updateLatestValue: any): SubscriptionLike {
|
2016-08-02 15:53:34 -07:00
|
|
|
|
return async.subscribe({next: updateLatestValue, error: (e: any) => { throw e; }});
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
|
dispose(subscription: SubscriptionLike): void { subscription.unsubscribe(); }
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
|
onDestroy(subscription: SubscriptionLike): void { subscription.unsubscribe(); }
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 14:22:31 +01:00
|
|
|
|
class PromiseStrategy implements SubscriptionStrategy {
|
2017-03-24 09:54:02 -07:00
|
|
|
|
createSubscription(async: Promise<any>, updateLatestValue: (v: any) => any): Promise<any> {
|
2016-05-25 17:16:50 -07:00
|
|
|
|
return async.then(updateLatestValue, e => { throw e; });
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 09:54:02 -07:00
|
|
|
|
dispose(subscription: Promise<any>): void {}
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
2017-03-24 09:54:02 -07:00
|
|
|
|
onDestroy(subscription: Promise<any>): void {}
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 00:27:56 -07:00
|
|
|
|
const _promiseStrategy = new PromiseStrategy();
|
|
|
|
|
const _observableStrategy = new ObservableStrategy();
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
|
|
|
|
/**
|
2016-09-08 21:41:09 -07:00
|
|
|
|
* @ngModule CommonModule
|
|
|
|
|
* @description
|
2018-03-29 11:12:34 +01:00
|
|
|
|
*
|
|
|
|
|
* Unwraps a value from an asynchronous primitive.
|
|
|
|
|
*
|
2018-09-02 22:33:49 +08:00
|
|
|
|
* 从一个异步回执中解出一个值。
|
|
|
|
|
*
|
2016-06-01 03:23:29 +02:00
|
|
|
|
* The `async` pipe subscribes to an `Observable` or `Promise` and returns the latest value it has
|
2016-09-08 21:41:09 -07:00
|
|
|
|
* 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
|
2016-06-01 03:23:29 +02:00
|
|
|
|
* potential memory leaks.
|
2015-08-04 11:55:21 -07:00
|
|
|
|
*
|
2018-09-02 22:33:49 +08:00
|
|
|
|
* `async` 管道会订阅一个 `Observable` 或 `Promise`,并返回它发出的最近一个值。
|
|
|
|
|
* 当新值到来时,`async` 管道就会把该组件标记为需要进行变更检测。当组件被销毁时,`async` 管道就会自动取消订阅,以消除潜在的内存泄露问题。
|
2016-06-01 03:23:29 +02:00
|
|
|
|
*
|
2018-05-03 09:38:05 +01:00
|
|
|
|
* @usageNotes
|
2016-06-01 03:23:29 +02:00
|
|
|
|
*
|
2018-05-03 09:38:05 +01:00
|
|
|
|
* ### Examples
|
2015-08-04 11:55:21 -07:00
|
|
|
|
*
|
2018-10-21 17:44:10 +08:00
|
|
|
|
* ### 例子
|
2018-09-02 22:33:49 +08:00
|
|
|
|
*
|
2015-11-02 15:46:59 -08:00
|
|
|
|
* This example binds a `Promise` to the view. Clicking the `Resolve` button resolves the
|
|
|
|
|
* promise.
|
|
|
|
|
*
|
2018-09-02 22:33:49 +08:00
|
|
|
|
* 这个例子把一个 `Promise` 绑定到了视图中。点击 `Resolve` 按钮就会解析此 Promise。
|
|
|
|
|
*
|
2016-09-08 21:41:09 -07:00
|
|
|
|
* {@example common/pipes/ts/async_pipe.ts region='AsyncPipePromise'}
|
2015-11-02 15:46:59 -08:00
|
|
|
|
*
|
|
|
|
|
* It's also possible to use `async` with Observables. The example below binds the `time` Observable
|
2016-11-22 22:31:27 -05:00
|
|
|
|
* to the view. The Observable continuously updates the view with the current time.
|
2015-11-02 15:46:59 -08:00
|
|
|
|
*
|
2018-09-02 22:33:49 +08:00
|
|
|
|
* 还可以把 `async` 用于 `Observable`。下面的例子就把 `time` 这个 `Observable` 绑定到了视图上。这个 `Observable` 会不断使用当前时间更新视图。
|
|
|
|
|
*
|
2016-09-08 21:41:09 -07:00
|
|
|
|
* {@example common/pipes/ts/async_pipe.ts region='AsyncPipeObservable'}
|
2016-05-27 11:24:05 -07:00
|
|
|
|
*
|
2018-10-19 15:06:08 +01:00
|
|
|
|
* @publicApi
|
2015-08-04 11:55:21 -07:00
|
|
|
|
*/
|
2015-09-08 09:17:58 -07:00
|
|
|
|
@Pipe({name: 'async', pure: false})
|
2017-01-21 11:27:42 +01:00
|
|
|
|
export class AsyncPipe implements OnDestroy, PipeTransform {
|
2017-03-24 09:54:02 -07:00
|
|
|
|
private _latestValue: any = null;
|
|
|
|
|
private _latestReturnedValue: any = null;
|
2016-09-11 00:27:56 -07:00
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
|
private _subscription: SubscriptionLike|Promise<any>|null = null;
|
2017-03-24 09:54:02 -07:00
|
|
|
|
private _obj: Observable<any>|Promise<any>|EventEmitter<any>|null = null;
|
|
|
|
|
private _strategy: SubscriptionStrategy = null !;
|
2016-05-27 12:20:45 -07:00
|
|
|
|
|
2016-09-11 00:27:56 -07:00
|
|
|
|
constructor(private _ref: ChangeDetectorRef) {}
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
2015-11-17 10:09:23 -08:00
|
|
|
|
ngOnDestroy(): void {
|
2016-09-11 00:27:56 -07:00
|
|
|
|
if (this._subscription) {
|
2015-08-04 11:55:21 -07:00
|
|
|
|
this._dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 09:54:02 -07:00
|
|
|
|
transform<T>(obj: null): null;
|
|
|
|
|
transform<T>(obj: undefined): undefined;
|
2018-02-12 09:06:17 -08:00
|
|
|
|
transform<T>(obj: Observable<T>|null|undefined): T|null;
|
|
|
|
|
transform<T>(obj: Promise<T>|null|undefined): T|null;
|
2017-03-24 09:54:02 -07:00
|
|
|
|
transform(obj: Observable<any>|Promise<any>|null|undefined): any {
|
2016-09-11 00:27:56 -07:00
|
|
|
|
if (!this._obj) {
|
|
|
|
|
if (obj) {
|
2016-05-25 17:16:50 -07:00
|
|
|
|
this._subscribe(obj);
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
2015-12-17 20:30:40 -08:00
|
|
|
|
this._latestReturnedValue = this._latestValue;
|
2015-12-04 16:43:49 -08:00
|
|
|
|
return this._latestValue;
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (obj !== this._obj) {
|
|
|
|
|
this._dispose();
|
2017-01-31 18:11:18 -08:00
|
|
|
|
return this.transform(obj as any);
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this._latestValue === this._latestReturnedValue) {
|
|
|
|
|
return this._latestReturnedValue;
|
|
|
|
|
}
|
2016-09-11 00:27:56 -07:00
|
|
|
|
|
|
|
|
|
this._latestReturnedValue = this._latestValue;
|
|
|
|
|
return WrappedValue.wrap(this._latestValue);
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 00:27:56 -07:00
|
|
|
|
private _subscribe(obj: Observable<any>|Promise<any>|EventEmitter<any>): void {
|
2015-08-04 11:55:21 -07:00
|
|
|
|
this._obj = obj;
|
|
|
|
|
this._strategy = this._selectStrategy(obj);
|
2016-02-11 17:01:17 -08:00
|
|
|
|
this._subscription = this._strategy.createSubscription(
|
2016-05-25 17:16:50 -07:00
|
|
|
|
obj, (value: Object) => this._updateLatestValue(obj, value));
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 00:27:56 -07:00
|
|
|
|
private _selectStrategy(obj: Observable<any>|Promise<any>|EventEmitter<any>): any {
|
2017-02-17 12:55:55 -08:00
|
|
|
|
if (ɵisPromise(obj)) {
|
2015-08-04 11:55:21 -07:00
|
|
|
|
return _promiseStrategy;
|
2016-09-11 00:27:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 12:55:55 -08:00
|
|
|
|
if (ɵisObservable(obj)) {
|
2015-08-04 11:55:21 -07:00
|
|
|
|
return _observableStrategy;
|
|
|
|
|
}
|
2016-09-11 00:27:56 -07:00
|
|
|
|
|
2017-01-27 13:19:00 -08:00
|
|
|
|
throw invalidPipeArgumentError(AsyncPipe, obj);
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 00:27:56 -07:00
|
|
|
|
private _dispose(): void {
|
2017-03-24 09:54:02 -07:00
|
|
|
|
this._strategy.dispose(this._subscription !);
|
2015-08-04 11:55:21 -07:00
|
|
|
|
this._latestValue = null;
|
|
|
|
|
this._latestReturnedValue = null;
|
|
|
|
|
this._subscription = null;
|
|
|
|
|
this._obj = null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-21 11:27:42 +01:00
|
|
|
|
private _updateLatestValue(async: any, value: Object): void {
|
2015-08-04 11:55:21 -07:00
|
|
|
|
if (async === this._obj) {
|
|
|
|
|
this._latestValue = value;
|
2015-08-28 10:08:18 -07:00
|
|
|
|
this._ref.markForCheck();
|
2015-08-04 11:55:21 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-14 10:03:45 -07:00
|
|
|
|
}
|