Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"prosemirror-transform": "^1.3.3",
"prosemirror-view": "1.18.7",
"shuffle-seed": "^1.1.6",
"sializer": "^0.2.0",
"ts-mocha": "^8.0.0",
"uuid": "^3.4.0"
},
Expand Down
49 changes: 39 additions & 10 deletions src/micromerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
getTextWithFormatting,
} from "./peritext"

const CHILDREN = Symbol("children")
const ROOT = Symbol("_root")
const HEAD = Symbol("_head")
import { sia, desia } from "sializer";

const CHILDREN = "children"
const ROOT = "_root"
const HEAD = "_head"

/** A patch represents a change to make to a JSON document.
* These are a way for Micromerge to notify a listener of incremental changes
Expand Down Expand Up @@ -272,25 +274,52 @@ export default class Micromerge {
/** Map from actorId to last sequence number seen from that actor. */
public clock: Record<string, number> = {}
/** Objects, keyed by the ID of the operation that created the object. */
private objects: Record<ObjectId, JsonComposite> & Record<typeof ROOT, Record<string, Json>> = {
[ROOT]: {},
}
private objects: Record<ObjectId, JsonComposite> & Record<typeof ROOT, Record<string, Json>>;
/** Map from object ID to CRDT metadata for each object field. */
private metadata: Record<ObjectId, Metadata> = {
[ROOT]: { [CHILDREN]: {} },
}
private metadata: Record<ObjectId, Metadata>;

constructor(actorId: string = uuid.v4()) {
this.actorId = actorId
this.objects = {
[ROOT]: {},
}
this.metadata = {
[ROOT]: { [CHILDREN]: {} },
}
}

/**
* Returns the document root object.
*/
get root(): Record<string, Json> {
return this.objects[ROOT]
}

public save(): string {
const payload = {
actorId: this.actorId,
seq: this.seq,
maxOp: this.maxOp,
clock: this.clock,
objects: this.objects,
metadata: this.metadata
}

return sia(payload)
}

public static load(json: string): Micromerge {
const payload = desia(json)
const micromerge = new Micromerge()
micromerge.actorId = payload.actorId
micromerge.maxOp = payload.maxOp
micromerge.seq = payload.seq
micromerge.clock = payload.clock
micromerge.objects = payload.objects
micromerge.metadata = payload.metadata
return micromerge

}

/**
* Return the document root object, cast to a given shape.
* The result will still make all fields optional, so the consumer
Expand Down
17 changes: 16 additions & 1 deletion test/micromerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { RootDoc } from "../src/bridge"
import { inspect } from "util"
import { generateDocs } from "./generateDocs"
import { accumulatePatches } from "./accumulatePatches"
import Micromerge from '../src/micromerge';

const defaultText = "The Peritext editor"
const textChars = defaultText.split("")
Expand Down Expand Up @@ -47,7 +48,9 @@ const testConcurrentWrites = (args: TraceSpec): void => {
const { initialText = "The Peritext editor", preOps, inputOps1 = [], inputOps2 = [], expectedResult } = args

const { docs, patches } = generateDocs(initialText)
const [doc1, doc2] = docs
let [doc1, doc2] = docs
doc1 = Micromerge.load(doc1.save())
doc2 = Micromerge.load(doc2.save())
let [patchesForDoc1, patchesForDoc2] = patches

if (preOps) {
Expand Down Expand Up @@ -1414,5 +1417,17 @@ describe.only("Micromerge", () => {

assert.deepStrictEqual(currentIndex, 0)
})


it("de/re-serializes ", () => {
const { docs } = generateDocs()
const [doc1, doc2] = docs

let binary = doc1.save()
let copy = Micromerge.load(binary)

assert.deepStrictEqual(copy.getRoot(), doc1.getRoot())

})
})
})