Compare commits

...

1 commit

Author SHA1 Message Date
Orion Henry
ed90699c70 add loadIncremental to js 2022-10-10 19:23:22 -04:00
2 changed files with 34 additions and 0 deletions

View file

@ -231,6 +231,20 @@ export function load<T>(data: Uint8Array, _opts?: ActorId | InitOptions<T>) : Do
return doc return doc
} }
export function loadIncremental<T>(doc: Doc<T>, data: Uint8Array, opts?: ApplyOptions<T>) : Doc<T> {
if (!opts) { opts = {} }
const state = _state(doc)
if (state.heads) {
throw new RangeError("Attempting to change an out of date document - set at: " + _trace(doc));
}
if (_readonly(doc) === false) {
throw new RangeError("Calls to Automerge.change cannot be nested")
}
const heads = state.handle.getHeads()
state.handle.loadIncremental(data)
return progressDocument(doc, heads, opts.patchCallback || state.patchCallback)
}
export function save<T>(doc: Doc<T>) : Uint8Array { export function save<T>(doc: Doc<T>) : Uint8Array {
return _state(doc).handle.save() return _state(doc).handle.save()
} }

View file

@ -0,0 +1,20 @@
import * as assert from 'assert'
import * as Automerge from '../src'
describe('Automerge', () => {
describe('basics', () => {
it('should allow you to load incrementally', () => {
let doc1 = Automerge.from({ foo: "bar" })
let doc2 = Automerge.init();
doc2 = Automerge.loadIncremental(doc2, Automerge.save(doc1))
doc1 = Automerge.change(doc1, (d) => d.foo2 = "bar2")
doc2 = Automerge.loadIncremental(doc2, Automerge.getBackend(doc1).saveIncremental() )
doc1 = Automerge.change(doc1, (d) => d.foo = "bar2")
doc2 = Automerge.loadIncremental(doc2, Automerge.getBackend(doc1).saveIncremental() )
doc1 = Automerge.change(doc1, (d) => d.x = "y")
doc2 = Automerge.loadIncremental(doc2, Automerge.getBackend(doc1).saveIncremental() )
assert.deepEqual(doc1,doc2)
})
})
})