angular2
Version:
Angular 2 - a web framework for modern web apps
136 lines • 5.49 kB
JavaScript
'use strict';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);
};
var lang_1 = require('angular2/src/facade/lang');
var async_1 = require('angular2/src/facade/async');
var core_1 = require('angular2/core');
var invalid_pipe_argument_exception_1 = require('./invalid_pipe_argument_exception');
var ObservableStrategy = (function () {
function ObservableStrategy() {
}
ObservableStrategy.prototype.createSubscription = function (async, updateLatestValue) {
return async_1.ObservableWrapper.subscribe(async, updateLatestValue, function (e) { throw e; });
};
ObservableStrategy.prototype.dispose = function (subscription) { async_1.ObservableWrapper.dispose(subscription); };
ObservableStrategy.prototype.onDestroy = function (subscription) { async_1.ObservableWrapper.dispose(subscription); };
return ObservableStrategy;
})();
var PromiseStrategy = (function () {
function PromiseStrategy() {
}
PromiseStrategy.prototype.createSubscription = function (async, updateLatestValue) {
return async.then(updateLatestValue);
};
PromiseStrategy.prototype.dispose = function (subscription) { };
PromiseStrategy.prototype.onDestroy = function (subscription) { };
return PromiseStrategy;
})();
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
*
* 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
* ```
*/
var AsyncPipe = (function () {
function AsyncPipe(_ref) {
/** @internal */
this._latestValue = null;
/** @internal */
this._latestReturnedValue = null;
/** @internal */
this._subscription = null;
/** @internal */
this._obj = null;
this._strategy = null;
this._ref = _ref;
}
AsyncPipe.prototype.ngOnDestroy = function () {
if (lang_1.isPresent(this._subscription)) {
this._dispose();
}
};
AsyncPipe.prototype.transform = function (obj, args) {
if (lang_1.isBlank(this._obj)) {
if (lang_1.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 core_1.WrappedValue.wrap(this._latestValue);
}
};
/** @internal */
AsyncPipe.prototype._subscribe = function (obj) {
var _this = this;
this._obj = obj;
this._strategy = this._selectStrategy(obj);
this._subscription =
this._strategy.createSubscription(obj, function (value) { return _this._updateLatestValue(obj, value); });
};
/** @internal */
AsyncPipe.prototype._selectStrategy = function (obj) {
if (lang_1.isPromise(obj)) {
return _promiseStrategy;
}
else if (async_1.ObservableWrapper.isObservable(obj)) {
return _observableStrategy;
}
else {
throw new invalid_pipe_argument_exception_1.InvalidPipeArgumentException(AsyncPipe, obj);
}
};
/** @internal */
AsyncPipe.prototype._dispose = function () {
this._strategy.dispose(this._subscription);
this._latestValue = null;
this._latestReturnedValue = null;
this._subscription = null;
this._obj = null;
};
/** @internal */
AsyncPipe.prototype._updateLatestValue = function (async, value) {
if (async === this._obj) {
this._latestValue = value;
this._ref.markForCheck();
}
};
AsyncPipe = __decorate([
core_1.Pipe({ name: 'async', pure: false }),
core_1.Injectable(),
__metadata('design:paramtypes', [core_1.ChangeDetectorRef])
], AsyncPipe);
return AsyncPipe;
})();
exports.AsyncPipe = AsyncPipe;
//# sourceMappingURL=async_pipe.js.map