art-standard-lib
Version:
The Standard Library for JavaScript that aught to be.
75 lines (57 loc) • 2.25 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Promise, ReschedulableTimer, currentSecond, timeout;
Promise = require('./Promise').Promise;
currentSecond = require('./Time').currentSecond;
timeout = require('./AsyncExtensions').timeout;
module.exports = ReschedulableTimer = (function() {
function ReschedulableTimer() {
this._actionCount = 0;
this.cancel();
}
/*
every time you call timeout it effectively cancels all previously pending timeouts
leaving only this, new timeout active.
In actuality, the repvious timeouts complete at some point, but their 'actions' are skipped.
OUT:
Promise.then ->
The result of the next-completed timeout.
Note:
If no additional calls to timeout are made within your ms, then
this will be the result of your action.
However, if another timeout is triggered before ms expires, the result
will be the result of the action passed in then.
*/
ReschedulableTimer.prototype.timeout = function(ms, action) {
var actionCount;
actionCount = this._actionCount += 1;
timeout(ms, (function(_this) {
return function() {
var ref, reject, resolve;
if (_this._actionCount === actionCount) {
ref = _this, reject = ref.reject, resolve = ref.resolve;
_this.cancel();
return Promise.then(function() {
return action();
})["catch"](reject).then(resolve);
}
};
})(this));
return this._getPendingPromise();
};
ReschedulableTimer.prototype.cancel = function() {
this.resolve = this.reject = this._pendingTimeoutPromise = null;
return this._actionCount++;
};
ReschedulableTimer.prototype._getPendingPromise = function() {
return this._pendingTimeoutPromise != null ? this._pendingTimeoutPromise : this._pendingTimeoutPromise = new Promise((function(_this) {
return function(resolve1, reject1) {
_this.resolve = resolve1;
_this.reject = reject1;
};
})(this));
};
return ReschedulableTimer;
})();
}).call(this);
//# sourceMappingURL=ReschedulableTimer.js.map