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
ealush committed Nov 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7ca4a56 commit b201b1f
Showing 7 changed files with 46 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -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",
@@ -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",
Original file line number Diff line number Diff line change
@@ -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",
@@ -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",
@@ -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",
Original file line number Diff line number Diff line change
@@ -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",
@@ -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",
@@ -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",
@@ -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",
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>;
@@ -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.
@@ -95,6 +98,7 @@ function useRunAsNew<Callback extends CB = CB>(
}

IsolateMutator.setDone(current);
emit(RuntimeEvents.ASYNC_ISOLATE_DONE, current);
});
} else {
IsolateMutator.setDone(current);
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', () => {
@@ -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>) {
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.