Skip to content

Commit

Permalink
feat(import-export): added exportObject and importObject functions to…
Browse files Browse the repository at this point in the history
… api (#57)
  • Loading branch information
kirangadhave authored Aug 16, 2023
2 parents bcd4239 + 2f151de commit 5e0cac3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/provenance/trrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ export function initializeTrrack<State = any, Event extends string = string>({
export() {
return JSON.stringify(graph.backend);
},
exportObject() {
return JSON.parse(
JSON.stringify(graph.backend)
) as typeof graph.backend;
},
import(graphString: string) {
const g: ProvenanceGraph<State, Event> = JSON.parse(graphString);

Expand All @@ -386,6 +391,12 @@ export function initializeTrrack<State = any, Event extends string = string>({
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,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/provenance/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SideEffects,
UnsubscribeCurrentChangeListener,
} from '../graph';
import { ProvenanceGraph } from '../graph/graph-slice';
import { Registry } from '../registry';
import { TrrackEvents } from './trrack-events';

Expand Down Expand Up @@ -74,5 +75,7 @@ export interface Trrack<State, Event extends string> {
tree(): any;
on(event: TrrackEvents, listener: (args?: any) => void): void;
export(): string;
exportObject(): ProvenanceGraph<State, Event>;
import(graphString: string): void;
importObject(graph: ProvenanceGraph<State, Event>): void;
}
74 changes: 74 additions & 0 deletions packages/core/tests/import_export.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});

1 comment on commit 5e0cac3

@vercel
Copy link

@vercel vercel bot commented on 5e0cac3 Aug 16, 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:

trrackjs – ./

trrackjs.vercel.app
trrackjs-trrack.vercel.app
trrackjs-git-main-trrack.vercel.app

Please sign in to comment.