Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 56530f0

Browse filesBrowse files
Fishrock123targos
authored andcommitted
timers: make timer.refresh() a public API
Originally added in bb5575a discussions such as #20261 show the usefulness of this API to the Node.js ecosystem. PR-URL: #20298 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
1 parent 41e1dc0 commit 56530f0
Copy full SHA for 56530f0

File tree

Expand file treeCollapse file tree

5 files changed

+44
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+44
-14
lines changed
Open diff view settings
Collapse file

‎doc/api/timers.md‎

Copy file name to clipboardExpand all lines: doc/api/timers.md
+15Lines changed: 15 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ When called, requests that the Node.js event loop *not* exit so long as the
7474
By default, all `Timeout` objects are "ref'ed", making it normally unnecessary
7575
to call `timeout.ref()` unless `timeout.unref()` had been called previously.
7676

77+
### timeout.refresh()
78+
<!-- YAML
79+
added: REPLACEME
80+
-->
81+
82+
* Returns: {Timeout} a reference to `timeout`
83+
84+
Sets the timer's start time to the current time, and reschedules the timer to
85+
call its callback at the previously specified duration adjusted to the current
86+
time. This is useful for refreshing a timer without allocating a new
87+
JavaScript object.
88+
89+
Using this on a timer that has already called its callback will reactivate the
90+
timer.
91+
7792
### timeout.unref()
7893
<!-- YAML
7994
added: v0.9.1
Collapse file

‎lib/internal/http2/core.js‎

Copy file name to clipboardExpand all lines: lib/internal/http2/core.js
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ const {
107107
const {
108108
kTimeout,
109109
setUnrefTimeout,
110-
validateTimerDuration,
111-
refreshFnSymbol
110+
validateTimerDuration
112111
} = require('internal/timers');
113112
const {
114113
createWriteWrap,
@@ -962,7 +961,7 @@ class Http2Session extends EventEmitter {
962961
[kUpdateTimer]() {
963962
if (this.destroyed)
964963
return;
965-
if (this[kTimeout]) this[kTimeout][refreshFnSymbol]();
964+
if (this[kTimeout]) this[kTimeout].refresh();
966965
}
967966

968967
// Sets the id of the next stream to be created by this Http2Session.
@@ -1539,7 +1538,7 @@ class Http2Stream extends Duplex {
15391538
if (this.destroyed)
15401539
return;
15411540
if (this[kTimeout])
1542-
this[kTimeout][refreshFnSymbol]();
1541+
this[kTimeout].refresh();
15431542
if (this[kSession])
15441543
this[kSession][kUpdateTimer]();
15451544
}
Collapse file

‎lib/internal/timers.js‎

Copy file name to clipboardExpand all lines: lib/internal/timers.js
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
// Timeout values > TIMEOUT_MAX are set to 1.
2020
const TIMEOUT_MAX = 2 ** 31 - 1;
2121

22-
const refreshFnSymbol = Symbol('refresh()');
2322
const unrefedSymbol = Symbol('unrefed');
2423

2524
module.exports = {
@@ -29,7 +28,6 @@ module.exports = {
2928
trigger_async_id_symbol,
3029
Timeout,
3130
initAsyncResource,
32-
refreshFnSymbol,
3331
setUnrefTimeout,
3432
validateTimerDuration
3533
};
@@ -82,7 +80,7 @@ function Timeout(callback, after, args, isRepeat, isUnrefed) {
8280
initAsyncResource(this, 'Timeout');
8381
}
8482

85-
Timeout.prototype[refreshFnSymbol] = function refresh() {
83+
Timeout.prototype.refresh = function() {
8684
if (this._handle) {
8785
// Would be more ideal with uv_timer_again(), however that API does not
8886
// cause libuv's sorted timers data structure (a binary heap at the time
@@ -93,6 +91,8 @@ Timeout.prototype[refreshFnSymbol] = function refresh() {
9391
} else {
9492
getTimers().active(this);
9593
}
94+
95+
return this;
9696
};
9797

9898
function setUnrefTimeout(callback, after, arg1, arg2, arg3) {
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ const exceptionWithHostPort = errors.exceptionWithHostPort;
8989
const {
9090
kTimeout,
9191
setUnrefTimeout,
92-
validateTimerDuration,
93-
refreshFnSymbol
92+
validateTimerDuration
9493
} = require('internal/timers');
9594

9695
function noop() {}
@@ -325,7 +324,7 @@ util.inherits(Socket, stream.Duplex);
325324
Socket.prototype._unrefTimer = function _unrefTimer() {
326325
for (var s = this; s !== null; s = s._parent) {
327326
if (s[kTimeout])
328-
s[kTimeout][refreshFnSymbol]();
327+
s[kTimeout].refresh();
329328
}
330329
};
331330

Collapse file

‎test/parallel/test-timers-refresh.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-timers-refresh.js
+21-4Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const common = require('../common');
66

77
const { strictEqual } = require('assert');
8-
const { setUnrefTimeout, refreshFnSymbol } = require('internal/timers');
8+
const { setUnrefTimeout } = require('internal/timers');
99

1010
// Schedule the unrefed cases first so that the later case keeps the event loop
1111
// active.
@@ -27,7 +27,7 @@ const { setUnrefTimeout, refreshFnSymbol } = require('internal/timers');
2727
strictEqual(called, false, 'unref()\'d timer returned before check');
2828
}), 1);
2929

30-
timer[refreshFnSymbol]();
30+
strictEqual(timer.refresh(), timer);
3131
}
3232

3333
// unref pooled timer
@@ -41,7 +41,7 @@ const { setUnrefTimeout, refreshFnSymbol } = require('internal/timers');
4141
strictEqual(called, false, 'unref pooled timer returned before check');
4242
}), 1);
4343

44-
timer[refreshFnSymbol]();
44+
strictEqual(timer.refresh(), timer);
4545
}
4646

4747
// regular timer
@@ -55,5 +55,22 @@ const { setUnrefTimeout, refreshFnSymbol } = require('internal/timers');
5555
strictEqual(called, false, 'pooled timer returned before check');
5656
}), 1);
5757

58-
timer[refreshFnSymbol]();
58+
strictEqual(timer.refresh(), timer);
59+
}
60+
61+
// interval
62+
{
63+
let called = 0;
64+
const timer = setInterval(common.mustCall(() => {
65+
called += 1;
66+
if (called === 2) {
67+
clearInterval(timer);
68+
}
69+
}, 2), 1);
70+
71+
setTimeout(common.mustCall(() => {
72+
strictEqual(called, 0, 'pooled timer returned before check');
73+
}), 1);
74+
75+
strictEqual(timer.refresh(), timer);
5976
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.