Skip to content

Commit

Permalink
patch(vestjs-runtime): emit event on async isolate done
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 9, 2023
1 parent 7ca4a56 commit b201b1f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ exports[`IsolateTest TestObject constructor 1`] = `
"$type": "UnitTest",
"children": [],
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "unicycle",
"message": "I am Root.",
"severity": "error",
Expand All @@ -34,15 +26,7 @@ exports[`IsolateTest testObject.warn Should mark the test as warning 1`] = `
"$type": "UnitTest",
"children": [],
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "unicycle",
"message": "I am Root.",
"severity": "warning",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a key
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "field_name",
"message": "failure message",
"severity": "error",
Expand Down Expand Up @@ -84,15 +76,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "field_name",
"severity": "error",
"status": "PASSING",
Expand Down Expand Up @@ -163,15 +147,7 @@ exports[`Test Vest's \`test\` function test params creates a test without a mess
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "field_name",
"severity": "error",
"status": "PASSING",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "t1",
"severity": "error",
"status": "FAILED",
Expand All @@ -35,15 +27,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "t2",
"severity": "error",
"status": "FAILED",
Expand All @@ -64,15 +48,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "t1",
"groupName": "g1",
"severity": "error",
Expand All @@ -90,15 +66,7 @@ exports[`staticSuite dump should output a dump of the suite 1`] = `
"allowReorder": undefined,
"children": null,
"data": {
"abortController": AbortController {
Symbol(signal): AbortSignal {
Symbol(kEvents): Map {},
Symbol(events.maxEventTargetListeners): 10,
Symbol(events.maxEventTargetListenersWarned): false,
Symbol(kAborted): false,
Symbol(kReason): undefined,
},
},
"abortController": AbortController {},
"fieldName": "t3",
"groupName": "g1",
"severity": "error",
Expand Down
4 changes: 4 additions & 0 deletions packages/vestjs-runtime/src/Isolate/Isolate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CB, Maybe, Nullable, isNotNullish, isPromise } from 'vest-utils';

import { useEmit } from 'Bus';
import { IsolateKeys } from 'IsolateKeys';
import { IsolateMutator } from 'IsolateMutator';
import { IsolateStatus } from 'IsolateStatus';
import { Reconciler } from 'Reconciler';
import { RuntimeEvents } from 'RuntimeEvents';
import * as VestRuntime from 'VestRuntime';

export type IsolateKey = Nullable<string>;
Expand Down Expand Up @@ -74,6 +76,7 @@ function useRunAsNew<Callback extends CB = CB>(
callback: CB
): ReturnType<Callback> {
const runtimeRoot = VestRuntime.useRuntimeRoot();
const emit = useEmit();

// We're creating a new child isolate context where the local history node
// is the current history node, thus advancing the history cursor.
Expand All @@ -95,6 +98,7 @@ function useRunAsNew<Callback extends CB = CB>(
}

IsolateMutator.setDone(current);
emit(RuntimeEvents.ASYNC_ISOLATE_DONE, current);
});
} else {
IsolateMutator.setDone(current);
Expand Down
29 changes: 29 additions & 0 deletions packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CB } from 'vest-utils';
import wait from 'wait';

import { useBus } from 'Bus';
import { Isolate, TIsolate } from 'Isolate';
import { RuntimeEvents } from 'RuntimeEvents';
import { VestRuntime } from 'vestjs-runtime';

describe('AsyncIsolate', () => {
Expand Down Expand Up @@ -71,6 +73,33 @@ describe('AsyncIsolate', () => {
done();
});
});

it('Should emit an event when an async isolate is done running', () => {
const cb = jest.fn();
return new Promise<void>(async done => {
let root = {} as TIsolate;
withRunTime(() => {
// Create root isolate from which all others will be created
root = Isolate.create('URoot', () => {
const bus = useBus();
bus.on(RuntimeEvents.ASYNC_ISOLATE_DONE, cb);

expect(cb).not.toHaveBeenCalled();
Isolate.create('UChild_1', async () => {
await wait(10);
expect(cb).not.toHaveBeenCalled();
});
expect(cb).not.toHaveBeenCalled();
});
});
expect(cb).not.toHaveBeenCalled();
expect(root?.children?.[0].status).not.toBe('DONE');
await wait(10);
expect(cb).toHaveBeenCalled();
expect(root?.children?.[0].status).toBe('DONE');
done();
});
});
});

function withRunTime<T>(fn: CB<T>) {
Expand Down
3 changes: 3 additions & 0 deletions packages/vestjs-runtime/src/RuntimeEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const RuntimeEvents = {
ASYNC_ISOLATE_DONE: 'ASYNC_ISOLATE_DONE',
};
1 change: 1 addition & 0 deletions packages/vestjs-runtime/tsconfig.json

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

2 comments on commit b201b1f

@vercel
Copy link

@vercel vercel bot commented on b201b1f Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app
vest-next-git-latest-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b201b1f Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest-ealush.vercel.app
vestjs.dev
vest.vercel.app
www.vestjs.dev
vest-git-latest-ealush.vercel.app

Please sign in to comment.