Skip to content

Commit

Permalink
patch(vestjs-runtime): Support inflation of serialized values
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 30, 2023
1 parent e7858d2 commit 3765ad9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/vestjs-runtime/src/Isolate/IsolateKeys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign } from 'vest-utils';
import { assign } from 'lodash';

export enum IsolateKeys {
Type = '$type',
Expand Down
35 changes: 28 additions & 7 deletions packages/vestjs-runtime/src/exports/IsolateSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ErrorStrings } from 'ErrorStrings';
import {
Maybe,
Nullable,
assign,
hasOwnProperty,
invariant,
isEmpty,
Expand All @@ -18,12 +19,11 @@ import {
KeyToMinified,
MinifiedKeys,
MinifiedToKey,
invertKeyMap,
} from 'IsolateKeys';
import { IsolateMutator } from 'IsolateMutator';

export class IsolateSerializer {
// eslint-disable-next-line max-statements, complexity
// eslint-disable-next-line max-statements, complexity, max-lines-per-function
static deserialize(
node: Record<string, any> | TIsolate | string,
miniMaps: Maybe<MiniMaps>
Expand All @@ -35,7 +35,7 @@ export class IsolateSerializer {
// to avoid circular references during serialization.
// we need to rebuild the tree and add back the parent property to the children
// and the keys property to the parents.
const inverseMinimap = invertKeyMap(miniMaps?.keys?.data);
const inverseMinimap = deeplyInvertKeyMap(miniMaps);

// Validate the root object
const root = isStringValue(node)
Expand Down Expand Up @@ -65,12 +65,20 @@ export class IsolateSerializer {
const keyToUse = MinifiedToKey[key];

// If the key is data, then we may need to transform the keys
// eslint-disable-next-line max-depth
if (keyToUse === IsolateKeys.Data) {
// Transform the keys
current[keyToUse] = transformKeys(value, inverseMinimap);
current[keyToUse] = transformKeys(
value,
inverseMinimap?.keys?.data
);
} else {
// Otherwise, just set the key
current[keyToUse] = value;
current[keyToUse] = transformValueByKey(
keyToUse,
value,
inverseMinimap
);
}

// Remove the old key
Expand Down Expand Up @@ -163,7 +171,7 @@ function transformIsolate(
}

const keyToUse = minifyKey(key);
next[keyToUse] = minifyValueByKey(key, value, miniMaps);
next[keyToUse] = transformValueByKey(key, value, miniMaps);
}

return next;
Expand All @@ -177,7 +185,7 @@ function minifyKey(key: string): string {
return KeyToMinified[key as keyof typeof KeyToMinified] ?? key;
}

function minifyValueByKey(
function transformValueByKey(
key: string,
value: any,
miniMaps: Maybe<MiniMaps>
Expand Down Expand Up @@ -223,3 +231,16 @@ type MiniMaps = Partial<{
[IsolateKeys.Type]: MiniMap;
}>;
}>;

export function deeplyInvertKeyMap(miniMaps: Maybe<MiniMaps> = {}): MiniMaps {
return Object.entries(miniMaps).reduce((acc, [key, value]) => {
if (typeof value === 'object') {
return assign(acc, {
[key]: deeplyInvertKeyMap(value as MiniMaps),
});
}
return assign(acc, {
[value]: key,
});
}, {} as MiniMaps);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,69 @@ describe('IsolateSerializer', () => {
`"{"C":[{"D":{"some_data":true},"$":"UC1","S":"d"},{"$":"UC2","S":"f"},{"$":"UC3"}],"D":{"some_data":true},"$":"UR","S":"p"}"`
);
});

it('Should correctly expand values', () => {
const { root } = createRoot();

root.status = 'pending';
// @ts-ignore
root.children[0].status = 'done';
// @ts-ignore
root.children[1].status = 'failed';

const minimap = {
values: {
status: {
pending: 'p',
done: 'd',
failed: 'f',
},
$type: {
URoot: 'UR',
UChild_1: 'UC1',
UChild_2: 'UC2',
UChild_3: 'UC3',
},
},
};

const serialized = IsolateSerializer.serialize(root, minimap);
const inflated = IsolateSerializer.deserialize(serialized, minimap);

expect(inflated.status).toBe('pending');
// @ts-ignore
expect(inflated.children[0].status).toBe('done');
// @ts-ignore
expect(inflated.children[1].status).toBe('failed');
expect(inflated).toMatchInlineSnapshot(`
{
"$type": "URoot",
"children": [
{
"$type": "UChild_1",
"data": {
"some_data": true,
},
"parent": [Circular],
"status": "done",
},
{
"$type": "UChild_2",
"parent": [Circular],
"status": "failed",
},
{
"$type": "UChild_3",
"parent": [Circular],
},
],
"data": {
"some_data": true,
},
"status": "pending",
}
`);
});
});

it('Should inflate with correct keys', () => {
Expand Down

2 comments on commit 3765ad9

@vercel
Copy link

@vercel vercel bot commented on 3765ad9 Nov 30, 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

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

@vercel
Copy link

@vercel vercel bot commented on 3765ad9 Nov 30, 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.