Skip to content

Commit

Permalink
fix(timer.refresh): should just change callAt (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Mar 3, 2022
1 parent fc56a4d commit 75950c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,14 @@ function withGlobal(_global) {
return this.refed;
},
refresh: function () {
clearTimeout(timer.id);
const args = [timer.func, timer.delay].concat(timer.args);
return setTimeout.apply(null, args);
timer.callAt =
clock.now +
(parseInt(timer.delay) || (clock.duringTick ? 1 : 0));

// it _might_ have been removed, but if not the assignment is perfectly fine
clock.timers[timer.id] = timer;

return res;
},
[Symbol.toPrimitive]: function () {
return timer.id;
Expand Down
24 changes: 15 additions & 9 deletions test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3607,16 +3607,16 @@ describe("FakeTimers", function () {
}
});

it("global fake setTimeout().refresh() should return timer", function () {
it("global fake setTimeout().refresh() should return same timer", function () {
this.clock = FakeTimers.install();
const stub = sinon.stub();

if (typeof setTimeout(NOOP, 0) === "object") {
const to = setTimeout(stub, 1000).refresh();
assert.isNumber(Number(to));
assert.isFunction(to.ref);
assert.isFunction(to.refresh);
const timeout = setTimeout(stub, 1000);
const to = timeout.refresh();
assert(timeout === to);
}
this.clock.uninstall();
});

it("replaces global clearTimeout", function () {
Expand Down Expand Up @@ -4834,7 +4834,7 @@ describe("#368 - timeout.refresh setTimeout arguments", function () {
}
});

it("should forward arguments passed to setTimeout", function () {
it("should forward arguments passed to setTimeout", function () {
const clock = FakeTimers.install();
const stub = sinon.stub();

Expand Down Expand Up @@ -4863,13 +4863,19 @@ describe("#187 - Support timeout.refresh in node environments", function () {
clock.uninstall();
});

it("assigns a new id to the refreshed timer", function () {
it("only calls stub once if not fired at time of refresh", function () {
const clock = FakeTimers.install();
const stub = sinon.stub();

if (typeof setTimeout(NOOP, 0) === "object") {
const t = setTimeout(stub, 1000);
const t2 = t.refresh();
refute.same(Number(t), Number(t2));
clock.tick(999);
assert(stub.notCalled);
t.refresh();
clock.tick(999);
assert(stub.notCalled);
clock.tick(1);
assert(stub.calledOnce);
}
clock.uninstall();
});
Expand Down

0 comments on commit 75950c0

Please sign in to comment.