UNPKG

@xutl/test-timers

Version:

Mocked/Intercepted Timers

156 lines (155 loc) 5.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.bump = exports.update = exports.clearImmediate = exports.setImmediate = exports.clearInterval = exports.setInterval = exports.clearTimeout = exports.setTimeout = exports.Date = void 0; const handler_1 = __importDefault(require("./handler")); let time = globalThis.Date.now(); const Legacy = globalThis.Date; //@ts-ignore const Modern = function Date(year, month, date, hours, minutes, seconds, ms, ...args) { if (this && this instanceof Modern) { switch (arguments.length) { case 0: return new Legacy(time); case 1: return new Legacy(year !== null && year !== void 0 ? year : 0); case 2: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0); case 3: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date); case 4: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date, hours); case 5: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date, hours, minutes); case 6: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date, hours, minutes, seconds); case 7: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date, hours, minutes, seconds, ms); default: return new Legacy(year !== null && year !== void 0 ? year : 0, month !== null && month !== void 0 ? month : 0, date, hours, minutes, seconds, ...args); } } else { return `${new Legacy(time)}`; } }; Modern.now = () => time; Modern.parse = (value) => Legacy.parse(value); Modern.UTC = (year, month, ...args) => Legacy.UTC(year, month, ...args); //@ts-ignore Modern.prototype = Legacy.prototype; globalThis.Date = Modern; exports.Date = Legacy; const timers = new Map(); //@ts-ignore exports.setTimeout = globalThis.setTimeout; //@ts-ignore globalThis.setTimeout = (handler, timeout) => { const id = Symbol(); timers.set(id, { start: time, timeout: Math.max(timeout !== null && timeout !== void 0 ? timeout : 4, 4), handler: handler_1.default(handler), repeat: false, }); return id; }; //@ts-ignore exports.clearTimeout = globalThis.clearTimeout; //@ts-ignore globalThis.clearTimeout = (spot) => { const timer = timers.get(spot); if (!timer || timer.repeat) return false; timers.delete(spot); return true; }; //@ts-ignore exports.setInterval = globalThis.setInterval; //@ts-ignore globalThis.setInterval = (handler, timeout) => { const id = Symbol(); timers.set(id, { start: time, timeout: Math.max(timeout !== null && timeout !== void 0 ? timeout : 4, 4), handler: handler_1.default(handler), repeat: true, }); return id; }; //@ts-ignore exports.clearInterval = globalThis.clearInterval; //@ts-ignore globalThis.clearInterval = (spot) => { const timer = timers.get(spot); if (!timer || !timer.repeat || !timer.timeout) return false; timers.delete(spot); return true; }; //@ts-ignore exports.setImmediate = globalThis.setImmediate; //@ts-ignore globalThis.setImmediate = (handler) => { const id = Symbol(); timers.set(id, { start: time, timeout: 0, handler: handler_1.default(handler), repeat: false, }); return id; }; //@ts-ignore exports.clearImmediate = globalThis.clearImmediate; //@ts-ignore globalThis.Immediate = (spot) => { const timer = timers.get(spot); if (!timer || timer.repeat || timer.timeout) return false; timers.delete(spot); return true; }; function initialize(ms = Legacy.now()) { timers.clear(); time = ms; } exports.default = initialize; function update(ms = Legacy.now()) { const oldtime = time; const newtime = Math.max(ms, time); progressTime(oldtime, newtime); return time; } exports.update = update; function bump(ms = 0) { const oldtime = time; const newtime = time + Math.floor(Math.abs(ms)); progressTime(oldtime, newtime); return time; } exports.bump = bump; function progressTime(oldtime, newtime) { for (time = oldtime; time <= newtime; time += 4) timers.forEach(runTimer); if (time < newtime) { time = newtime; timers.forEach(runTimer); } } function runTimer(timer, spot) { const timerTime = timer.start + timer.timeout; if (timerTime > time) return; try { timer.handler.call(null); } catch (ex) { process.emit('uncaughtException', ex); } timer.start = time; if (!timer.repeat) timers.delete(spot); }