From aeab9e9cc3080ae1798eb5d5bf07e05532f0993c Mon Sep 17 00:00:00 2001 From: Kiran Gadhave Date: Thu, 10 Aug 2023 14:36:57 -0600 Subject: [PATCH 1/2] feat(import-export): added exportObject and importObject functions to api --- packages/core/src/provenance/trrack.ts | 11 ++++ packages/core/src/provenance/types.ts | 3 + packages/core/tests/import_export.test.ts | 74 +++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 packages/core/tests/import_export.test.ts diff --git a/packages/core/src/provenance/trrack.ts b/packages/core/src/provenance/trrack.ts index cc8fff6..f228cd0 100644 --- a/packages/core/src/provenance/trrack.ts +++ b/packages/core/src/provenance/trrack.ts @@ -378,6 +378,11 @@ export function initializeTrrack({ export() { return JSON.stringify(graph.backend); }, + exportObject() { + return JSON.parse( + JSON.stringify(graph.backend) + ) as typeof graph.backend; + }, import(graphString: string) { const g: ProvenanceGraph = JSON.parse(graphString); @@ -386,6 +391,12 @@ export function initializeTrrack({ graph.update(graph.load(g)); this.to(current); }, + importObject(g: typeof graph.backend) { + const current = g.current; + g.current = g.root; + graph.update(graph.load(g)); + this.to(current); + }, metadata, artifact, annotations, diff --git a/packages/core/src/provenance/types.ts b/packages/core/src/provenance/types.ts index bb1274e..d67c109 100644 --- a/packages/core/src/provenance/types.ts +++ b/packages/core/src/provenance/types.ts @@ -10,6 +10,7 @@ import { SideEffects, UnsubscribeCurrentChangeListener, } from '../graph'; +import { ProvenanceGraph } from '../graph/graph-slice'; import { Registry } from '../registry'; import { TrrackEvents } from './trrack-events'; @@ -74,5 +75,7 @@ export interface Trrack { tree(): any; on(event: TrrackEvents, listener: (args?: any) => void): void; export(): string; + exportObject(): ProvenanceGraph; import(graphString: string): void; + importObject(graph: ProvenanceGraph): void; } diff --git a/packages/core/tests/import_export.test.ts b/packages/core/tests/import_export.test.ts new file mode 100644 index 0000000..4b87370 --- /dev/null +++ b/packages/core/tests/import_export.test.ts @@ -0,0 +1,74 @@ +import { initializeTrrack } from '../src/provenance/trrack'; +import { Registry } from '../src/registry'; + +function setup() { + const registry = Registry.create(); + + const add = registry.register('add', (state, amt) => { + state.counter += amt; + }); + + const sub = registry.register('sub', (state, amt) => { + state.counter -= amt; + }); + + const trrack = initializeTrrack({ + registry, + initialState: { + counter: 0, + }, + }); + + return { trrack, add, sub }; +} + +describe('Export', () => { + it('export functions should return a stringified trrack graph and graph object', () => { + const { trrack, add } = setup(); + + expect(trrack.getState().counter).toEqual(0); + + trrack.apply('Add', add(2)); + expect(trrack.getState().counter).toEqual(2); + + const exportStr = trrack.export(); + expect(exportStr).toBeTypeOf('string'); + + const parsed = JSON.parse(exportStr); + expect(parsed).toHaveProperty('nodes'); + expect(parsed).toHaveProperty('current'); + expect(parsed).toHaveProperty('root'); + }); + + it('import functions should load a stringified trrack graph and graph object', () => { + const { trrack, add } = setup(); + + expect(trrack.getState().counter).toEqual(0); + + trrack.apply('Add', add(2)); + expect(trrack.getState().counter).toEqual(2); + + const exportStr = trrack.export(); + const exportObject = trrack.exportObject(); + + const { trrack: trrackStr } = setup(); + const { trrack: trrackObj } = setup(); + + trrackStr.import(exportStr); + trrackObj.importObject(exportObject); + + expect(trrackStr.getState().counter).toEqual(trrack.getState().counter); + expect(trrackStr.root.id).toEqual(trrack.root.id); + expect(trrackStr.current.id).toEqual(trrack.current.id); + expect(Object.keys(trrackStr.graph.backend).length).toEqual( + Object.keys(trrack.graph.backend).length + ); + + expect(trrackObj.getState().counter).toEqual(trrack.getState().counter); + expect(trrackObj.root.id).toEqual(trrack.root.id); + expect(trrackObj.current.id).toEqual(trrack.current.id); + expect(Object.keys(trrackObj.graph.backend).length).toEqual( + Object.keys(trrack.graph.backend).length + ); + }); +}); From 2f151ded490530af8b8d885ccf4c289c8885f610 Mon Sep 17 00:00:00 2001 From: kirangadhave Date: Thu, 10 Aug 2023 20:49:12 +0000 Subject: [PATCH 2/2] release(version): Release @trrack/core 1.3.0-beta.1 [skip ci] # [1.3.0-beta.1](https://github.com/Trrack/trrackjs/compare/@trrack/core@1.2.0...@trrack/core@1.3.0-beta.1) (2023-08-10) ### Features * **import-export:** added exportObject and importObject functions to api ([aeab9e9](https://github.com/Trrack/trrackjs/commit/aeab9e9cc3080ae1798eb5d5bf07e05532f0993c)) --- packages/core/CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index e6e72da..e48ee84 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,3 +1,10 @@ +# [1.3.0-beta.1](https://github.com/Trrack/trrackjs/compare/@trrack/core@1.2.0...@trrack/core@1.3.0-beta.1) (2023-08-10) + + +### Features + +* **import-export:** added exportObject and importObject functions to api ([aeab9e9](https://github.com/Trrack/trrackjs/commit/aeab9e9cc3080ae1798eb5d5bf07e05532f0993c)) + # [1.2.0](https://github.com/Trrack/trrackjs/compare/@trrack/core@1.1.0...@trrack/core@1.2.0) (2023-05-15)