2015-11-06 17:34:07 -08:00
|
|
|
import {isBlank, isPresent, isPromise, CONST} from 'angular2/src/facade/lang';
|
|
|
|
import {Promise, ObservableWrapper, Observable, EventEmitter} from 'angular2/src/facade/async';
|
2015-09-03 22:01:36 -07:00
|
|
|
import {
|
2015-11-18 15:55:43 -08:00
|
|
|
Pipe,
|
|
|
|
Injectable,
|
2015-09-03 22:01:36 -07:00
|
|
|
ChangeDetectorRef,
|
2015-11-17 10:09:23 -08:00
|
|
|
OnDestroy,
|
2015-09-03 22:01:36 -07:00
|
|
|
PipeTransform,
|
|
|
|
WrappedValue
|
2015-11-18 15:55:43 -08:00
|
|
|
} from 'angular2/core';
|
2015-08-07 11:41:38 -07:00
|
|
|
|
|
|
|
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
2015-08-04 11:55:21 -07:00
|
|
|
|
|
|
|
class ObservableStrategy {
|
|
|
|
createSubscription(async: any, updateLatestValue: any): any {
|
|
|
|
return ObservableWrapper.subscribe(async, updateLatestValue, e => { throw e; });
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(subscription: any): void { ObservableWrapper.dispose(subscription); }
|
|
|
|
|
|
|
|
onDestroy(subscription: any): void { ObservableWrapper.dispose(subscription); }
|
|
|
|
}
|
|
|
|
|
|
|
|
class PromiseStrategy {
|
|
|
|
createSubscription(async: any, updateLatestValue: any): any {
|
|
|
|
return async.then(updateLatestValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(subscription: any): void {}
|
|
|
|
|
|
|
|
onDestroy(subscription: any): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _promiseStrategy = new PromiseStrategy();
|
|
|
|
var _observableStrategy = new ObservableStrategy();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-09 14:56:48 -07:00
|
|
|
* 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.
|
2015-08-04 11:55:21 -07:00
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-08-04 11:55:21 -07:00
|
|
|
*
|
2015-11-02 15:46:59 -08:00
|
|
|
* This example binds a `Promise` to the view. Clicking the `Resolve` button resolves the
|
|
|
|
* promise.
|
|
|
|
*
|
|
|
|
* {@example core/pipes/ts/async_pipe/async_pipe_example.ts region='AsyncPipe'}
|
|
|
|
*
|
|
|
|
* It's also possible to use `async` with Observables. The example below binds the `time` Observable
|
|
|
|
* to the view. Every 500ms, the `time` Observable updates the view with the current time.
|
|
|
|
*
|
|
|
|
* ```typescript
|
2015-09-09 21:49:35 -07:00
|
|
|
* ```
|
2015-08-04 11:55:21 -07:00
|
|
|
*/
|
2015-09-08 09:17:58 -07:00
|
|
|
@Pipe({name: 'async', pure: false})
|
2015-08-06 10:39:02 -07:00
|
|
|
@Injectable()
|
2015-11-17 10:09:23 -08:00
|
|
|
export class AsyncPipe implements PipeTransform, OnDestroy {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-04 11:55:21 -07:00
|
|
|
_latestValue: Object = null;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-04 11:55:21 -07:00
|
|
|
_latestReturnedValue: Object = null;
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-04 11:55:21 -07:00
|
|
|
_subscription: Object = null;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-10-24 18:48:43 -07:00
|
|
|
_obj: Observable<any>| Promise<any>| EventEmitter<any> = null;
|
2015-08-04 11:55:21 -07:00
|
|
|
private _strategy: any = null;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
|
|
|
public _ref: ChangeDetectorRef;
|
|
|
|
constructor(_ref: ChangeDetectorRef) { this._ref = _ref; }
|
2015-08-04 11:55:21 -07:00
|
|
|
|
2015-11-17 10:09:23 -08:00
|
|
|
ngOnDestroy(): void {
|
2015-08-04 11:55:21 -07:00
|
|
|
if (isPresent(this._subscription)) {
|
|
|
|
this._dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 18:48:43 -07:00
|
|
|
transform(obj: Observable<any>| Promise<any>| EventEmitter<any>, args?: any[]): any {
|
2015-08-04 11:55:21 -07:00
|
|
|
if (isBlank(this._obj)) {
|
|
|
|
if (isPresent(obj)) {
|
|
|
|
this._subscribe(obj);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-10-24 18:48:43 -07:00
|
|
|
_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);
|
|
|
|
this._subscription =
|
|
|
|
this._strategy.createSubscription(obj, value => this._updateLatestValue(obj, value));
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-10-24 18:48:43 -07:00
|
|
|
_selectStrategy(obj: Observable<any>| Promise<any>| EventEmitter<any>): any {
|
2015-08-04 11:55:21 -07:00
|
|
|
if (isPromise(obj)) {
|
|
|
|
return _promiseStrategy;
|
|
|
|
} else if (ObservableWrapper.isObservable(obj)) {
|
|
|
|
return _observableStrategy;
|
|
|
|
} else {
|
2015-08-06 10:39:02 -07:00
|
|
|
throw new InvalidPipeArgumentException(AsyncPipe, obj);
|
2015-08-04 11:55:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-04 11:55:21 -07:00
|
|
|
_dispose(): void {
|
|
|
|
this._strategy.dispose(this._subscription);
|
|
|
|
this._latestValue = null;
|
|
|
|
this._latestReturnedValue = null;
|
|
|
|
this._subscription = null;
|
|
|
|
this._obj = null;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-04 11:55:21 -07:00
|
|
|
_updateLatestValue(async: any, value: Object) {
|
|
|
|
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
|
|
|
}
|