mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
PR-URL: https://github.com/nodejs/node/pull/23794 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matheus Marchini <mat@mmarchini.me>
40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
|
const { AsyncResource } = require('async_hooks');
|
|
const { getDefaultTriggerAsyncId } = require('internal/async_hooks');
|
|
const { enqueueMicrotask } = internalBinding('util');
|
|
|
|
const setupQueueMicrotask = (triggerFatalException) => {
|
|
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 (error) {
|
|
// TODO(devsnek) remove this if
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
|
|
// is resolved such that V8 triggers the fatal exception
|
|
// handler for microtasks
|
|
triggerFatalException(error);
|
|
} finally {
|
|
asyncResource.emitDestroy();
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
return queueMicrotask;
|
|
};
|
|
|
|
module.exports = { setupQueueMicrotask };
|