| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b483afc commit 522e3d3
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,16 +119,27 @@ function listOnTimeoutNT(list) { | |||
| 119 | 119 | } | |
| 120 | 120 | ||
| 121 | 121 | ||
| 122 | - const unenroll = exports.unenroll = function(item) { | ||
| 122 | + function reuse(item) { | ||
| 123 | 123 | L.remove(item); | |
| 124 | 124 | ||
| 125 | 125 | var list = lists[item._idleTimeout]; | |
| 126 | - // if empty then stop the watcher | ||
| 127 | - debug('unenroll'); | ||
| 126 | + // if empty - reuse the watcher | ||
| 128 | 127 | if (list && L.isEmpty(list)) { | |
| 128 | + debug('reuse hit'); | ||
| 129 | + list.stop(); | ||
| 130 | + delete lists[item._idleTimeout]; | ||
| 131 | + return list; | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + return null; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + | ||
| 138 | + const unenroll = exports.unenroll = function(item) { | ||
| 139 | + var list = reuse(item); | ||
| 140 | + if (list) { | ||
| 129 | 141 | debug('unenroll: list empty'); | |
| 130 | 142 | list.close(); | |
| 131 | - delete lists[item._idleTimeout]; | ||
| 132 | 143 | } | |
| 133 | 144 | // if active is called later, then we want to make sure not to insert again | |
| 134 | 145 | item._idleTimeout = -1; | |
@@ -312,12 +323,16 @@ Timeout.prototype.unref = function() { | |||
| 312 | 323 | if (!this._idleStart) this._idleStart = now; | |
| 313 | 324 | var delay = this._idleStart + this._idleTimeout - now; | |
| 314 | 325 | if (delay < 0) delay = 0; | |
| 315 | - exports.unenroll(this); | ||
| 316 | 326 | ||
| 317 | 327 | // Prevent running cb again when unref() is called during the same cb | |
| 318 | - if (this._called && !this._repeat) return; | ||
| 328 | + if (this._called && !this._repeat) { | ||
| 329 | + exports.unenroll(this); | ||
| 330 | + return; | ||
| 331 | + } | ||
| 332 | + | ||
| 333 | + var handle = reuse(this); | ||
| 319 | 334 | ||
| 320 | - this._handle = new Timer(); | ||
| 335 | + this._handle = handle || new Timer(); | ||
| 321 | 336 | this._handle.owner = this; | |
| 322 | 337 | this._handle[kOnTimeout] = unrefdHandle; | |
| 323 | 338 | this._handle.start(delay, 0); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + | ||
| 6 | + var once = 0; | ||
| 7 | + | ||
| 8 | + process.on('beforeExit', () => { | ||
| 9 | + if (once > 1) | ||
| 10 | + throw new RangeError('beforeExit should only have been called once!'); | ||
| 11 | + | ||
| 12 | + setTimeout(() => {}, 1).unref(); | ||
| 13 | + once++; | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + process.on('exit', (code) => { | ||
| 17 | + if (code !== 0) return; | ||
| 18 | + | ||
| 19 | + assert.strictEqual(once, 1); | ||
| 20 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments