Skip to content

Commit

Permalink
minor(vestjs-utils): Support async isolate behavior (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush authored Nov 6, 2023
1 parent 950c178 commit 289acc4
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/vestjs-runtime/src/Isolate/Isolate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CB, Maybe, Nullable } from 'vest-utils';
import { CB, Maybe, Nullable, isNotNullish, isPromise } from 'vest-utils';

import { IsolateKeys } from 'IsolateKeys';
import { IsolateMutator } from 'IsolateMutator';
Expand Down Expand Up @@ -52,6 +52,10 @@ export class Isolate {

return nextIsolateChild as TIsolate<Payload>;
}

static isIsolate(node: any): node is TIsolate {
return isNotNullish(node) && node[IsolateKeys.Type];
}
}

/**
Expand All @@ -77,7 +81,19 @@ function useRunAsNew<Callback extends CB = CB>(
runtimeNode: current,
...(!runtimeRoot && { runtimeRoot: current }),
},
() => callback(current)
() => {
const output = callback(current);

if (isPromise(output)) {
output.then(iso => {
if (Isolate.isIsolate(iso)) {
IsolateMutator.addChild(current, iso);
}
});
}

return output;
}
);

current.output = output;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
{
"$type": "URoot",
"allowReorder": undefined,
"children": [
{
"$type": "UChild_1",
"allowReorder": undefined,
"children": [
{
"$type": "UGrandChild_1",
"allowReorder": undefined,
"children": null,
"data": {},
"key": null,
"keys": null,
"output": undefined,
"parent": [Circular],
},
{
"$type": "UGrandChild_2",
"allowReorder": undefined,
"children": null,
"data": {},
"key": null,
"keys": null,
"output": undefined,
"parent": [Circular],
},
{
"$type": "UGrandChild_3",
"allowReorder": undefined,
"children": null,
"data": {},
"key": null,
"keys": null,
"output": undefined,
"parent": [Circular],
},
],
"data": {},
"key": null,
"keys": null,
"output": undefined,
"parent": [Circular],
},
],
"data": {},
"key": null,
"keys": null,
"output": Promise {},
"parent": null,
}
`;
68 changes: 68 additions & 0 deletions packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { CB } from 'vest-utils';
import wait from 'wait';

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

describe('AsyncIsolate', () => {
test('It should resolve async isolate into the parent', () => {
return new Promise<void>(async done => {
let root = {} as TIsolate;
const control = jest.fn();
withRunTime(() => {
// Create root isolate from which all others will be created
root = Isolate.create('URoot', genChildren);
});
expect(control).not.toHaveBeenCalled();
expect(root).toMatchInlineSnapshot(`
{
"$type": "URoot",
"allowReorder": undefined,
"children": null,
"data": {},
"key": null,
"keys": null,
"output": Promise {},
"parent": null,
}
`);
await wait(10);
expect(root?.children?.[0]?.$type).toBe('UChild_1');
expect(root?.children?.[0].parent).toBe(root);
expect(root?.children?.[0]?.children?.[0]?.$type).toBe('UGrandChild_1');
expect(root?.children?.[0]?.children?.[0].parent).toBe(
root?.children?.[0]
);
expect(root?.children?.[0]?.children?.[1]?.$type).toBe('UGrandChild_2');
expect(root?.children?.[0]?.children?.[1].parent).toBe(
root?.children?.[0]
);
expect(root?.children?.[0]?.children?.[2]?.$type).toBe('UGrandChild_3');
expect(root?.children?.[0]?.children?.[2].parent).toBe(
root?.children?.[0]
);
expect(root).toMatchSnapshot();

done();
});
});
});

function withRunTime<T>(fn: CB<T>) {
return VestRuntime.Run(VestRuntime.createRef({}), () => {
return fn();
});
}

async function genChildren() {
await wait(10);
// Create first child isolate
return withRunTime(() =>
Isolate.create('UChild_1', () => {
// Create three grandchildren
Isolate.create('UGrandChild_1', () => {});
Isolate.create('UGrandChild_2', () => {});
Isolate.create('UGrandChild_3', () => {});
})
);
}

1 comment on commit 289acc4

@vercel
Copy link

@vercel vercel bot commented on 289acc4 Nov 6, 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

Please sign in to comment.