angular2
Version:
Angular 2 - a web framework for modern web apps
134 lines • 4.82 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { isBlank, isPresent, isPromise } from 'angular2/src/facade/lang';
import { ObservableWrapper } from 'angular2/src/facade/async';
import { Pipe } from 'angular2/src/core/metadata';
import { Injectable } from 'angular2/src/core/di';
import { ChangeDetectorRef, WrappedValue } from 'angular2/src/core/change_detection';
import { InvalidPipeArgumentException } from './invalid_pipe_argument_exception';
class ObservableStrategy {
createSubscription(async, updateLatestValue) {
return ObservableWrapper.subscribe(async, updateLatestValue, e => { throw e; });
}
dispose(subscription) { ObservableWrapper.dispose(subscription); }
onDestroy(subscription) { ObservableWrapper.dispose(subscription); }
}
class PromiseStrategy {
createSubscription(async, updateLatestValue) {
return async.then(updateLatestValue);
}
dispose(subscription) { }
onDestroy(subscription) { }
}
var _promiseStrategy = new PromiseStrategy();
var _observableStrategy = new ObservableStrategy();
/**
* 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.
*
* ### Example
* The example below binds the `time` Observable to the view. Every 500ms, the `time` Observable
* updates the view with the current time.
*
* ```
* import {Observable} from 'angular2/core';
* @Component({
* selector: "task-cmp",
* template: "Time: {{ time | async }}"
* })
* class Task {
* time = new Observable<number>(observer => {
* setInterval(_ =>
* observer.next(new Date().getTime()), 500);
* });
* }
* ```
*/
export let AsyncPipe = class {
constructor(_ref) {
/** @internal */
this._latestValue = null;
/** @internal */
this._latestReturnedValue = null;
/** @internal */
this._subscription = null;
/** @internal */
this._obj = null;
this._strategy = null;
this._ref = _ref;
}
onDestroy() {
if (isPresent(this._subscription)) {
this._dispose();
}
}
transform(obj, args) {
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);
}
}
/** @internal */
_subscribe(obj) {
this._obj = obj;
this._strategy = this._selectStrategy(obj);
this._subscription =
this._strategy.createSubscription(obj, value => this._updateLatestValue(obj, value));
}
/** @internal */
_selectStrategy(obj) {
if (isPromise(obj)) {
return _promiseStrategy;
}
else if (ObservableWrapper.isObservable(obj)) {
return _observableStrategy;
}
else {
throw new InvalidPipeArgumentException(AsyncPipe, obj);
}
}
/** @internal */
_dispose() {
this._strategy.dispose(this._subscription);
this._latestValue = null;
this._latestReturnedValue = null;
this._subscription = null;
this._obj = null;
}
/** @internal */
_updateLatestValue(async, value) {
if (async === this._obj) {
this._latestValue = value;
this._ref.markForCheck();
}
}
};
AsyncPipe = __decorate([
Pipe({ name: 'async', pure: false }),
Injectable(),
__metadata('design:paramtypes', [ChangeDetectorRef])
], AsyncPipe);
//# sourceMappingURL=async_pipe.js.map