Skip to content

Commit 4ed6584

Browse files
committed
timers: allow passing delay to timer.refresh()
1 parent 8d0f49c commit 4ed6584

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

doc/api/deprecations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3242,7 +3242,7 @@ Convert them to primitive strings.
32423242
[`setTimeout()`]: timers.md#settimeoutcallback-delay-args
32433243
[`socket.bufferSize`]: net.md#socketbuffersize
32443244
[`timeout.ref()`]: timers.md#timeoutref
3245-
[`timeout.refresh()`]: timers.md#timeoutrefresh
3245+
[`timeout.refresh()`]: timers.md#timeoutrefreshdelay-resetinterval
32463246
[`timeout.unref()`]: timers.md#timeoutunref
32473247
[`tls.CryptoStream`]: tls.md#class-tlscryptostream
32483248
[`tls.SecureContext`]: tls.md#tlscreatesecurecontextoptions

doc/api/timers.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,26 @@ When called, requests that the Node.js event loop _not_ exit so long as the
111111
By default, all `Timeout` objects are "ref'ed", making it normally unnecessary
112112
to call `timeout.ref()` unless `timeout.unref()` had been called previously.
113113

114-
### `timeout.refresh()`
114+
### `timeout.refresh([delay[, resetInterval]])`
115115

116116
<!-- YAML
117117
added: v10.2.0
118+
changes:
119+
- version: REPLACEME
120+
pr-url: https://github.com/nodejs/node/pull/40434
121+
description: Added `delay` and `resetInterval` parameters.
118122
-->
119123

124+
* `delay` {number} The number of milliseconds to wait before calling the
125+
original callback.
126+
* `resetInterval` {boolean} For an interval timer, should the interval be set to
127+
`delay`? **Default:** `false`.
120128
* Returns: {Timeout} a reference to `timeout`
121129

122130
Sets the timer's start time to the current time, and reschedules the timer to
123-
call its callback at the previously specified duration adjusted to the current
124-
time. This is useful for refreshing a timer without allocating a new
125-
JavaScript object.
131+
call its callback at the previously specified duration or the duration specified
132+
by `delay` adjusted to the current time. This is useful for refreshing a timer
133+
without allocating a new JavaScript object.
126134

127135
Using this on a timer that has already called its callback will reactivate the
128136
timer.

lib/internal/timers.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,26 @@ function initAsyncResource(resource, type) {
158158
if (initHooksExist())
159159
emitInit(asyncId, type, triggerAsyncId, resource);
160160
}
161+
162+
function calcAfter(after) {
163+
after *= 1; // Coalesce to number or NaN
164+
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
165+
if (after > TIMEOUT_MAX) {
166+
process.emitWarning(`${after} does not fit into` +
167+
' a 32-bit signed integer.' +
168+
'\nTimeout duration was set to 1.',
169+
'TimeoutOverflowWarning');
170+
}
171+
after = 1; // Schedule on next tick, follows browser behavior
172+
}
173+
return after;
174+
}
175+
161176
class Timeout {
162177
// Timer constructor function.
163178
// The entire prototype is defined in lib/timers.js
164179
constructor(callback, after, args, isRepeat, isRefed) {
165-
after *= 1; // Coalesce to number or NaN
166-
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
167-
if (after > TIMEOUT_MAX) {
168-
process.emitWarning(`${after} does not fit into` +
169-
' a 32-bit signed integer.' +
170-
'\nTimeout duration was set to 1.',
171-
'TimeoutOverflowWarning');
172-
}
173-
after = 1; // Schedule on next tick, follows browser behavior
174-
}
180+
after = calcAfter(after);
175181

176182
this._idleTimeout = after;
177183
this._idlePrev = this;
@@ -204,7 +210,14 @@ class Timeout {
204210
});
205211
}
206212

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

0 commit comments

Comments
 (0)