Skip to content

Commit 48a2568

Browse files
committed
timers: add hasRef method to Timeout & Immediate
Provide a way to check whether the current timer or immediate is refed. PR-URL: #20898 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent de73272 commit 48a2568

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

doc/api/timers.md

+18
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ running as long as the immediate is active. The `Immediate` object returned by
2323
[`setImmediate()`][] exports both `immediate.ref()` and `immediate.unref()`
2424
functions that can be used to control this default behavior.
2525

26+
### immediate.hasRef()
27+
<!-- YAML
28+
added: REPLACEME
29+
-->
30+
31+
* Returns: {boolean}
32+
33+
If true, the `Immediate` object will keep the Node.js event loop active.
34+
2635
### immediate.ref()
2736
<!-- YAML
2837
added: v9.7.0
@@ -61,6 +70,15 @@ timer is active. Each of the `Timeout` objects returned by these functions
6170
export both `timeout.ref()` and `timeout.unref()` functions that can be used to
6271
control this default behavior.
6372

73+
### timeout.hasRef()
74+
<!-- YAML
75+
added: REPLACEME
76+
-->
77+
78+
* Returns: {boolean}
79+
80+
If true, the `Timeout` object will keep the Node.js event loop active.
81+
6482
### timeout.ref()
6583
<!-- YAML
6684
added: v0.9.1

lib/timers.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ Timeout.prototype.ref = function() {
547547
return this;
548548
};
549549

550+
Timeout.prototype.hasRef = function() {
551+
return !!this[kRefed];
552+
};
553+
550554
Timeout.prototype.close = function() {
551555
clearTimeout(this);
552556
return this;
@@ -622,7 +626,7 @@ function processImmediate() {
622626
count++;
623627
if (immediate[kRefed])
624628
refCount++;
625-
immediate[kRefed] = undefined;
629+
immediate[kRefed] = null;
626630

627631
tryOnImmediate(immediate, tail, count, refCount);
628632

@@ -713,6 +717,10 @@ const Immediate = class Immediate {
713717
}
714718
return this;
715719
}
720+
721+
hasRef() {
722+
return !!this[kRefed];
723+
}
716724
};
717725

718726
function setImmediate(callback, arg1, arg2, arg3) {
@@ -761,7 +769,7 @@ exports.clearImmediate = function clearImmediate(immediate) {
761769

762770
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
763771
toggleImmediateRef(false);
764-
immediate[kRefed] = undefined;
772+
immediate[kRefed] = null;
765773

766774
if (destroyHooksExist()) {
767775
emitDestroy(immediate[async_id_symbol]);

test/parallel/test-timers-immediate-unref.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
const common = require('../common');
44
const Countdown = require('../common/countdown');
55

6+
const assert = require('assert');
7+
8+
const immediate = setImmediate(() => {});
9+
assert.strictEqual(immediate.hasRef(), true);
10+
immediate.unref();
11+
assert.strictEqual(immediate.hasRef(), false);
12+
clearImmediate(immediate);
13+
614
// This immediate should execute as it was unrefed and refed again.
715
// It also confirms that unref/ref are chainable.
816
setImmediate(common.mustCall(firstStep)).ref().unref().unref().ref();

test/parallel/test-timers-unref.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@
2323

2424
const common = require('../common');
2525

26+
const assert = require('assert');
27+
2628
let unref_interval = false;
2729
let unref_timer = false;
2830
let checks = 0;
2931

3032
const LONG_TIME = 10 * 1000;
3133
const SHORT_TIME = 100;
3234

35+
const timer = setTimeout(() => {}, 10);
36+
assert.strictEqual(timer.hasRef(), true);
3337
// Should not throw.
34-
setTimeout(() => {}, 10).unref().ref().unref();
38+
timer.unref().ref().unref();
39+
assert.strictEqual(timer.hasRef(), false);
40+
3541
setInterval(() => {}, 10).unref().ref().unref();
3642

3743
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();

0 commit comments

Comments
 (0)