Files
30-seconds-of-code/node_modules/xstate/lib/SimulatedClock.js
2019-08-20 15:52:05 +02:00

50 lines
1.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SimulatedClock = /** @class */ (function () {
function SimulatedClock() {
this.timeouts = new Map();
this._now = 0;
this._id = 0;
}
SimulatedClock.prototype.now = function () {
return this._now;
};
SimulatedClock.prototype.getId = function () {
return this._id++;
};
SimulatedClock.prototype.setTimeout = function (fn, timeout) {
var id = this.getId();
this.timeouts.set(id, {
start: this.now(),
timeout: timeout,
fn: fn
});
return id;
};
SimulatedClock.prototype.clearTimeout = function (id) {
this.timeouts.delete(id);
};
SimulatedClock.prototype.set = function (time) {
if (this._now > time) {
throw new Error('Unable to travel back in time');
}
this._now = time;
this.flushTimeouts();
};
SimulatedClock.prototype.flushTimeouts = function () {
var _this = this;
this.timeouts.forEach(function (timeout, id) {
if (_this.now() - timeout.start >= timeout.timeout) {
timeout.fn.call(null);
_this.timeouts.delete(id);
}
});
};
SimulatedClock.prototype.increment = function (ms) {
this._now += ms;
this.flushTimeouts();
};
return SimulatedClock;
}());
exports.SimulatedClock = SimulatedClock;
//# sourceMappingURL=SimulatedClock.js.map