From 1ea14a44a2ac639e628cca8fc0eede676dd9b5d6 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Wed, 9 Oct 2024 23:15:45 -0400 Subject: [PATCH] minor(vestjs-runtime): Add Replacer to IsolateSerializer --- .../src/exports/IsolateSerializer.ts | 12 ++- .../__tests__/IsolateSerializer.test.ts | 97 +++++++++---------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/packages/vestjs-runtime/src/exports/IsolateSerializer.ts b/packages/vestjs-runtime/src/exports/IsolateSerializer.ts index 9b57e4a60..daa211dec 100644 --- a/packages/vestjs-runtime/src/exports/IsolateSerializer.ts +++ b/packages/vestjs-runtime/src/exports/IsolateSerializer.ts @@ -65,12 +65,20 @@ export class IsolateSerializer { return expanded; } - static serialize(isolate: Nullable): string { + static serialize( + isolate: Nullable, + 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); } diff --git a/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts b/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts index 553d3e890..c9ad8150f 100644 --- a/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts +++ b/packages/vestjs-runtime/src/exports/__tests__/IsolateSerializer.test.ts @@ -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', () => { @@ -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); @@ -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(); @@ -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 @@ -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'); @@ -188,12 +182,15 @@ describe('IsolateSerializer', () => { }); function withRunTime(fn: CB) { - return VestRuntime.Run(VestRuntime.createRef({}), () => { - return fn(); - }); + return VestRuntime.Run( + VestRuntime.createRef({} as IRecociler, v => v), + () => { + return fn(); + }, + ); } -function createRoot(miniMap: Maybe>) { +function createRoot(replacer: (_value: any, _key: string) => any = v => v) { let serialized: string, root: TIsolate; withRunTime(() => { @@ -209,7 +206,7 @@ function createRoot(miniMap: Maybe>) { }, ); - serialized = IsolateSerializer.serialize(root, miniMap); + serialized = IsolateSerializer.serialize(root, replacer); }); // @ts-ignore