Skip to content

Commit efff092

Browse files
committed
timers: allow passing delay to timer.refresh()
1 parent d8460de commit efff092

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

doc/api/deprecations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3234,7 +3234,7 @@ Convert them to primitive strings.
32343234
[`setTimeout()`]: timers.md#settimeoutcallback-delay-args
32353235
[`socket.bufferSize`]: net.md#socketbuffersize
32363236
[`timeout.ref()`]: timers.md#timeoutref
3237-
[`timeout.refresh()`]: timers.md#timeoutrefresh
3237+
[`timeout.refresh()`]: timers.md#timeoutrefreshdelay-resetinterval
32383238
[`timeout.unref()`]: timers.md#timeoutunref
32393239
[`tls.CryptoStream`]: tls.md#class-tlscryptostream
32403240
[`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

+23-11
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,25 @@ function initAsyncResource(resource, type) {
158158
if (initHooksExist())
159159
emitInit(asyncId, type, triggerAsyncId, resource);
160160
}
161+
function calcAfter(after) {
162+
after *= 1; // Coalesce to number or NaN
163+
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
164+
if (after > TIMEOUT_MAX) {
165+
process.emitWarning(`${after} does not fit into` +
166+
' a 32-bit signed integer.' +
167+
'\nTimeout duration was set to 1.',
168+
'TimeoutOverflowWarning');
169+
}
170+
after = 1; // Schedule on next tick, follows browser behavior
171+
}
172+
return after;
173+
}
174+
161175
class Timeout {
162176
// Timer constructor function.
163177
// The entire prototype is defined in lib/timers.js
164178
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-
}
179+
after = calcAfter(after);
175180

176181
this._idleTimeout = after;
177182
this._idlePrev = this;
@@ -204,7 +209,14 @@ class Timeout {
204209
});
205210
}
206211

207-
refresh() {
212+
refresh(after, resetInterval) {
213+
if (after !== undefined) {
214+
after = calcAfter(after);
215+
this._idleTimeout = after;
216+
if (this._repeat !== null && resetInterval)
217+
this._repeat = after;
218+
}
219+
208220
if (this[kRefed])
209221
active(this);
210222
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)