/// <reference path='../../../third_party/typings/es6-promise/es6-promise.d.ts' />
// All handle calls get the same result for an aggregated collection of things
// in the queue.
var AggregateHandlerClass = (function () {
function AggregateHandlerClass(aggregator_) {
var _this = this;
this.aggregator_ = aggregator_;
this.resetNextPromise_ = function () {
_this.nextAggregate_ = new Promise(function (F, R) {
_this.fulfillNextFn_ = F;
});
};
// Checks to see if the aggregator can now aggregate the inputs. Returns
// true if the old |nextAggregate| has been fulfilled and a new
// |nextAggregate| has been created.
this.tryNext = function () {
var result = _this.aggregator_.check();
if (result === null) {
return false;
}
_this.fulfillNextFn_(result);
_this.resetNextPromise_();
return true;
};
// The handle function for aggregating elements. Note that all handle calls
// of values will get the same aggregated reult promise.
this.handle = function (x) {
_this.aggregator_.input(x);
var currentPromise = _this.nextAggregate_;
_this.tryNext();
return currentPromise;
};
this.resetNextPromise_();
}
AggregateHandlerClass.prototype.nextAggregate = function () {
return this.nextAggregate_;
};
return AggregateHandlerClass;
})();
function createAggregateHandler(aggregator) {
return new AggregateHandlerClass(aggregator);
}
exports.createAggregateHandler = createAggregateHandler;
|