Skip to content

Commit 036daa6

Browse files
committed
patch(vestjs-runtime): set isolate status as pending
1 parent c9457e9 commit 036daa6

File tree

12 files changed

+101
-3
lines changed

12 files changed

+101
-3
lines changed

packages/vestjs-runtime/src/Isolate/Isolate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CB, Maybe, Nullable, isNotNullish, isPromise } from 'vest-utils';
22

33
import { IsolateKeys } from 'IsolateKeys';
44
import { IsolateMutator } from 'IsolateMutator';
5+
import { IsolateStatus } from 'IsolateStatus';
56
import { Reconciler } from 'Reconciler';
67
import * as VestRuntime from 'VestRuntime';
78

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

8789
if (isPromise(output)) {
90+
IsolateMutator.setPending(current);
91+
8892
output.then(iso => {
8993
if (Isolate.isIsolate(iso)) {
9094
IsolateMutator.addChild(current, iso);
9195
}
96+
97+
IsolateMutator.setDone(current);
9298
});
99+
} else {
100+
IsolateMutator.setDone(current);
93101
}
94102

95103
return output;
@@ -112,6 +120,7 @@ function baseIsolate(
112120
[IsolateKeys.Parent]: null,
113121
[IsolateKeys.Type]: type,
114122
[IsolateKeys.Data]: data as IsolateData,
123+
[IsolateKeys.Status]: IsolateStatus.INITIAL,
115124
children: null,
116125
key,
117126
output: null,

packages/vestjs-runtime/src/Isolate/IsolateKeys.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum IsolateKeys {
55
Parent = 'parent',
66
Data = 'data',
77
AllowReorder = 'allowReorder',
8+
Status = 'status',
89
}
910

1011
enum MinifiedKeys {
@@ -14,6 +15,7 @@ enum MinifiedKeys {
1415
Parent = 'P',
1516
Data = 'D',
1617
AllowReorder = 'aR',
18+
Status = 'S',
1719
}
1820

1921
export const KeyToMinified = {
@@ -23,6 +25,7 @@ export const KeyToMinified = {
2325
[IsolateKeys.Data]: MinifiedKeys.Data,
2426
[IsolateKeys.Key]: MinifiedKeys.Key,
2527
[IsolateKeys.AllowReorder]: MinifiedKeys.AllowReorder,
28+
[IsolateKeys.Status]: MinifiedKeys.Status,
2629
};
2730

2831
// This const is an object that looks like this:

packages/vestjs-runtime/src/Isolate/IsolateMutator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Nullable, invariant, isNullish } from 'vest-utils';
22

33
import { TIsolate } from 'Isolate';
4+
import { IsolateStatus } from 'IsolateStatus';
45

56
export class IsolateMutator {
67
static setParent(isolate: TIsolate, parent: Nullable<TIsolate>): TIsolate {
@@ -50,4 +51,12 @@ export class IsolateMutator {
5051
static setData(isolate: TIsolate, data: any): void {
5152
isolate.data = data;
5253
}
54+
55+
static setPending(isolate: TIsolate): void {
56+
isolate.status = IsolateStatus.PENDING;
57+
}
58+
59+
static setDone(isolate: TIsolate): void {
60+
isolate.status = IsolateStatus.DONE;
61+
}
5362
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum IsolateStatus {
2+
PENDING = 'PENDING',
3+
DONE = 'DONE',
4+
INITIAL = 'INITIAL',
5+
}

packages/vestjs-runtime/src/Isolate/__tests__/Isolate.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ describe('Isolate', () => {
9393
expect(child.parent).toBe(parent);
9494
});
9595
});
96+
97+
test('Isolate status is INITIAL before running', () => {
98+
const control = jest.fn();
99+
withRunTime(() => {
100+
return Isolate.create(IsolateType.Isolate, () => {
101+
expect(useAvailableRoot().status).toBe('INITIAL');
102+
control();
103+
});
104+
});
105+
106+
expect(control).toHaveBeenCalled();
107+
});
108+
109+
test('Isolate status is DONE after running', () => {
110+
const isolate = withRunTime(() => {
111+
return Isolate.create(IsolateType.Isolate, () => {});
112+
});
113+
114+
expect(isolate.status).toBe('DONE');
115+
});
96116
});
97117

98118
function withRunTime<T>(fn: CB<T>) {

packages/vestjs-runtime/src/Isolate/__tests__/__snapshots__/asyncIsolate.test.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
1818
"keys": null,
1919
"output": undefined,
2020
"parent": [Circular],
21+
"status": "DONE",
2122
},
2223
{
2324
"$type": "UGrandChild_2",
@@ -28,6 +29,7 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
2829
"keys": null,
2930
"output": undefined,
3031
"parent": [Circular],
32+
"status": "DONE",
3133
},
3234
{
3335
"$type": "UGrandChild_3",
@@ -38,19 +40,36 @@ exports[`AsyncIsolate It should resolve async isolate into the parent 2`] = `
3840
"keys": null,
3941
"output": undefined,
4042
"parent": [Circular],
43+
"status": "DONE",
4144
},
4245
],
4346
"data": {},
4447
"key": null,
4548
"keys": null,
4649
"output": undefined,
4750
"parent": [Circular],
51+
"status": "DONE",
4852
},
4953
],
5054
"data": {},
5155
"key": null,
5256
"keys": null,
5357
"output": Promise {},
5458
"parent": null,
59+
"status": "DONE",
60+
}
61+
`;
62+
63+
exports[`AsyncIsolate It should set the isolate state to pending 1`] = `
64+
{
65+
"$type": "URoot",
66+
"allowReorder": undefined,
67+
"children": null,
68+
"data": {},
69+
"key": null,
70+
"keys": null,
71+
"output": Promise {},
72+
"parent": null,
73+
"status": "PENDING",
5574
}
5675
`;

packages/vestjs-runtime/src/Isolate/__tests__/asyncIsolate.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ describe('AsyncIsolate', () => {
88
test('It should resolve async isolate into the parent', () => {
99
return new Promise<void>(async done => {
1010
let root = {} as TIsolate;
11-
const control = jest.fn();
1211
withRunTime(() => {
1312
// Create root isolate from which all others will be created
1413
root = Isolate.create('URoot', genChildren);
1514
});
16-
expect(control).not.toHaveBeenCalled();
1715
expect(root).toMatchInlineSnapshot(`
1816
{
1917
"$type": "URoot",
@@ -24,6 +22,7 @@ describe('AsyncIsolate', () => {
2422
"keys": null,
2523
"output": Promise {},
2624
"parent": null,
25+
"status": "PENDING",
2726
}
2827
`);
2928
await wait(10);
@@ -46,6 +45,32 @@ describe('AsyncIsolate', () => {
4645
done();
4746
});
4847
});
48+
49+
test('It should set the isolate state to pending', () => {
50+
return new Promise<void>(done => {
51+
let root = {} as TIsolate;
52+
withRunTime(() => {
53+
// Create root isolate from which all others will be created
54+
root = Isolate.create('URoot', genChildren);
55+
});
56+
expect(root).toMatchSnapshot();
57+
expect(root?.status).toBe('PENDING');
58+
done();
59+
});
60+
});
61+
62+
it('should set the isolate state to done when complete', () => {
63+
return new Promise<void>(async done => {
64+
let root = {} as TIsolate;
65+
withRunTime(() => {
66+
// Create root isolate from which all others will be created
67+
root = Isolate.create('URoot', genChildren);
68+
});
69+
await wait(10);
70+
expect(root?.status).toBe('DONE');
71+
done();
72+
});
73+
});
4974
});
5075

5176
function withRunTime<T>(fn: CB<T>) {

packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@ describe('IsolateSerializer', () => {
2929
"some_data": true,
3030
},
3131
"parent": [Circular],
32+
"status": "DONE",
3233
},
3334
{
3435
"$type": "UChild_2",
3536
"data": {},
3637
"parent": [Circular],
38+
"status": "DONE",
3739
},
3840
{
3941
"$type": "UChild_3",
4042
"data": {},
4143
"parent": [Circular],
44+
"status": "DONE",
4245
},
4346
],
4447
"data": {
4548
"some_data": true,
4649
},
50+
"status": "DONE",
4751
}
4852
`);
4953
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
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}}"`;
3+
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"}"`;

packages/vestjs-runtime/src/exports/test-utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TIsolate } from 'Isolate';
22
import { IsolateKeys } from 'IsolateKeys';
3+
import { IsolateStatus } from 'IsolateStatus';
34

45
export function genTestIsolate(data: Record<string, any> = {}): TIsolate {
56
return {
@@ -9,6 +10,7 @@ export function genTestIsolate(data: Record<string, any> = {}): TIsolate {
910
keys: {},
1011
output: null,
1112
parent: null,
13+
status: IsolateStatus.INITIAL,
1214
[IsolateKeys.Type]: 'UnitTest',
1315
};
1416
}

0 commit comments

Comments
 (0)