Skip to content

Commit f9e1ff7

Browse files
committed
feat(json-crdt): 🎸 add ability to print file to human-readable string
1 parent c24cea3 commit f9e1ff7

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/json-crdt/file/File.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import {Encoder as StructuralEncoderCompact} from '../codec/structural/compact/E
66
import {Encoder as StructuralEncoderVerbose} from '../codec/structural/verbose/Encoder';
77
import {encode as encodeCompact} from '../../json-crdt-patch/codec/compact/encode';
88
import {encode as encodeVerbose} from '../../json-crdt-patch/codec/verbose/encode';
9+
import {printTree} from "../../util/print/printTree";
910
import type * as types from "./types";
11+
import type {Printable} from "../../util/print/types";
1012

11-
export class File {
12-
public static fromModel(model: Model): File {
13+
export class File implements Printable {
14+
public static fromModel(model: Model<any>): File {
1315
return new File(model, PatchLog.fromModel(model));
1416
}
1517

@@ -89,4 +91,14 @@ export class File {
8991
history,
9092
];
9193
}
94+
95+
// ---------------------------------------------------------------- Printable
96+
97+
public toString(tab?: string) {
98+
return `file` + printTree(tab, [
99+
(tab) => this.model.toString(tab),
100+
() => '',
101+
(tab) => this.history.toString(tab),
102+
]);
103+
}
92104
}

src/json-crdt/file/PatchLog.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import {ITimestampStruct, Patch, ServerClockVector, compare} from "../../json-crdt-patch";
1+
import {ITimestampStruct, Patch, compare} from "../../json-crdt-patch";
2+
import {printTree} from "../../util/print/printTree";
23
import {AvlMap} from "../../util/trees/avl/AvlMap";
34
import {Model} from "../model";
5+
import type {Printable} from "../../util/print/types";
46

5-
export class PatchLog {
6-
public static fromModel (model: Model): PatchLog {
7+
export class PatchLog implements Printable {
8+
public static fromModel(model: Model<any>): PatchLog {
79
const start = new Model(model.clock.clone());
810
const log = new PatchLog(start);
911
if (model.api.builder.patch.ops.length) {
@@ -22,4 +24,16 @@ export class PatchLog {
2224
if (!id) return;
2325
this.patches.set(id, patch);
2426
}
27+
28+
// ---------------------------------------------------------------- Printable
29+
30+
public toString(tab?: string) {
31+
const log: Patch[] = [];
32+
this.patches.forEach(({v}) => log.push(v));
33+
return `log` + printTree(tab, [
34+
(tab) => this.start.toString(tab),
35+
() => '',
36+
(tab) => 'history' + printTree(tab, log.map((patch, i) => (tab) => `${i}: ${patch.toString(tab)}`)),
37+
]);
38+
}
2539
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {s} from '../../../json-crdt-patch';
2+
import {Model} from '../../model';
3+
import {File} from '../File';
4+
5+
test('can create File from new model', () => {
6+
const model = Model.withServerClock()
7+
.setSchema(s.obj({
8+
foo: s.str('bar'),
9+
}));
10+
const file = File.fromModel(model);
11+
expect(file.history.start.view()).toBe(undefined);
12+
expect(file.model.view()).toEqual({
13+
foo: 'bar',
14+
});
15+
expect(file.history.start.clock.sid).toBe(file.model.clock.sid);
16+
});
17+
18+
test.todo('patches are flushed and stored in memory');
19+
test.todo('can replay history');

src/json-crdt/model/Model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const UNDEFINED = new ConNode(ORIGIN, undefined);
2121
* In instance of Model class represents the underlying data structure,
2222
* i.e. model, of the JSON CRDT document.
2323
*/
24-
export class Model<N extends JsonNode = JsonNode> implements Printable {
24+
export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
2525
/**
2626
* Create a CRDT model which uses logical clock. Logical clock assigns a
2727
* logical timestamp to every node and operation. Logical timestamp consists

0 commit comments

Comments
 (0)