Skip to content

Commit

Permalink
Return new heads from DocHandle.changeAt
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Sep 12, 2023
1 parent eb59ad3 commit 5881dc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,23 +326,31 @@ export class DocHandle<T> //
})
}

/** Make a change as if the document were at `heads`
*
* @returns A set of heads representing the concurrent change that was made.
*/
changeAt(
heads: A.Heads,
callback: A.ChangeFn<T>,
options: A.ChangeOptions<T> = {}
) {
): string[] | undefined {
if (!this.isReady()) {
throw new Error(
`DocHandle#${this.documentId} is not ready. Check \`handle.isReady()\` before accessing the document.`
)
}
let resultHeads: string[] | undefined = undefined
this.#machine.send(UPDATE, {
payload: {
callback: (doc: A.Doc<T>) => {
return A.changeAt(doc, heads, options, callback).newDoc
const result = A.changeAt(doc, heads, options, callback)
resultHeads = result.newHeads
return result.newDoc
},
},
})
return resultHeads
}

unavailable() {
Expand Down
3 changes: 2 additions & 1 deletion packages/automerge-repo/test/DocHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,11 @@ describe("DocHandle", () => {
})

let wasBar = false
handle.changeAt(headsBefore, doc => {
let newHeads = handle.changeAt(headsBefore, doc => {
wasBar = doc.foo === "bar"
doc.foo = "baz"
})
assert(newHeads && newHeads.length > 0, "should have new heads")

assert(wasBar, "foo should have been bar as we changed at the old heads")
})
Expand Down

0 comments on commit 5881dc4

Please sign in to comment.