Update checked-in dependencies
This commit is contained in:
parent
1afca056e3
commit
6989ba7bd2
3942 changed files with 55190 additions and 132206 deletions
574
node_modules/@sinonjs/fake-timers/src/fake-timers-src.js
generated
vendored
574
node_modules/@sinonjs/fake-timers/src/fake-timers-src.js
generated
vendored
|
|
@ -1,13 +1,18 @@
|
|||
"use strict";
|
||||
|
||||
const globalObject = require("@sinonjs/commons").global;
|
||||
let timersModule;
|
||||
let timersModule, timersPromisesModule;
|
||||
if (typeof require === "function" && typeof module === "object") {
|
||||
try {
|
||||
timersModule = require("timers");
|
||||
} catch (e) {
|
||||
// ignored
|
||||
}
|
||||
try {
|
||||
timersPromisesModule = require("timers/promises");
|
||||
} catch (e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -94,6 +99,8 @@ if (typeof require === "function" && typeof module === "object") {
|
|||
* @property {Function[]} methods - the methods that are faked
|
||||
* @property {boolean} [shouldClearNativeTimers] inherited from config
|
||||
* @property {{methodName:string, original:any}[] | undefined} timersModuleMethods
|
||||
* @property {{methodName:string, original:any}[] | undefined} timersPromisesModuleMethods
|
||||
* @property {Map<function(): void, AbortSignal>} abortListenerMap
|
||||
*/
|
||||
/* eslint-enable jsdoc/require-property-description */
|
||||
|
||||
|
|
@ -107,6 +114,7 @@ if (typeof require === "function" && typeof module === "object") {
|
|||
* @property {boolean} [shouldAdvanceTime] tells FakeTimers to increment mocked time automatically (default false)
|
||||
* @property {number} [advanceTimeDelta] increment mocked time every <<advanceTimeDelta>> ms (default: 20ms)
|
||||
* @property {boolean} [shouldClearNativeTimers] forwards clear timer calls to native functions if they are not fakes (default: false)
|
||||
* @property {boolean} [ignoreMissingTimers] default is false, meaning asking to fake timers that are not present will throw an error
|
||||
*/
|
||||
|
||||
/* eslint-disable jsdoc/require-property-description */
|
||||
|
|
@ -151,16 +159,26 @@ function withGlobal(_global) {
|
|||
const NOOP_ARRAY = function () {
|
||||
return [];
|
||||
};
|
||||
const timeoutResult = _global.setTimeout(NOOP, 0);
|
||||
const addTimerReturnsObject = typeof timeoutResult === "object";
|
||||
const hrtimePresent =
|
||||
const isPresent = {};
|
||||
let timeoutResult,
|
||||
addTimerReturnsObject = false;
|
||||
|
||||
if (_global.setTimeout) {
|
||||
isPresent.setTimeout = true;
|
||||
timeoutResult = _global.setTimeout(NOOP, 0);
|
||||
addTimerReturnsObject = typeof timeoutResult === "object";
|
||||
}
|
||||
isPresent.clearTimeout = Boolean(_global.clearTimeout);
|
||||
isPresent.setInterval = Boolean(_global.setInterval);
|
||||
isPresent.clearInterval = Boolean(_global.clearInterval);
|
||||
isPresent.hrtime =
|
||||
_global.process && typeof _global.process.hrtime === "function";
|
||||
const hrtimeBigintPresent =
|
||||
hrtimePresent && typeof _global.process.hrtime.bigint === "function";
|
||||
const nextTickPresent =
|
||||
isPresent.hrtimeBigint =
|
||||
isPresent.hrtime && typeof _global.process.hrtime.bigint === "function";
|
||||
isPresent.nextTick =
|
||||
_global.process && typeof _global.process.nextTick === "function";
|
||||
const utilPromisify = _global.process && require("util").promisify;
|
||||
const performancePresent =
|
||||
isPresent.performance =
|
||||
_global.performance && typeof _global.performance.now === "function";
|
||||
const hasPerformancePrototype =
|
||||
_global.Performance &&
|
||||
|
|
@ -169,29 +187,60 @@ function withGlobal(_global) {
|
|||
_global.performance &&
|
||||
_global.performance.constructor &&
|
||||
_global.performance.constructor.prototype;
|
||||
const queueMicrotaskPresent = _global.hasOwnProperty("queueMicrotask");
|
||||
const requestAnimationFramePresent =
|
||||
isPresent.queueMicrotask = _global.hasOwnProperty("queueMicrotask");
|
||||
isPresent.requestAnimationFrame =
|
||||
_global.requestAnimationFrame &&
|
||||
typeof _global.requestAnimationFrame === "function";
|
||||
const cancelAnimationFramePresent =
|
||||
isPresent.cancelAnimationFrame =
|
||||
_global.cancelAnimationFrame &&
|
||||
typeof _global.cancelAnimationFrame === "function";
|
||||
const requestIdleCallbackPresent =
|
||||
isPresent.requestIdleCallback =
|
||||
_global.requestIdleCallback &&
|
||||
typeof _global.requestIdleCallback === "function";
|
||||
const cancelIdleCallbackPresent =
|
||||
isPresent.cancelIdleCallbackPresent =
|
||||
_global.cancelIdleCallback &&
|
||||
typeof _global.cancelIdleCallback === "function";
|
||||
const setImmediatePresent =
|
||||
isPresent.setImmediate =
|
||||
_global.setImmediate && typeof _global.setImmediate === "function";
|
||||
const intlPresent = _global.Intl && typeof _global.Intl === "object";
|
||||
isPresent.clearImmediate =
|
||||
_global.clearImmediate && typeof _global.clearImmediate === "function";
|
||||
isPresent.Intl = _global.Intl && typeof _global.Intl === "object";
|
||||
|
||||
_global.clearTimeout(timeoutResult);
|
||||
if (_global.clearTimeout) {
|
||||
_global.clearTimeout(timeoutResult);
|
||||
}
|
||||
|
||||
const NativeDate = _global.Date;
|
||||
const NativeIntl = _global.Intl;
|
||||
let uniqueTimerId = idCounterStart;
|
||||
|
||||
if (NativeDate === undefined) {
|
||||
throw new Error(
|
||||
"The global scope doesn't have a `Date` object" +
|
||||
" (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)",
|
||||
);
|
||||
}
|
||||
isPresent.Date = true;
|
||||
|
||||
/**
|
||||
* The PerformanceEntry object encapsulates a single performance metric
|
||||
* that is part of the browser's performance timeline.
|
||||
*
|
||||
* This is an object returned by the `mark` and `measure` methods on the Performance prototype
|
||||
*/
|
||||
class FakePerformanceEntry {
|
||||
constructor(name, entryType, startTime, duration) {
|
||||
this.name = name;
|
||||
this.entryType = entryType;
|
||||
this.startTime = startTime;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return JSON.stringify({ ...this });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} num
|
||||
* @returns {boolean}
|
||||
|
|
@ -376,109 +425,76 @@ function withGlobal(_global) {
|
|||
return infiniteLoopError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Date} target
|
||||
* @param {Date} source
|
||||
* @returns {Date} the target after modifications
|
||||
*/
|
||||
function mirrorDateProperties(target, source) {
|
||||
let prop;
|
||||
for (prop in source) {
|
||||
if (source.hasOwnProperty(prop)) {
|
||||
target[prop] = source[prop];
|
||||
}
|
||||
}
|
||||
|
||||
// set special now implementation
|
||||
if (source.now) {
|
||||
target.now = function now() {
|
||||
return target.clock.now;
|
||||
};
|
||||
} else {
|
||||
delete target.now;
|
||||
}
|
||||
|
||||
// set special toSource implementation
|
||||
if (source.toSource) {
|
||||
target.toSource = function toSource() {
|
||||
return source.toSource();
|
||||
};
|
||||
} else {
|
||||
delete target.toSource;
|
||||
}
|
||||
|
||||
// set special toString implementation
|
||||
target.toString = function toString() {
|
||||
return source.toString();
|
||||
};
|
||||
|
||||
target.prototype = source.prototype;
|
||||
target.parse = source.parse;
|
||||
target.UTC = source.UTC;
|
||||
target.prototype.toUTCString = source.prototype.toUTCString;
|
||||
target.isFake = true;
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
//eslint-disable-next-line jsdoc/require-jsdoc
|
||||
function createDate() {
|
||||
/**
|
||||
* @param {number} year
|
||||
* @param {number} month
|
||||
* @param {number} date
|
||||
* @param {number} hour
|
||||
* @param {number} minute
|
||||
* @param {number} second
|
||||
* @param {number} ms
|
||||
* @returns {Date}
|
||||
*/
|
||||
function ClockDate(year, month, date, hour, minute, second, ms) {
|
||||
// the Date constructor called as a function, ref Ecma-262 Edition 5.1, section 15.9.2.
|
||||
// This remains so in the 10th edition of 2019 as well.
|
||||
if (!(this instanceof ClockDate)) {
|
||||
return new NativeDate(ClockDate.clock.now).toString();
|
||||
class ClockDate extends NativeDate {
|
||||
/**
|
||||
* @param {number} year
|
||||
* @param {number} month
|
||||
* @param {number} date
|
||||
* @param {number} hour
|
||||
* @param {number} minute
|
||||
* @param {number} second
|
||||
* @param {number} ms
|
||||
* @returns void
|
||||
*/
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
constructor(year, month, date, hour, minute, second, ms) {
|
||||
// Defensive and verbose to avoid potential harm in passing
|
||||
// explicit undefined when user does not pass argument
|
||||
if (arguments.length === 0) {
|
||||
super(ClockDate.clock.now);
|
||||
} else {
|
||||
super(...arguments);
|
||||
}
|
||||
}
|
||||
|
||||
// if Date is called as a constructor with 'new' keyword
|
||||
// Defensive and verbose to avoid potential harm in passing
|
||||
// explicit undefined when user does not pass argument
|
||||
switch (arguments.length) {
|
||||
case 0:
|
||||
return new NativeDate(ClockDate.clock.now);
|
||||
case 1:
|
||||
return new NativeDate(year);
|
||||
case 2:
|
||||
return new NativeDate(year, month);
|
||||
case 3:
|
||||
return new NativeDate(year, month, date);
|
||||
case 4:
|
||||
return new NativeDate(year, month, date, hour);
|
||||
case 5:
|
||||
return new NativeDate(year, month, date, hour, minute);
|
||||
case 6:
|
||||
return new NativeDate(
|
||||
year,
|
||||
month,
|
||||
date,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
);
|
||||
default:
|
||||
return new NativeDate(
|
||||
year,
|
||||
month,
|
||||
date,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
ms,
|
||||
);
|
||||
static [Symbol.hasInstance](instance) {
|
||||
return instance instanceof NativeDate;
|
||||
}
|
||||
}
|
||||
|
||||
return mirrorDateProperties(ClockDate, NativeDate);
|
||||
ClockDate.isFake = true;
|
||||
|
||||
if (NativeDate.now) {
|
||||
ClockDate.now = function now() {
|
||||
return ClockDate.clock.now;
|
||||
};
|
||||
}
|
||||
|
||||
if (NativeDate.toSource) {
|
||||
ClockDate.toSource = function toSource() {
|
||||
return NativeDate.toSource();
|
||||
};
|
||||
}
|
||||
|
||||
ClockDate.toString = function toString() {
|
||||
return NativeDate.toString();
|
||||
};
|
||||
|
||||
// noinspection UnnecessaryLocalVariableJS
|
||||
/**
|
||||
* A normal Class constructor cannot be called without `new`, but Date can, so we need
|
||||
* to wrap it in a Proxy in order to ensure this functionality of Date is kept intact
|
||||
*
|
||||
* @type {ClockDate}
|
||||
*/
|
||||
const ClockDateProxy = new Proxy(ClockDate, {
|
||||
// handler for [[Call]] invocations (i.e. not using `new`)
|
||||
apply() {
|
||||
// the Date constructor called as a function, ref Ecma-262 Edition 5.1, section 15.9.2.
|
||||
// This remains so in the 10th edition of 2019 as well.
|
||||
if (this instanceof ClockDate) {
|
||||
throw new TypeError(
|
||||
"A Proxy should only capture `new` calls with the `construct` handler. This is not supposed to be possible, so check the logic.",
|
||||
);
|
||||
}
|
||||
|
||||
return new NativeDate(ClockDate.clock.now).toString();
|
||||
},
|
||||
});
|
||||
|
||||
return ClockDateProxy;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -931,6 +947,16 @@ function withGlobal(_global) {
|
|||
timersModule[entry.methodName] = entry.original;
|
||||
}
|
||||
}
|
||||
if (clock.timersPromisesModuleMethods !== undefined) {
|
||||
for (
|
||||
let j = 0;
|
||||
j < clock.timersPromisesModuleMethods.length;
|
||||
j++
|
||||
) {
|
||||
const entry = clock.timersPromisesModuleMethods[j];
|
||||
timersPromisesModule[entry.methodName] = entry.original;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (config.shouldAdvanceTime === true) {
|
||||
|
|
@ -940,6 +966,11 @@ function withGlobal(_global) {
|
|||
// Prevent multiple executions which will completely remove these props
|
||||
clock.methods = [];
|
||||
|
||||
for (const [listener, signal] of clock.abortListenerMap.entries()) {
|
||||
signal.removeEventListener("abort", listener);
|
||||
clock.abortListenerMap.delete(listener);
|
||||
}
|
||||
|
||||
// return pending timers, to enable checking what timers remained on uninstall
|
||||
if (!clock.timers) {
|
||||
return [];
|
||||
|
|
@ -962,8 +993,7 @@ function withGlobal(_global) {
|
|||
clock[`_${method}`] = target[method];
|
||||
|
||||
if (method === "Date") {
|
||||
const date = mirrorDateProperties(clock[method], target[method]);
|
||||
target[method] = date;
|
||||
target[method] = clock[method];
|
||||
} else if (method === "Intl") {
|
||||
target[method] = clock[method];
|
||||
} else if (method === "performance") {
|
||||
|
|
@ -1042,44 +1072,47 @@ function withGlobal(_global) {
|
|||
Date: _global.Date,
|
||||
};
|
||||
|
||||
if (setImmediatePresent) {
|
||||
if (isPresent.setImmediate) {
|
||||
timers.setImmediate = _global.setImmediate;
|
||||
}
|
||||
|
||||
if (isPresent.clearImmediate) {
|
||||
timers.clearImmediate = _global.clearImmediate;
|
||||
}
|
||||
|
||||
if (hrtimePresent) {
|
||||
if (isPresent.hrtime) {
|
||||
timers.hrtime = _global.process.hrtime;
|
||||
}
|
||||
|
||||
if (nextTickPresent) {
|
||||
if (isPresent.nextTick) {
|
||||
timers.nextTick = _global.process.nextTick;
|
||||
}
|
||||
|
||||
if (performancePresent) {
|
||||
if (isPresent.performance) {
|
||||
timers.performance = _global.performance;
|
||||
}
|
||||
|
||||
if (requestAnimationFramePresent) {
|
||||
if (isPresent.requestAnimationFrame) {
|
||||
timers.requestAnimationFrame = _global.requestAnimationFrame;
|
||||
}
|
||||
|
||||
if (queueMicrotaskPresent) {
|
||||
timers.queueMicrotask = true;
|
||||
if (isPresent.queueMicrotask) {
|
||||
timers.queueMicrotask = _global.queueMicrotask;
|
||||
}
|
||||
|
||||
if (cancelAnimationFramePresent) {
|
||||
if (isPresent.cancelAnimationFrame) {
|
||||
timers.cancelAnimationFrame = _global.cancelAnimationFrame;
|
||||
}
|
||||
|
||||
if (requestIdleCallbackPresent) {
|
||||
if (isPresent.requestIdleCallback) {
|
||||
timers.requestIdleCallback = _global.requestIdleCallback;
|
||||
}
|
||||
|
||||
if (cancelIdleCallbackPresent) {
|
||||
if (isPresent.cancelIdleCallback) {
|
||||
timers.cancelIdleCallback = _global.cancelIdleCallback;
|
||||
}
|
||||
|
||||
if (intlPresent) {
|
||||
if (isPresent.Intl) {
|
||||
timers.Intl = _global.Intl;
|
||||
}
|
||||
|
||||
|
|
@ -1098,13 +1131,6 @@ function withGlobal(_global) {
|
|||
let nanos = 0;
|
||||
const adjustedSystemTime = [0, 0]; // [millis, nanoremainder]
|
||||
|
||||
if (NativeDate === undefined) {
|
||||
throw new Error(
|
||||
"The global scope doesn't have a `Date` object" +
|
||||
" (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)",
|
||||
);
|
||||
}
|
||||
|
||||
const clock = {
|
||||
now: start,
|
||||
Date: createDate(),
|
||||
|
|
@ -1165,14 +1191,14 @@ function withGlobal(_global) {
|
|||
return millis;
|
||||
}
|
||||
|
||||
if (hrtimeBigintPresent) {
|
||||
if (isPresent.hrtimeBigint) {
|
||||
hrtime.bigint = function () {
|
||||
const parts = hrtime();
|
||||
return BigInt(parts[0]) * BigInt(1e9) + BigInt(parts[1]); // eslint-disable-line
|
||||
};
|
||||
}
|
||||
|
||||
if (intlPresent) {
|
||||
if (isPresent.Intl) {
|
||||
clock.Intl = createIntl();
|
||||
clock.Intl.clock = clock;
|
||||
}
|
||||
|
|
@ -1257,7 +1283,7 @@ function withGlobal(_global) {
|
|||
return clearTimer(clock, timerId, "Interval");
|
||||
};
|
||||
|
||||
if (setImmediatePresent) {
|
||||
if (isPresent.setImmediate) {
|
||||
clock.setImmediate = function setImmediate(func) {
|
||||
return addTimer(clock, {
|
||||
func: func,
|
||||
|
|
@ -1696,12 +1722,12 @@ function withGlobal(_global) {
|
|||
clock.tick(ms);
|
||||
};
|
||||
|
||||
if (performancePresent) {
|
||||
if (isPresent.performance) {
|
||||
clock.performance = Object.create(null);
|
||||
clock.performance.now = fakePerformanceNow;
|
||||
}
|
||||
|
||||
if (hrtimePresent) {
|
||||
if (isPresent.hrtime) {
|
||||
clock.hrtime = hrtime;
|
||||
}
|
||||
|
||||
|
|
@ -1749,6 +1775,20 @@ function withGlobal(_global) {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} timer/object the name of the thing that is not present
|
||||
* @param timer
|
||||
*/
|
||||
function handleMissingTimer(timer) {
|
||||
if (config.ignoreMissingTimers) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new ReferenceError(
|
||||
`non-existent timers and/or objects cannot be faked: '${timer}'`,
|
||||
);
|
||||
}
|
||||
|
||||
let i, l;
|
||||
const clock = createClock(config.now, config.loopLimit);
|
||||
clock.shouldClearNativeTimers = config.shouldClearNativeTimers;
|
||||
|
|
@ -1757,13 +1797,12 @@ function withGlobal(_global) {
|
|||
return uninstall(clock, config);
|
||||
};
|
||||
|
||||
clock.abortListenerMap = new Map();
|
||||
|
||||
clock.methods = config.toFake || [];
|
||||
|
||||
if (clock.methods.length === 0) {
|
||||
// do not fake nextTick by default - GitHub#126
|
||||
clock.methods = Object.keys(timers).filter(function (key) {
|
||||
return key !== "nextTick" && key !== "queueMicrotask";
|
||||
});
|
||||
clock.methods = Object.keys(timers);
|
||||
}
|
||||
|
||||
if (config.shouldAdvanceTime === true) {
|
||||
|
|
@ -1797,18 +1836,30 @@ function withGlobal(_global) {
|
|||
: NOOP;
|
||||
}
|
||||
});
|
||||
// ensure `mark` returns a value that is valid
|
||||
clock.performance.mark = (name) =>
|
||||
new FakePerformanceEntry(name, "mark", 0, 0);
|
||||
clock.performance.measure = (name) =>
|
||||
new FakePerformanceEntry(name, "measure", 0, 100);
|
||||
} else if ((config.toFake || []).includes("performance")) {
|
||||
// user explicitly tried to fake performance when not present
|
||||
throw new ReferenceError(
|
||||
"non-existent performance object cannot be faked",
|
||||
);
|
||||
return handleMissingTimer("performance");
|
||||
}
|
||||
}
|
||||
if (_global === globalObject && timersModule) {
|
||||
clock.timersModuleMethods = [];
|
||||
}
|
||||
if (_global === globalObject && timersPromisesModule) {
|
||||
clock.timersPromisesModuleMethods = [];
|
||||
}
|
||||
for (i = 0, l = clock.methods.length; i < l; i++) {
|
||||
const nameOfMethodToReplace = clock.methods[i];
|
||||
|
||||
if (!isPresent[nameOfMethodToReplace]) {
|
||||
handleMissingTimer(nameOfMethodToReplace);
|
||||
// eslint-disable-next-line
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nameOfMethodToReplace === "hrtime") {
|
||||
if (
|
||||
_global.process &&
|
||||
|
|
@ -1838,6 +1889,239 @@ function withGlobal(_global) {
|
|||
timersModule[nameOfMethodToReplace] =
|
||||
_global[nameOfMethodToReplace];
|
||||
}
|
||||
if (clock.timersPromisesModuleMethods !== undefined) {
|
||||
if (nameOfMethodToReplace === "setTimeout") {
|
||||
clock.timersPromisesModuleMethods.push({
|
||||
methodName: "setTimeout",
|
||||
original: timersPromisesModule.setTimeout,
|
||||
});
|
||||
|
||||
timersPromisesModule.setTimeout = (
|
||||
delay,
|
||||
value,
|
||||
options = {},
|
||||
) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const abort = () => {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
|
||||
// This is safe, there is no code path that leads to this function
|
||||
// being invoked before handle has been assigned.
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
clock.clearTimeout(handle);
|
||||
reject(options.signal.reason);
|
||||
};
|
||||
|
||||
const handle = clock.setTimeout(() => {
|
||||
if (options.signal) {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
}
|
||||
|
||||
resolve(value);
|
||||
}, delay);
|
||||
|
||||
if (options.signal) {
|
||||
if (options.signal.aborted) {
|
||||
abort();
|
||||
} else {
|
||||
options.signal.addEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.set(
|
||||
abort,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if (nameOfMethodToReplace === "setImmediate") {
|
||||
clock.timersPromisesModuleMethods.push({
|
||||
methodName: "setImmediate",
|
||||
original: timersPromisesModule.setImmediate,
|
||||
});
|
||||
|
||||
timersPromisesModule.setImmediate = (value, options = {}) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const abort = () => {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
|
||||
// This is safe, there is no code path that leads to this function
|
||||
// being invoked before handle has been assigned.
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
clock.clearImmediate(handle);
|
||||
reject(options.signal.reason);
|
||||
};
|
||||
|
||||
const handle = clock.setImmediate(() => {
|
||||
if (options.signal) {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
}
|
||||
|
||||
resolve(value);
|
||||
});
|
||||
|
||||
if (options.signal) {
|
||||
if (options.signal.aborted) {
|
||||
abort();
|
||||
} else {
|
||||
options.signal.addEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.set(
|
||||
abort,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if (nameOfMethodToReplace === "setInterval") {
|
||||
clock.timersPromisesModuleMethods.push({
|
||||
methodName: "setInterval",
|
||||
original: timersPromisesModule.setInterval,
|
||||
});
|
||||
|
||||
timersPromisesModule.setInterval = (
|
||||
delay,
|
||||
value,
|
||||
options = {},
|
||||
) => ({
|
||||
[Symbol.asyncIterator]: () => {
|
||||
const createResolvable = () => {
|
||||
let resolve, reject;
|
||||
const promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
promise.resolve = resolve;
|
||||
promise.reject = reject;
|
||||
return promise;
|
||||
};
|
||||
|
||||
let done = false;
|
||||
let hasThrown = false;
|
||||
let returnCall;
|
||||
let nextAvailable = 0;
|
||||
const nextQueue = [];
|
||||
|
||||
const handle = clock.setInterval(() => {
|
||||
if (nextQueue.length > 0) {
|
||||
nextQueue.shift().resolve();
|
||||
} else {
|
||||
nextAvailable++;
|
||||
}
|
||||
}, delay);
|
||||
|
||||
const abort = () => {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
|
||||
clock.clearInterval(handle);
|
||||
done = true;
|
||||
for (const resolvable of nextQueue) {
|
||||
resolvable.resolve();
|
||||
}
|
||||
};
|
||||
|
||||
if (options.signal) {
|
||||
if (options.signal.aborted) {
|
||||
done = true;
|
||||
} else {
|
||||
options.signal.addEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.set(
|
||||
abort,
|
||||
options.signal,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
next: async () => {
|
||||
if (options.signal?.aborted && !hasThrown) {
|
||||
hasThrown = true;
|
||||
throw options.signal.reason;
|
||||
}
|
||||
|
||||
if (done) {
|
||||
return { done: true, value: undefined };
|
||||
}
|
||||
|
||||
if (nextAvailable > 0) {
|
||||
nextAvailable--;
|
||||
return { done: false, value: value };
|
||||
}
|
||||
|
||||
const resolvable = createResolvable();
|
||||
nextQueue.push(resolvable);
|
||||
|
||||
await resolvable;
|
||||
|
||||
if (returnCall && nextQueue.length === 0) {
|
||||
returnCall.resolve();
|
||||
}
|
||||
|
||||
if (options.signal?.aborted && !hasThrown) {
|
||||
hasThrown = true;
|
||||
throw options.signal.reason;
|
||||
}
|
||||
|
||||
if (done) {
|
||||
return { done: true, value: undefined };
|
||||
}
|
||||
|
||||
return { done: false, value: value };
|
||||
},
|
||||
return: async () => {
|
||||
if (done) {
|
||||
return { done: true, value: undefined };
|
||||
}
|
||||
|
||||
if (nextQueue.length > 0) {
|
||||
returnCall = createResolvable();
|
||||
await returnCall;
|
||||
}
|
||||
|
||||
clock.clearInterval(handle);
|
||||
done = true;
|
||||
|
||||
if (options.signal) {
|
||||
options.signal.removeEventListener(
|
||||
"abort",
|
||||
abort,
|
||||
);
|
||||
clock.abortListenerMap.delete(abort);
|
||||
}
|
||||
|
||||
return { done: true, value: undefined };
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return clock;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue