Skip to content

WIP: Support for v.16 and queueMicrotask #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ log.txt
node_modules
log-app.txt
coverage
junit.xml
junit.xml
.vscode/launch.json
11 changes: 8 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"main": "index.js",
"license": "MIT",
"engines": {
"node": "11.x"
"node": ">=16.x"
},
"scripts": {
"start": "node --experimental-worker index.js",
"start": "node index.js",
"test": "jest"
},
"dependencies": {
Expand Down
21 changes: 13 additions & 8 deletions src/main/eventsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ const eventsReducer = (state, evt) => {
({ asyncID }) => asyncID === payload.asyncID
);

if (microtaskInfo) {
if (
(microtaskInfo) ||
(state.prevEvt.type==='EnterFunction' && state.prevEvt.payload.name==='queueMicrotask')) {
state.events.push({
type: 'EnqueueMicrotask',
payload: { name: microtaskInfo.name },
payload: { name: microtaskInfo?.name || 'microtask' }, // TODO: Get callback name for q'd µtasks
});
}
}
Expand Down Expand Up @@ -198,12 +200,15 @@ const reduceEvents = (events) => {
})
);

// console.log({
// resolvedPromiseIds,
// promisesWithInvokedCallbacksInfo,
// parentsIdsOfPromisesWithInvokedCallbacks,
// parentsIdsOfMicrotasks,
// });
console.log({
resolvedPromiseIds,
promisesWithInvokedCallbacksInfo,
parentsIdsOfPromisesWithInvokedCallbacks,
promiseChildIdToParentId,
microtasksWithInvokedCallbacksInfo,
microtaskChildIdToParentId,
parentsIdsOfMicrotasks,
});

return events.reduce(eventsReducer, {
events: [],
Expand Down
59 changes: 30 additions & 29 deletions src/worker/asyncHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function debug(...args) {
}
const asyncIdToResource = {};
const init = (asyncId, type, triggerAsyncId, resource) => {
debug(`asyncId:${asyncId}, type:${type}, taid:${triggerAsyncId}, resource:`,resource);
asyncIdToResource[asyncId] = resource;
if (type === 'PROMISE') {
postEvent(Events.InitPromise(asyncId, triggerAsyncId));
Expand All @@ -19,11 +20,11 @@ const init = (asyncId, type, triggerAsyncId, resource) => {
const callbackName = resource._onImmediate.name || 'anonymous';
postEvent(Events.InitImmediate(asyncId, callbackName));
}
// if (type === 'Microtask') {
// const callbackName = resource.callback.name || 'anonymous';
// debug(resource);
// postEvent(Events.InitMicrotask(asyncId, triggerAsyncId, callbackName));
// }
if (type === 'Microtask') {
const callbackName = resource.callback?.name || 'anonymous';
debug('Microtask: ', resource);
postEvent(Events.InitMicrotask(asyncId, triggerAsyncId, callbackName));
}
if (
type === 'TickObject' &&
resource.callback.name !== 'maybeReadMore_' &&
Expand All @@ -36,7 +37,7 @@ const init = (asyncId, type, triggerAsyncId, resource) => {
resource.callback.name !== 'finish' &&
resource.callback.name !== 'resume_'
) {
const callbackName = resource.callback.name || 'anonymous';
const callbackName = resource?.callback?.name || 'microtask';
debug(callbackName);
postEvent(Events.InitMicrotask(asyncId, triggerAsyncId, callbackName));
}
Expand All @@ -45,7 +46,7 @@ const init = (asyncId, type, triggerAsyncId, resource) => {
const before = (asyncId) => {
const resource = asyncIdToResource[asyncId] || {};
const resourceName = resource.constructor.name;
if (resourceName === 'PromiseWrap') {
if (resourceName === 'Promise') {
postEvent(Events.BeforePromise(asyncId));
}
if (resourceName === 'Timeout') {
Expand All @@ -56,36 +57,36 @@ const before = (asyncId) => {
const callbackName = resource._onImmediate.name || 'anonymous';
postEvent(Events.BeforeImmediate(asyncId, callbackName));
}
if (
resourceName === 'Object' &&
resource.callback &&
resource.callback.name !== 'maybeReadMore_' &&
resource.callback.name !== 'afterWriteTick' &&
resource.callback.name !== 'onSocketNT' &&
resource.callback.name !== 'initRead' &&
resource.callback.name !== 'emitReadable_' &&
resource.callback.name !== 'emitCloseNT' &&
resource.callback.name !== 'endReadableNT' &&
resource.callback.name !== 'finish' &&
resource.callback.name !== 'resume_'
) {
const callbackName = resource.callback.name || 'anonymous';
postEvent(Events.BeforeMicrotask(asyncId, callbackName));
}
// if (resourceName === 'AsyncResource') {
// postEvent(Events.BeforeMicrotask(asyncId));
// if (
// resourceName === 'Object' &&
// resource.callback &&
// resource.callback.name !== 'maybeReadMore_' &&
// resource.callback.name !== 'afterWriteTick' &&
// resource.callback.name !== 'onSocketNT' &&
// resource.callback.name !== 'initRead' &&
// resource.callback.name !== 'emitReadable_' &&
// resource.callback.name !== 'emitCloseNT' &&
// resource.callback.name !== 'endReadableNT' &&
// resource.callback.name !== 'finish' &&
// resource.callback.name !== 'resume_'
// ) {
// const callbackName = resource.callback.name || 'anonymous';
// postEvent(Events.BeforeMicrotask(asyncId, callbackName));
// }
if (resourceName === 'AsyncResource') {
postEvent(Events.BeforeMicrotask(asyncId));
}
};

const after = (asyncId) => {
const resource = asyncIdToResource[asyncId] || {};
const resourceName = resource.constructor.name;
if (resourceName === 'PromiseWrap') {
if (resourceName === 'Promise') {
postEvent(Events.AfterPromise(asyncId));
}
// if (resourceName === 'AsyncResource') {
// postEvent(Events.AfterMicrotask(asyncId));
// }
if (resourceName === 'AsyncResource') {
postEvent(Events.AfterMicrotask(asyncId));
}
};

const destroy = (asyncId) => {
Expand Down