diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-23 08:45:07 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-01-06 10:28:47 +0800 |
commit | d18b0a01320e392e48f0c475effd8d4db2bb71a8 (patch) | |
tree | 91801c7d10b4adc09fe3dbc93cd1c962a7938533 /lib/internal/process | |
parent | bf566718b29a6fae8cef8d0fecd7e77948726d5a (diff) | |
download | android-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.tar.gz android-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.tar.bz2 android-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.zip |
process: make tick callback and promise rejection callback more robust
- Rename `internalTickCallback` to `processTicksAndRejections`, make
sure it does not get called if it's not set in C++.
- Rename `emitPromiseRejectionWarnings` to `processPromiseRejections`
since it also emit events that are not warnings.
- Sets `SetPromiseRejectCallback` in the `Environment` constructor
to make sure it only gets called once per-isolate, and make
sure it does not get called if it's not set in C++.
- Wrap promise rejection callback initialization into
`listenForRejections()`.
- Add comments.
PR-URL: https://github.com/nodejs/node/pull/25200
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/process')
-rw-r--r-- | lib/internal/process/next_tick.js | 20 | ||||
-rw-r--r-- | lib/internal/process/promises.js | 15 |
2 files changed, 20 insertions, 15 deletions
diff --git a/lib/internal/process/next_tick.js b/lib/internal/process/next_tick.js index 0bfe168975..24104ec34a 100644 --- a/lib/internal/process/next_tick.js +++ b/lib/internal/process/next_tick.js @@ -6,15 +6,14 @@ const { tickInfo, // Used to run V8's micro task queue. runMicrotasks, - setTickCallback, - initializePromiseRejectCallback + setTickCallback } = internalBinding('task_queue'); const { setHasRejectionToWarn, hasRejectionToWarn, - promiseRejectHandler, - emitPromiseRejectionWarnings + listenForRejections, + processPromiseRejections } = require('internal/process/promises'); const { @@ -49,10 +48,10 @@ function runNextTicks() { if (!hasTickScheduled() && !hasRejectionToWarn()) return; - internalTickCallback(); + processTicksAndRejections(); } -function internalTickCallback() { +function processTicksAndRejections() { let tock; do { while (tock = queue.shift()) { @@ -80,7 +79,7 @@ function internalTickCallback() { } setHasTickScheduled(false); runMicrotasks(); - } while (!queue.isEmpty() || emitPromiseRejectionWarnings()); + } while (!queue.isEmpty() || processPromiseRejections()); setHasRejectionToWarn(false); } @@ -134,11 +133,10 @@ function nextTick(callback) { // TODO(joyeecheung): make this a factory class so that node.js can // control the side effects caused by the initializers. exports.setup = function() { - // Initializes the per-isolate promise rejection callback which - // will call the handler being passed into this function. - initializePromiseRejectCallback(promiseRejectHandler); + // Sets the per-isolate promise rejection callback + listenForRejections(); // Sets the callback to be run in every tick. - setTickCallback(internalTickCallback); + setTickCallback(processTicksAndRejections); return { nextTick, runNextTicks diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 047a9d43aa..f3e118e2ce 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -8,7 +8,8 @@ const { kPromiseHandlerAddedAfterReject, kPromiseResolveAfterResolved, kPromiseRejectAfterResolved - } + }, + setPromiseRejectCallback } = internalBinding('task_queue'); // *Must* match Environment::TickInfo::Fields in src/env.h. @@ -117,7 +118,9 @@ function emitDeprecationWarning() { } } -function emitPromiseRejectionWarnings() { +// If this method returns true, at least one more tick need to be +// scheduled to process any potential pending rejections +function processPromiseRejections() { while (asyncHandledRejections.length > 0) { const { promise, warning } = asyncHandledRejections.shift(); if (!process.emit('rejectionHandled', promise)) { @@ -142,9 +145,13 @@ function emitPromiseRejectionWarnings() { return maybeScheduledTicks || pendingUnhandledRejections.length !== 0; } +function listenForRejections() { + setPromiseRejectCallback(promiseRejectHandler); +} + module.exports = { hasRejectionToWarn, setHasRejectionToWarn, - promiseRejectHandler, - emitPromiseRejectionWarnings + listenForRejections, + processPromiseRejections }; |