diff options
author | Gus Caplan <me@gus.host> | 2018-09-19 10:33:52 -0500 |
---|---|---|
committer | Gus Caplan <me@gus.host> | 2018-09-23 15:57:19 -0500 |
commit | de0441f6f677b338b78752b8fbaac767fdf35075 (patch) | |
tree | 6257215ec115f9c47457bdd109e7ad9b71af7cb3 /lib | |
parent | 59a8324d2793c504fc81a3126478d8c401df314e (diff) | |
download | android-node-v8-de0441f6f677b338b78752b8fbaac767fdf35075.tar.gz android-node-v8-de0441f6f677b338b78752b8fbaac767fdf35075.tar.bz2 android-node-v8-de0441f6f677b338b78752b8fbaac767fdf35075.zip |
lib: implement queueMicrotask
PR-URL: https://github.com/nodejs/node/pull/22951
Refs: https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-queuemicrotask
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/bootstrap/node.js | 28 | ||||
-rw-r--r-- | lib/internal/queue_microtask.js | 32 |
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 8537131e8b..e1c56e9b7e 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -134,6 +134,7 @@ setupGlobalConsole(); setupGlobalURL(); setupGlobalEncoding(); + setupQueueMicrotask(); } if (process.binding('config').experimentalWorker) { @@ -527,6 +528,33 @@ }); } + function setupQueueMicrotask() { + const { queueMicrotask } = NativeModule.require('internal/queue_microtask'); + Object.defineProperty(global, 'queueMicrotask', { + get: () => { + process.emitWarning('queueMicrotask() is experimental.', + 'ExperimentalWarning'); + Object.defineProperty(global, 'queueMicrotask', { + value: queueMicrotask, + writable: true, + enumerable: false, + configurable: true, + }); + return queueMicrotask; + }, + set: (v) => { + Object.defineProperty(global, 'queueMicrotask', { + value: v, + writable: true, + enumerable: false, + configurable: true, + }); + }, + enumerable: false, + configurable: true, + }); + } + function setupDOMException() { // Registers the constructor with C++. NativeModule.require('internal/domexception'); diff --git a/lib/internal/queue_microtask.js b/lib/internal/queue_microtask.js new file mode 100644 index 0000000000..3ff7ae9ae4 --- /dev/null +++ b/lib/internal/queue_microtask.js @@ -0,0 +1,32 @@ +'use strict'; + +const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; +const { AsyncResource } = require('async_hooks'); +const { getDefaultTriggerAsyncId } = require('internal/async_hooks'); +const { internalBinding } = require('internal/bootstrap/loaders'); +const { enqueueMicrotask } = internalBinding('util'); + +// declared separately for name, arrow function to prevent construction +const queueMicrotask = (callback) => { + if (typeof callback !== 'function') { + throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback); + } + + const asyncResource = new AsyncResource('Microtask', { + triggerAsyncId: getDefaultTriggerAsyncId(), + requireManualDestroy: true, + }); + + enqueueMicrotask(() => { + asyncResource.runInAsyncScope(() => { + try { + callback(); + } catch (e) { + process.emit('error', e); + } + }); + asyncResource.emitDestroy(); + }); +}; + +module.exports = { queueMicrotask }; |