Skip to content

Commit

Permalink
Fix issue #390: remove dependency on faking setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Jun 18, 2021
1 parent 836a37c commit 31b1127
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,12 +803,6 @@ function withGlobal(_global) {
} else {
if (_global[method] && _global[method].hadOwnProperty) {
_global[method] = clock[`_${method}`];
if (
method === "clearInterval" &&
config.shouldAdvanceTime === true
) {
_global[method](clock.attachedInterval);
}
} else {
try {
delete _global[method];
Expand All @@ -819,6 +813,10 @@ function withGlobal(_global) {
}
}

if (config.shouldAdvanceTime === true) {
_global.clearInterval(clock.attachedInterval);
}

// Prevent multiple executions which will completely remove these props
clock.methods = [];

Expand Down Expand Up @@ -1602,6 +1600,19 @@ function withGlobal(_global) {
});
}

if (config.shouldAdvanceTime === true) {
const intervalTick = doIntervalTick.bind(
null,
clock,
config.advanceTimeDelta
);
const intervalId = _global.setInterval(
intervalTick,
config.advanceTimeDelta
);
clock.attachedInterval = intervalId;
}

for (i = 0, l = clock.methods.length; i < l; i++) {
if (clock.methods[i] === "hrtime") {
if (
Expand All @@ -1618,21 +1629,6 @@ function withGlobal(_global) {
hijackMethod(_global.process, clock.methods[i], clock);
}
} else {
if (
clock.methods[i] === "setInterval" &&
config.shouldAdvanceTime === true
) {
const intervalTick = doIntervalTick.bind(
null,
clock,
config.advanceTimeDelta
);
const intervalId = _global[clock.methods[i]](
intervalTick,
config.advanceTimeDelta
);
clock.attachedInterval = intervalId;
}
hijackMethod(_global, clock.methods[i], clock);
}
}
Expand Down

0 comments on commit 31b1127

Please sign in to comment.