Skip to content

Commit

Permalink
make sure we always have at least an empty document to simplify the t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
pvh committed Jul 17, 2024
1 parent 4b3b3bf commit 9e245c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
#machine

/** The last known state of our document. */
#prevDocState: T | undefined
#prevDocState: T = A.init<T>()

/** How long to wait before giving up on a document. (Note that a document will be marked
* unavailable much sooner if all known peers respond that they don't have it.) */
Expand Down Expand Up @@ -70,7 +70,7 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
}),
onDelete: assign(() => {
this.emit("delete", { handle: this })
return { doc: undefined }
return { doc: A.init() }
}),
onUnavailable: () => {
this.emit("unavailable", { handle: this })
Expand Down Expand Up @@ -171,7 +171,7 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
* Called after state transitions. If the document has changed, emits a change event. If we just
* received the document for the first time, signal that our request has been completed.
*/
#checkForChanges(before: A.Doc<T> | undefined = A.init(), after: A.Doc<T> | undefined = A.init()) {
#checkForChanges(before: A.Doc<T>, after: A.Doc<T>) {
const beforeHeads = A.getHeads(before)
const afterHeads = A.getHeads(after)
const docChanged = !headsAreSame(afterHeads, beforeHeads)
Expand Down
4 changes: 2 additions & 2 deletions packages/automerge-repo/test/DocSynchronizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ describe("DocSynchronizer", () => {
const setup = () => {
const docId = parseAutomergeUrl(generateAutomergeUrl()).documentId
handle = new DocHandle<TestDoc>(docId)
// hmm, bit of a smell here
handle.update(() => Automerge.init())
handle.doneLoading()

docSynchronizer = new DocSynchronizer({
handle: handle as DocHandle<unknown>,
})

return { handle, docSynchronizer }
}

Expand Down

0 comments on commit 9e245c5

Please sign in to comment.