Skip to content

Commit 5881dc4

Browse files
committed
Return new heads from DocHandle.changeAt
1 parent eb59ad3 commit 5881dc4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

packages/automerge-repo/src/DocHandle.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,23 +326,31 @@ export class DocHandle<T> //
326326
})
327327
}
328328

329+
/** Make a change as if the document were at `heads`
330+
*
331+
* @returns A set of heads representing the concurrent change that was made.
332+
*/
329333
changeAt(
330334
heads: A.Heads,
331335
callback: A.ChangeFn<T>,
332336
options: A.ChangeOptions<T> = {}
333-
) {
337+
): string[] | undefined {
334338
if (!this.isReady()) {
335339
throw new Error(
336340
`DocHandle#${this.documentId} is not ready. Check \`handle.isReady()\` before accessing the document.`
337341
)
338342
}
343+
let resultHeads: string[] | undefined = undefined
339344
this.#machine.send(UPDATE, {
340345
payload: {
341346
callback: (doc: A.Doc<T>) => {
342-
return A.changeAt(doc, heads, options, callback).newDoc
347+
const result = A.changeAt(doc, heads, options, callback)
348+
resultHeads = result.newHeads
349+
return result.newDoc
343350
},
344351
},
345352
})
353+
return resultHeads
346354
}
347355

348356
unavailable() {

packages/automerge-repo/test/DocHandle.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,11 @@ describe("DocHandle", () => {
295295
})
296296

297297
let wasBar = false
298-
handle.changeAt(headsBefore, doc => {
298+
let newHeads = handle.changeAt(headsBefore, doc => {
299299
wasBar = doc.foo === "bar"
300300
doc.foo = "baz"
301301
})
302+
assert(newHeads && newHeads.length > 0, "should have new heads")
302303

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

0 commit comments

Comments
 (0)