-
Notifications
You must be signed in to change notification settings - Fork 28.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #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>
- Loading branch information
Showing
9 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const initHooks = require('./init-hooks'); | ||
const { checkInvocations } = require('./hook-checks'); | ||
|
||
const hooks = initHooks(); | ||
hooks.enable(); | ||
|
||
const rootAsyncId = async_hooks.executionAsyncId(); | ||
|
||
queueMicrotask(common.mustCall(function() { | ||
assert.strictEqual(async_hooks.triggerAsyncId(), rootAsyncId); | ||
})); | ||
|
||
process.on('exit', function() { | ||
hooks.sanityCheck(); | ||
|
||
const as = hooks.activitiesOfTypes('Microtask'); | ||
checkInvocations(as[0], { | ||
init: 1, before: 1, after: 1, destroy: 1 | ||
}, 'when process exits'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
assert.strictEqual(typeof queueMicrotask, 'function'); | ||
|
||
[ | ||
undefined, | ||
null, | ||
0, | ||
'x = 5', | ||
].forEach((t) => { | ||
assert.throws(common.mustCall(() => { | ||
queueMicrotask(t); | ||
}), { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
}); | ||
|
||
{ | ||
let called = false; | ||
queueMicrotask(common.mustCall(() => { | ||
called = true; | ||
})); | ||
assert.strictEqual(called, false); | ||
} | ||
|
||
queueMicrotask(common.mustCall(function() { | ||
assert.strictEqual(arguments.length, 0); | ||
}), 'x', 'y'); | ||
|
||
{ | ||
const q = []; | ||
Promise.resolve().then(() => q.push('a')); | ||
queueMicrotask(common.mustCall(() => q.push('b'))); | ||
Promise.reject().catch(() => q.push('c')); | ||
|
||
queueMicrotask(common.mustCall(() => { | ||
assert.deepStrictEqual(q, ['a', 'b', 'c']); | ||
})); | ||
} | ||
|
||
const eq = []; | ||
process.on('error', (e) => { | ||
eq.push(e); | ||
}); | ||
|
||
process.on('exit', () => { | ||
assert.strictEqual(eq.length, 2); | ||
assert.strictEqual(eq[0].message, 'E1'); | ||
assert.strictEqual( | ||
eq[1].message, 'Class constructor cannot be invoked without \'new\''); | ||
}); | ||
|
||
queueMicrotask(common.mustCall(() => { | ||
throw new Error('E1'); | ||
})); | ||
|
||
queueMicrotask(class {}); |
@devsnek
gives
whereas
gives
So this doesn’t seem to be accurate?