Skip to content

Commit 10d58e7

Browse files
committed
timers: allow passing delay to timer.refresh()
1 parent c350c21 commit 10d58e7

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

doc/api/deprecations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2952,7 +2952,7 @@ it was an aborted or graceful destroy.
29522952
[`setTimeout()`]: timers.md#settimeoutcallback-delay-args
29532953
[`socket.bufferSize`]: net.md#socketbuffersize
29542954
[`timeout.ref()`]: timers.md#timeoutref
2955-
[`timeout.refresh()`]: timers.md#timeoutrefresh
2955+
[`timeout.refresh()`]: timers.md#timeoutrefreshdelay-resetinterval
29562956
[`timeout.unref()`]: timers.md#timeoutunref
29572957
[`tls.CryptoStream`]: tls.md#class-tlscryptostream
29582958
[`tls.SecureContext`]: tls.md#tlscreatesecurecontextoptions

doc/api/timers.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,25 @@ When called, requests that the Node.js event loop *not* exit so long as the
105105
By default, all `Timeout` objects are "ref'ed", making it normally unnecessary
106106
to call `timeout.ref()` unless `timeout.unref()` had been called previously.
107107

108-
### `timeout.refresh()`
108+
### `timeout.refresh([delay[, resetInterval]])`
109109
<!-- YAML
110110
added: v10.2.0
111+
changes:
112+
- version: REPLACEME
113+
pr-url: https://github.com/nodejs/node/pull/40434
114+
description: Added `delay` and `resetInterval` parameters.
111115
-->
112116

117+
* `delay` {number} The number of milliseconds to wait before calling the
118+
original callback. **Default:** `1`.
119+
* `resetInterval` {boolean} For an interval timer, should the interval be set to
120+
`delay`? **Default:** `false`.
113121
* Returns: {Timeout} a reference to `timeout`
114122

115123
Sets the timer's start time to the current time, and reschedules the timer to
116-
call its callback at the previously specified duration adjusted to the current
117-
time. This is useful for refreshing a timer without allocating a new
118-
JavaScript object.
124+
call its callback at the previously specified duration or the duration specified
125+
by `delay` adjusted to the current time. This is useful for refreshing a timer
126+
without allocating a new JavaScript object.
119127

120128
Using this on a timer that has already called its callback will reactivate the
121129
timer.

lib/internal/timers.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ function initAsyncResource(resource, type) {
162162
emitInit(asyncId, type, triggerAsyncId, resource);
163163
}
164164

165-
// Timer constructor function.
166-
// The entire prototype is defined in lib/timers.js
167-
function Timeout(callback, after, args, isRepeat, isRefed) {
165+
function calcAfter(after) {
168166
after *= 1; // Coalesce to number or NaN
169167
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
170168
if (after > TIMEOUT_MAX) {
@@ -175,6 +173,13 @@ function Timeout(callback, after, args, isRepeat, isRefed) {
175173
}
176174
after = 1; // Schedule on next tick, follows browser behavior
177175
}
176+
return after;
177+
}
178+
179+
// Timer constructor function.
180+
// The entire prototype is defined in lib/timers.js
181+
function Timeout(callback, after, args, isRepeat, isRefed) {
182+
after = calcAfter(after);
178183

179184
this._idleTimeout = after;
180185
this._idlePrev = this;
@@ -207,7 +212,14 @@ Timeout.prototype[inspect.custom] = function(_, options) {
207212
});
208213
};
209214

210-
Timeout.prototype.refresh = function() {
215+
Timeout.prototype.refresh = function(after, resetInterval) {
216+
if (after !== undefined) {
217+
after = calcAfter(after);
218+
this._idleTimeout = after;
219+
if (this._repeat !== null && resetInterval)
220+
this._repeat = after;
221+
}
222+
211223
if (this[kRefed])
212224
active(this);
213225
else
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
let last;
6+
let t = setTimeout(() => {
7+
if (last !== undefined) {
8+
assert(Date.now() - last < 200);
9+
10+
last = undefined;
11+
let count = 0;
12+
t = setInterval(() => {
13+
if (last !== undefined)
14+
assert(Date.now() - last < 200);
15+
last = Date.now();
16+
switch (count++) {
17+
case 0:
18+
t.refresh(100, true);
19+
case 3:
20+
clearInterval(t);
21+
return;
22+
}
23+
}, 200);
24+
return;
25+
}
26+
last = Date.now();
27+
t.refresh(100);
28+
}, 200);

0 commit comments

Comments
 (0)