Skip to content

Commit

Permalink
minor(vestjs-runtime): Add Replacer to IsolateSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 10, 2024
1 parent 557835d commit 1ea14a4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
12 changes: 10 additions & 2 deletions packages/vestjs-runtime/src/exports/IsolateSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ export class IsolateSerializer {
return expanded;
}

static serialize(isolate: Nullable<TIsolate>): string {
static serialize(
isolate: Nullable<TIsolate>,
replacer: (value: any, key: string) => any,
): string {
if (isNullish(isolate)) {
return '';
}

const minified = minifyObject(isolate, ExcludedFromDump);
const minified = minifyObject(isolate, (value: any, key: string) => {
if (ExcludedFromDump.has(key as any)) {
return undefined;
}
return replacer(value, key);
});

return JSON.stringify(minified);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Isolate, TIsolate } from 'Isolate';
import { IsolateSerializer } from 'IsolateSerializer';
import { Maybe } from 'vest-utils';
import { CB } from 'vest-utils';
import { describe, it, expect, test } from 'vitest';

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

describe('IsolateSerializer', () => {
describe('serialize', () => {
Expand All @@ -16,7 +16,7 @@ describe('IsolateSerializer', () => {

describe('deserialize', () => {
it('Should fully inflate the tree', () => {
const { root, serialized } = createRoot();
const { serialized } = createRoot();

const inflated = IsolateSerializer.deserialize(serialized);

Expand Down Expand Up @@ -59,18 +59,40 @@ describe('IsolateSerializer', () => {

describe('Custom Data Serialization', () => {
it('Should serialize data with custom keys', () => {
const { serialized } = createRoot({
keys: {
data: {
some_data: 'sd',
},
},
});
const { serialized } = createRoot();

const parsed = JSON.parse(serialized);
expect(serialized).toMatchSnapshot();
});

it('Should take a replacer param', () => {
const { root } = createRoot();

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

const serialized = IsolateSerializer.serialize(
root,
(value: any, key: string) => {
if (key === 'status' && value === 'pending') {
return 'incomplete';
}

return value;
},
);

const inflated = IsolateSerializer.deserialize(serialized);

expect(inflated.status).toBe('incomplete');
// @ts-ignore
expect(inflated.children[0].status).toBe('done');
// @ts-ignore
expect(inflated.children[1].status).toBe('failed');
});

describe('value serialization', () => {
it('Should correctly expand values', () => {
const { root } = createRoot();
Expand All @@ -81,24 +103,8 @@ describe('IsolateSerializer', () => {
// @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);
const serialized = IsolateSerializer.serialize(root, v => v);
const inflated = IsolateSerializer.deserialize(serialized);

expect(inflated.status).toBe('pending');
// @ts-ignore
Expand Down Expand Up @@ -137,21 +143,9 @@ describe('IsolateSerializer', () => {
});

it('Should inflate with correct keys', () => {
const { serialized } = createRoot({
keys: {
data: {
some_data: 'sd',
},
},
});
const { serialized } = createRoot();

const inflated = IsolateSerializer.deserialize(serialized, {
keys: {
data: {
some_data: 'sd',
},
},
});
const inflated = IsolateSerializer.deserialize(serialized);

expect(inflated.data.some_data).toBe(true);
expect(inflated).not.toHaveProperty('sd');
Expand Down Expand Up @@ -188,12 +182,15 @@ describe('IsolateSerializer', () => {
});

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

function createRoot(miniMap: Maybe<Record<string, any>>) {
function createRoot(replacer: (_value: any, _key: string) => any = v => v) {
let serialized: string, root: TIsolate;

withRunTime(() => {
Expand All @@ -209,7 +206,7 @@ function createRoot(miniMap: Maybe<Record<string, any>>) {
},
);

serialized = IsolateSerializer.serialize(root, miniMap);
serialized = IsolateSerializer.serialize(root, replacer);
});

// @ts-ignore
Expand Down

0 comments on commit 1ea14a4

Please sign in to comment.