| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 823ee2b commit a4cbd57
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -155,7 +155,7 @@ function initAsyncResource(resource, type) { | |||
| 155 | 155 | ||
| 156 | 156 | // Timer constructor function. | |
| 157 | 157 | // The entire prototype is defined in lib/timers.js | |
| 158 | - function Timeout(callback, after, args, isRepeat) { | ||
| 158 | + function Timeout(callback, after, args, isRepeat, isRefed) { | ||
| 159 | 159 | after *= 1; // Coalesce to number or NaN | |
| 160 | 160 | if (!(after >= 1 && after <= TIMEOUT_MAX)) { | |
| 161 | 161 | if (after > TIMEOUT_MAX) { | |
@@ -179,7 +179,9 @@ function Timeout(callback, after, args, isRepeat) { | |||
| 179 | 179 | this._repeat = isRepeat ? after : null; | |
| 180 | 180 | this._destroyed = false; | |
| 181 | 181 | ||
| 182 | - this[kRefed] = null; | ||
| 182 | + if (isRefed) | ||
| 183 | + incRefCount(); | ||
| 184 | + this[kRefed] = isRefed; | ||
| 183 | 185 | ||
| 184 | 186 | initAsyncResource(this, 'Timeout'); | |
| 185 | 187 | } | |
@@ -295,27 +297,43 @@ function decRefCount() { | |||
| 295 | 297 | // Schedule or re-schedule a timer. | |
| 296 | 298 | // The item must have been enroll()'d first. | |
| 297 | 299 | function active(item) { | |
| 298 | - insert(item, true, getLibuvNow()); | ||
| 300 | + insertGuarded(item, true); | ||
| 299 | 301 | } | |
| 300 | 302 | ||
| 301 | 303 | // Internal APIs that need timeouts should use `unrefActive()` instead of | |
| 302 | 304 | // `active()` so that they do not unnecessarily keep the process open. | |
| 303 | 305 | function unrefActive(item) { | |
| 304 | - insert(item, false, getLibuvNow()); | ||
| 306 | + insertGuarded(item, false); | ||
| 305 | 307 | } | |
| 306 | 308 | ||
| 307 | 309 | // The underlying logic for scheduling or re-scheduling a timer. | |
| 308 | 310 | // | |
| 309 | 311 | // Appends a timer onto the end of an existing timers list, or creates a new | |
| 310 | 312 | // list if one does not already exist for the specified timeout duration. | |
| 311 | - function insert(item, refed, start) { | ||
| 312 | - let msecs = item._idleTimeout; | ||
| 313 | + function insertGuarded(item, refed, start) { | ||
| 314 | + const msecs = item._idleTimeout; | ||
| 313 | 315 | if (msecs < 0 || msecs === undefined) | |
| 314 | 316 | return; | |
| 315 | 317 | ||
| 316 | - // Truncate so that accuracy of sub-millisecond timers is not assumed. | ||
| 317 | - msecs = MathTrunc(msecs); | ||
| 318 | + insert(item, msecs, start); | ||
| 319 | + | ||
| 320 | + if (!item[async_id_symbol] || item._destroyed) { | ||
| 321 | + item._destroyed = false; | ||
| 322 | + initAsyncResource(item, 'Timeout'); | ||
| 323 | + } | ||
| 324 | + | ||
| 325 | + if (refed === !item[kRefed]) { | ||
| 326 | + if (refed) | ||
| 327 | + incRefCount(); | ||
| 328 | + else | ||
| 329 | + decRefCount(); | ||
| 330 | + } | ||
| 331 | + item[kRefed] = refed; | ||
| 332 | + } | ||
| 318 | 333 | ||
| 334 | + function insert(item, msecs, start = getLibuvNow()) { | ||
| 335 | + // Truncate so that accuracy of sub-milisecond timers is not assumed. | ||
| 336 | + msecs = MathTrunc(msecs); | ||
| 319 | 337 | item._idleStart = start; | |
| 320 | 338 | ||
| 321 | 339 | // Use an existing list if there is one, otherwise we need to make a new one. | |
@@ -332,19 +350,6 @@ function insert(item, refed, start) { | |||
| 332 | 350 | } | |
| 333 | 351 | } | |
| 334 | 352 | ||
| 335 | - if (!item[async_id_symbol] || item._destroyed) { | ||
| 336 | - item._destroyed = false; | ||
| 337 | - initAsyncResource(item, 'Timeout'); | ||
| 338 | - } | ||
| 339 | - | ||
| 340 | - if (refed === !item[kRefed]) { | ||
| 341 | - if (refed) | ||
| 342 | - incRefCount(); | ||
| 343 | - else | ||
| 344 | - decRefCount(); | ||
| 345 | - } | ||
| 346 | - item[kRefed] = refed; | ||
| 347 | - | ||
| 348 | 353 | L.append(list, item); | |
| 349 | 354 | } | |
| 350 | 355 | ||
@@ -354,8 +359,8 @@ function setUnrefTimeout(callback, after) { | |||
| 354 | 359 | throw new ERR_INVALID_CALLBACK(callback); | |
| 355 | 360 | } | |
| 356 | 361 | ||
| 357 | - const timer = new Timeout(callback, after, undefined, false); | ||
| 358 | - unrefActive(timer); | ||
| 362 | + const timer = new Timeout(callback, after, undefined, false, false); | ||
| 363 | + insert(timer, timer._idleTimeout); | ||
| 359 | 364 | ||
| 360 | 365 | return timer; | |
| 361 | 366 | } | |
@@ -540,16 +545,14 @@ function getTimerCallbacks(runNextTicks) { | |||
| 540 | 545 | } finally { | |
| 541 | 546 | if (timer._repeat && timer._idleTimeout !== -1) { | |
| 542 | 547 | timer._idleTimeout = timer._repeat; | |
| 543 | - if (start === undefined) | ||
| 544 | - start = getLibuvNow(); | ||
| 545 | - insert(timer, timer[kRefed], start); | ||
| 548 | + insert(timer, timer._idleTimeout, start); | ||
| 546 | 549 | } else if (!timer._idleNext && !timer._idlePrev) { | |
| 547 | 550 | if (timer[kRefed]) | |
| 548 | 551 | refCount--; | |
| 549 | 552 | timer[kRefed] = null; | |
| 550 | 553 | ||
| 551 | 554 | if (destroyHooksExist() && !timer._destroyed) { | |
| 552 | - emitDestroy(timer[async_id_symbol]); | ||
| 555 | + emitDestroy(asyncId); | ||
| 553 | 556 | } | |
| 554 | 557 | timer._destroyed = true; | |
| 555 | 558 | } | |
@@ -598,6 +601,7 @@ module.exports = { | |||
| 598 | 601 | }, | |
| 599 | 602 | active, | |
| 600 | 603 | unrefActive, | |
| 604 | + insert, | ||
| 601 | 605 | timerListMap, | |
| 602 | 606 | timerListQueue, | |
| 603 | 607 | decRefCount, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,7 +46,8 @@ const { | |||
| 46 | 46 | timerListQueue, | |
| 47 | 47 | immediateQueue, | |
| 48 | 48 | active, | |
| 49 | - unrefActive | ||
| 49 | + unrefActive, | ||
| 50 | + insert | ||
| 50 | 51 | } = require('internal/timers'); | |
| 51 | 52 | const { | |
| 52 | 53 | promisify: { custom: customPromisify }, | |
@@ -142,16 +143,17 @@ function setTimeout(callback, after, arg1, arg2, arg3) { | |||
| 142 | 143 | break; | |
| 143 | 144 | } | |
| 144 | 145 | ||
| 145 | - const timeout = new Timeout(callback, after, args, false); | ||
| 146 | - active(timeout); | ||
| 146 | + const timeout = new Timeout(callback, after, args, false, true); | ||
| 147 | + insert(timeout, timeout._idleTimeout); | ||
| 147 | 148 | ||
| 148 | 149 | return timeout; | |
| 149 | 150 | } | |
| 150 | 151 | ||
| 151 | 152 | setTimeout[customPromisify] = function(after, value) { | |
| 152 | 153 | const args = value !== undefined ? [value] : value; | |
| 153 | 154 | return new Promise((resolve) => { | |
| 154 | - active(new Timeout(resolve, after, args, false)); | ||
| 155 | + const timeout = new Timeout(resolve, after, args, false, true); | ||
| 156 | + insert(timeout, timeout._idleTimeout); | ||
| 155 | 157 | }); | |
| 156 | 158 | }; | |
| 157 | 159 | ||
@@ -188,8 +190,8 @@ function setInterval(callback, repeat, arg1, arg2, arg3) { | |||
| 188 | 190 | break; | |
| 189 | 191 | } | |
| 190 | 192 | ||
| 191 | - const timeout = new Timeout(callback, repeat, args, true); | ||
| 192 | - active(timeout); | ||
| 193 | + const timeout = new Timeout(callback, repeat, args, true, true); | ||
| 194 | + insert(timeout, timeout._idleTimeout); | ||
| 193 | 195 | ||
| 194 | 196 | return timeout; | |
| 195 | 197 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments