timers: add hasRef method to Timeout & Immediate
Provide a way to check whether the current timer or immediate is refed. PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
de732725d8
commit
48a2568f41
@ -23,6 +23,15 @@ running as long as the immediate is active. The `Immediate` object returned by
|
|||||||
[`setImmediate()`][] exports both `immediate.ref()` and `immediate.unref()`
|
[`setImmediate()`][] exports both `immediate.ref()` and `immediate.unref()`
|
||||||
functions that can be used to control this default behavior.
|
functions that can be used to control this default behavior.
|
||||||
|
|
||||||
|
### immediate.hasRef()
|
||||||
|
<!-- YAML
|
||||||
|
added: REPLACEME
|
||||||
|
-->
|
||||||
|
|
||||||
|
* Returns: {boolean}
|
||||||
|
|
||||||
|
If true, the `Immediate` object will keep the Node.js event loop active.
|
||||||
|
|
||||||
### immediate.ref()
|
### immediate.ref()
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v9.7.0
|
added: v9.7.0
|
||||||
@ -61,6 +70,15 @@ timer is active. Each of the `Timeout` objects returned by these functions
|
|||||||
export both `timeout.ref()` and `timeout.unref()` functions that can be used to
|
export both `timeout.ref()` and `timeout.unref()` functions that can be used to
|
||||||
control this default behavior.
|
control this default behavior.
|
||||||
|
|
||||||
|
### timeout.hasRef()
|
||||||
|
<!-- YAML
|
||||||
|
added: REPLACEME
|
||||||
|
-->
|
||||||
|
|
||||||
|
* Returns: {boolean}
|
||||||
|
|
||||||
|
If true, the `Timeout` object will keep the Node.js event loop active.
|
||||||
|
|
||||||
### timeout.ref()
|
### timeout.ref()
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.9.1
|
added: v0.9.1
|
||||||
|
@ -547,6 +547,10 @@ Timeout.prototype.ref = function() {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Timeout.prototype.hasRef = function() {
|
||||||
|
return !!this[kRefed];
|
||||||
|
};
|
||||||
|
|
||||||
Timeout.prototype.close = function() {
|
Timeout.prototype.close = function() {
|
||||||
clearTimeout(this);
|
clearTimeout(this);
|
||||||
return this;
|
return this;
|
||||||
@ -622,7 +626,7 @@ function processImmediate() {
|
|||||||
count++;
|
count++;
|
||||||
if (immediate[kRefed])
|
if (immediate[kRefed])
|
||||||
refCount++;
|
refCount++;
|
||||||
immediate[kRefed] = undefined;
|
immediate[kRefed] = null;
|
||||||
|
|
||||||
tryOnImmediate(immediate, tail, count, refCount);
|
tryOnImmediate(immediate, tail, count, refCount);
|
||||||
|
|
||||||
@ -713,6 +717,10 @@ const Immediate = class Immediate {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasRef() {
|
||||||
|
return !!this[kRefed];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function setImmediate(callback, arg1, arg2, arg3) {
|
function setImmediate(callback, arg1, arg2, arg3) {
|
||||||
@ -761,7 +769,7 @@ exports.clearImmediate = function clearImmediate(immediate) {
|
|||||||
|
|
||||||
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
|
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
|
||||||
toggleImmediateRef(false);
|
toggleImmediateRef(false);
|
||||||
immediate[kRefed] = undefined;
|
immediate[kRefed] = null;
|
||||||
|
|
||||||
if (destroyHooksExist()) {
|
if (destroyHooksExist()) {
|
||||||
emitDestroy(immediate[async_id_symbol]);
|
emitDestroy(immediate[async_id_symbol]);
|
||||||
|
@ -3,6 +3,14 @@
|
|||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const Countdown = require('../common/countdown');
|
const Countdown = require('../common/countdown');
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
const immediate = setImmediate(() => {});
|
||||||
|
assert.strictEqual(immediate.hasRef(), true);
|
||||||
|
immediate.unref();
|
||||||
|
assert.strictEqual(immediate.hasRef(), false);
|
||||||
|
clearImmediate(immediate);
|
||||||
|
|
||||||
// This immediate should execute as it was unrefed and refed again.
|
// This immediate should execute as it was unrefed and refed again.
|
||||||
// It also confirms that unref/ref are chainable.
|
// It also confirms that unref/ref are chainable.
|
||||||
setImmediate(common.mustCall(firstStep)).ref().unref().unref().ref();
|
setImmediate(common.mustCall(firstStep)).ref().unref().unref().ref();
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
let unref_interval = false;
|
let unref_interval = false;
|
||||||
let unref_timer = false;
|
let unref_timer = false;
|
||||||
let checks = 0;
|
let checks = 0;
|
||||||
@ -30,8 +32,12 @@ let checks = 0;
|
|||||||
const LONG_TIME = 10 * 1000;
|
const LONG_TIME = 10 * 1000;
|
||||||
const SHORT_TIME = 100;
|
const SHORT_TIME = 100;
|
||||||
|
|
||||||
|
const timer = setTimeout(() => {}, 10);
|
||||||
|
assert.strictEqual(timer.hasRef(), true);
|
||||||
// Should not throw.
|
// Should not throw.
|
||||||
setTimeout(() => {}, 10).unref().ref().unref();
|
timer.unref().ref().unref();
|
||||||
|
assert.strictEqual(timer.hasRef(), false);
|
||||||
|
|
||||||
setInterval(() => {}, 10).unref().ref().unref();
|
setInterval(() => {}, 10).unref().ref().unref();
|
||||||
|
|
||||||
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
|
setInterval(common.mustNotCall('Interval should not fire'), LONG_TIME).unref();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user