Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
|
ed90699c70 |
2 changed files with 34 additions and 0 deletions
|
@ -231,6 +231,20 @@ export function load<T>(data: Uint8Array, _opts?: ActorId | InitOptions<T>) : Do
|
|||
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 {
|
||||
return _state(doc).handle.save()
|
||||
}
|
||||
|
|
20
automerge-js/test/extra_api_tests.ts
Normal file
20
automerge-js/test/extra_api_tests.ts
Normal 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)
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue