Skip to content

Commit

Permalink
patch(vestjs-runtime): set isolate status as pending
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 7, 2023
1 parent c9457e9 commit 036daa6
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/vestjs-runtime/src/Isolate/Isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CB, Maybe, Nullable, isNotNullish, isPromise } from 'vest-utils';

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

Expand All @@ -13,6 +14,7 @@ export type TIsolate<P extends IsolatePayload = IsolatePayload> = {
[IsolateKeys.Type]: string;
[IsolateKeys.Keys]: Nullable<Record<string, TIsolate>>;
[IsolateKeys.Data]: DataOnly<P>;
[IsolateKeys.Status]: IsolateStatus;
children: Nullable<TIsolate[]>;
key: IsolateKey;
output: any;
Expand Down Expand Up @@ -85,11 +87,17 @@ function useRunAsNew<Callback extends CB = CB>(
const output = callback(current);

if (isPromise(output)) {
IsolateMutator.setPending(current);

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

IsolateMutator.setDone(current);
});
} else {
IsolateMutator.setDone(current);
}

return output;
Expand All @@ -112,6 +120,7 @@ function baseIsolate(
[IsolateKeys.Parent]: null,
[IsolateKeys.Type]: type,
[IsolateKeys.Data]: data as IsolateData,
[IsolateKeys.Status]: IsolateStatus.INITIAL,
children: null,
key,
output: null,
Expand Down
3 changes: 3 additions & 0 deletions packages/vestjs-runtime/src/Isolate/IsolateKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum IsolateKeys {
Parent = 'parent',
Data = 'data',
AllowReorder = 'allowReorder',
Status = 'status',
}

enum MinifiedKeys {
Expand All @@ -14,6 +15,7 @@ enum MinifiedKeys {
Parent = 'P',
Data = 'D',
AllowReorder = 'aR',
Status = 'S',
}

export const KeyToMinified = {
Expand All @@ -23,6 +25,7 @@ export const KeyToMinified = {
[IsolateKeys.Data]: MinifiedKeys.Data,
[IsolateKeys.Key]: MinifiedKeys.Key,
[IsolateKeys.AllowReorder]: MinifiedKeys.AllowReorder,
[IsolateKeys.Status]: MinifiedKeys.Status,
};

// This const is an object that looks like this:
Expand Down
9 changes: 9 additions & 0 deletions packages/vestjs-runtime/src/Isolate/IsolateMutator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Nullable, invariant, isNullish } from 'vest-utils';

import { TIsolate } from 'Isolate';
import { IsolateStatus } from 'IsolateStatus';

export class IsolateMutator {
static setParent(isolate: TIsolate, parent: Nullable<TIsolate>): TIsolate {
Expand Down Expand Up @@ -50,4 +51,12 @@ export class IsolateMutator {
static setData(isolate: TIsolate, data: any): void {
isolate.data = data;
}

static setPending(isolate: TIsolate): void {
isolate.status = IsolateStatus.PENDING;
}

static setDone(isolate: TIsolate): void {
isolate.status = IsolateStatus.DONE;
}
}
5 changes: 5 additions & 0 deletions packages/vestjs-runtime/src/Isolate/IsolateStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum IsolateStatus {
PENDING = 'PENDING',
DONE = 'DONE',
INITIAL = 'INITIAL',
}
20 changes: 20 additions & 0 deletions packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ describe('Isolate', () => {
expect(child.parent).toBe(parent);
});
});

test('Isolate status is INITIAL before running', () => {
const control = jest.fn();
withRunTime(() => {
return Isolate.create(IsolateType.Isolate, () => {
expect(useAvailableRoot().status).toBe('INITIAL');
control();
});
});

expect(control).toHaveBeenCalled();
});

test('Isolate status is DONE after running', () => {
const isolate = withRunTime(() => {
return Isolate.create(IsolateType.Isolate, () => {});
});

expect(isolate.status).toBe('DONE');
});
});

function withRunTime<T>(fn: CB<T>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
"keys": null,
"output": undefined,
"parent": [Circular],
"status": "DONE",
},
{
"$type": "UGrandChild_2",
Expand All @@ -28,6 +29,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
"keys": null,
"output": undefined,
"parent": [Circular],
"status": "DONE",
},
{
"$type": "UGrandChild_3",
Expand All @@ -38,19 +40,36 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
"keys": null,
"output": undefined,
"parent": [Circular],
"status": "DONE",
},
],
"data": {},
"key": null,
"keys": null,
"output": undefined,
"parent": [Circular],
"status": "DONE",
},
],
"data": {},
"key": null,
"keys": null,
"output": Promise {},
"parent": null,
"status": "DONE",
}
`;

exports[`AsyncIsolate It should set the isolate state to pending 1`] = `
{
"$type": "URoot",
"allowReorder": undefined,
"children": null,
"data": {},
"key": null,
"keys": null,
"output": Promise {},
"parent": null,
"status": "PENDING",
}
`;
29 changes: 27 additions & 2 deletions packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ 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",
Expand All @@ -24,6 +22,7 @@ describe('AsyncIsolate', () => {
"keys": null,
"output": Promise {},
"parent": null,
"status": "PENDING",
}
`);
await wait(10);
Expand All @@ -46,6 +45,32 @@ describe('AsyncIsolate', () => {
done();
});
});

test('It should set the isolate state to pending', () => {
return new Promise<void>(done => {
let root = {} as TIsolate;
withRunTime(() => {
// Create root isolate from which all others will be created
root = Isolate.create('URoot', genChildren);
});
expect(root).toMatchSnapshot();
expect(root?.status).toBe('PENDING');
done();
});
});

it('should set the isolate state to done when complete', () => {
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', genChildren);
});
await wait(10);
expect(root?.status).toBe('DONE');
done();
});
});
});

function withRunTime<T>(fn: CB<T>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,25 @@ describe('IsolateSerializer', () => {
"some_data": true,
},
"parent": [Circular],
"status": "DONE",
},
{
"$type": "UChild_2",
"data": {},
"parent": [Circular],
"status": "DONE",
},
{
"$type": "UChild_3",
"data": {},
"parent": [Circular],
"status": "DONE",
},
],
"data": {
"some_data": true,
},
"status": "DONE",
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`IsolateSerializer serialize Should produce serialized dump 1`] = `"{"children":[{"$":"UChild_1","D":{"some_data":true}},{"$":"UChild_2","D":{}},{"$":"UChild_3","D":{}}],"$":"URoot","D":{"some_data":true}}"`;
exports[`IsolateSerializer serialize Should produce serialized dump 1`] = `"{"children":[{"$":"UChild_1","D":{"some_data":true},"S":"DONE"},{"$":"UChild_2","D":{},"S":"DONE"},{"$":"UChild_3","D":{},"S":"DONE"}],"$":"URoot","D":{"some_data":true},"S":"DONE"}"`;
2 changes: 2 additions & 0 deletions packages/vestjs-runtime/src/exports/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TIsolate } from 'Isolate';
import { IsolateKeys } from 'IsolateKeys';
import { IsolateStatus } from 'IsolateStatus';

export function genTestIsolate(data: Record<string, any> = {}): TIsolate {
return {
Expand All @@ -9,6 +10,7 @@ export function genTestIsolate(data: Record<string, any> = {}): TIsolate {
keys: {},
output: null,
parent: null,
status: IsolateStatus.INITIAL,
[IsolateKeys.Type]: 'UnitTest',
};
}
1 change: 1 addition & 0 deletions packages/vestjs-runtime/src/vestjs-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { IsolateKey, TIsolate, Isolate } from 'Isolate';
export { IsolateStatus } from 'IsolateStatus';
export { Reconciler, IRecociler } from 'Reconciler';
export * as Walker from 'IsolateWalker';
export { RuntimeApi as VestRuntime } from 'VestRuntime';
Expand Down
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.

1 comment on commit 036daa6

@vercel
Copy link

@vercel vercel bot commented on 036daa6 Nov 7, 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-next.vercel.app
vest-website.vercel.app
vest-next-git-latest-ealush.vercel.app

Please sign in to comment.