diff options
author | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2018-01-09 13:12:06 -0500 |
---|---|---|
committer | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2018-01-26 16:47:16 -0500 |
commit | bb5575aa75fd3071724d5eccde39a3041e1af57a (patch) | |
tree | 78b2dba7b447d2e6a9e3f12348956643473577d4 /lib/internal/timers.js | |
parent | 54fe0a6cbb7b36b8169e7c8dd21dcaeec3da0035 (diff) | |
download | android-node-v8-bb5575aa75fd3071724d5eccde39a3041e1af57a.tar.gz android-node-v8-bb5575aa75fd3071724d5eccde39a3041e1af57a.tar.bz2 android-node-v8-bb5575aa75fd3071724d5eccde39a3041e1af57a.zip |
timers: add internal [@@ refresh()] function
Hidden via a symbol because I'm unsure exactly what the API should look
like in the end.
Removes the need to use _unrefActive for efficiently refreshing
timeouts.
It still uses it under the hood but that could be replaced with
insert() directly if it were in the same file.
PR-URL: https://github.com/nodejs/node/pull/18065
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'lib/internal/timers.js')
-rw-r--r-- | lib/internal/timers.js | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/internal/timers.js b/lib/internal/timers.js index cd5efb6dbb..aa061be3db 100644 --- a/lib/internal/timers.js +++ b/lib/internal/timers.js @@ -19,12 +19,16 @@ const errors = require('internal/errors'); // Timeout values > TIMEOUT_MAX are set to 1. const TIMEOUT_MAX = 2 ** 31 - 1; +const refreshFnSymbol = Symbol('refresh()'); +const unrefedSymbol = Symbol('unrefed'); + module.exports = { TIMEOUT_MAX, kTimeout: Symbol('timeout'), // For hiding Timeouts on other internals. async_id_symbol, trigger_async_id_symbol, Timeout, + refreshFnSymbol, setUnrefTimeout, validateTimerDuration }; @@ -39,7 +43,7 @@ function getTimers() { // Timer constructor function. // The entire prototype is defined in lib/timers.js -function Timeout(callback, after, args, isRepeat) { +function Timeout(callback, after, args, isRepeat, isUnrefed) { after *= 1; // coalesce to number or NaN if (!(after >= 1 && after <= TIMEOUT_MAX)) { if (after > TIMEOUT_MAX) { @@ -64,6 +68,8 @@ function Timeout(callback, after, args, isRepeat) { this._repeat = isRepeat ? after : null; this._destroyed = false; + this[unrefedSymbol] = isUnrefed; + this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); if (async_hook_fields[kInit] > 0) { @@ -74,6 +80,19 @@ function Timeout(callback, after, args, isRepeat) { } } +Timeout.prototype[refreshFnSymbol] = function refresh() { + if (this._handle) { + // Would be more ideal with uv_timer_again(), however that API does not + // cause libuv's sorted timers data structure (a binary heap at the time + // of writing) to re-sort itself. This causes ordering inconsistencies. + this._handle.stop(); + this._handle.start(this._idleTimeout); + } else if (this[unrefedSymbol]) { + getTimers()._unrefActive(this); + } else { + getTimers().active(this); + } +}; function setUnrefTimeout(callback, after, arg1, arg2, arg3) { // Type checking identical to setTimeout() @@ -102,7 +121,7 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) { break; } - const timer = new Timeout(callback, after, args, false); + const timer = new Timeout(callback, after, args, false, true); getTimers()._unrefActive(timer); return timer; |