Skip to content

Commit

Permalink
Add support to unload doc from DocHandle, with reload allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
George Su committed Aug 21, 2024
1 parent 42706f7 commit 8f0f839
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
unloaded: {
entry: "onUnload",
on: {
BEGIN: "loading",
RELOAD: "loading",
},
},
deleted: { entry: "onDelete", type: "final" },
Expand Down Expand Up @@ -448,6 +448,11 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
this.#machine.send({ type: UNLOAD })
}

/** Called by the repo to reuse an unloaded handle. */
reload() {
this.#machine.send({ type: RELOAD })
}

/** Called by the repo when the document is deleted. */
delete() {
this.#machine.send({ type: DELETE })
Expand Down Expand Up @@ -601,6 +606,7 @@ type DocHandleEvent<T> =
payload: { callback: (doc: A.Doc<T>) => A.Doc<T> }
}
| { type: typeof UNLOAD }
| { type: typeof RELOAD }
| { type: typeof DELETE }
| { type: typeof TIMEOUT }
| { type: typeof DOC_UNAVAILABLE }
Expand All @@ -610,6 +616,7 @@ const REQUEST = "REQUEST"
const DOC_READY = "DOC_READY"
const UPDATE = "UPDATE"
const UNLOAD = "UNLOAD"
const RELOAD = "RELOAD"
const DELETE = "DELETE"
const TIMEOUT = "TIMEOUT"
const DOC_UNAVAILABLE = "DOC_UNAVAILABLE"
4 changes: 2 additions & 2 deletions packages/automerge-repo/test/DocHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ describe("DocHandle", () => {

handle.unload()

// transition from unloaded to loading
handle.begin()
// reload to transition from unloaded to loading
handle.reload()

// simulate requesting from the network
handle.request()
Expand Down

0 comments on commit 8f0f839

Please sign in to comment.