alm
Version:
The best IDE for TypeScript
55 lines (54 loc) • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var RunOnceScheduler = /** @class */ (function () {
function RunOnceScheduler(runner, timeout) {
this.timeoutToken = -1;
this.runner = runner;
this.timeout = timeout;
this.timeoutHandler = this.onTimeout.bind(this);
}
/**
* Dispose RunOnceScheduler
*/
RunOnceScheduler.prototype.dispose = function () {
this.cancel();
this.runner = null;
};
/**
* Cancel current scheduled runner (if any).
*/
RunOnceScheduler.prototype.cancel = function () {
if (this.isScheduled()) {
clearTimeout(this.timeoutToken);
this.timeoutToken = -1;
}
};
/**
* Replace runner. If there is a runner already scheduled, the new runner will be called.
*/
RunOnceScheduler.prototype.setRunner = function (runner) {
this.runner = runner;
};
/**
* Cancel previous runner (if any) & schedule a new runner.
*/
RunOnceScheduler.prototype.schedule = function (delay) {
if (delay === void 0) { delay = this.timeout; }
this.cancel();
this.timeoutToken = setTimeout(this.timeoutHandler, this.timeout);
};
/**
* Returns true if scheduled.
*/
RunOnceScheduler.prototype.isScheduled = function () {
return this.timeoutToken !== -1;
};
RunOnceScheduler.prototype.onTimeout = function () {
this.timeoutToken = -1;
if (this.runner) {
this.runner();
}
};
return RunOnceScheduler;
}());
exports.RunOnceScheduler = RunOnceScheduler;