Skip to content

Commit

Permalink
Add comments and check for documentId being present
Browse files Browse the repository at this point in the history
  • Loading branch information
George Su committed Sep 11, 2024
1 parent 148b8a4 commit 96a7f78
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,17 @@ export class Repo extends EventEmitter<RepoEvents> {
)
}

/**
* Removes a DocHandle from the handleCache.
* @hidden this API is experimental and may change.
* @param documentId - documentId of the DocHandle to remove from handleCache, if present in cache.
* @returns Promise<void>
*/
async removeFromCache(documentId: DocumentId) {
if (!this.#handleCache[documentId]){
this.#log(`WARN: removeFromCache called but handle not found in handleCache for documentId: ${documentId}`)
return
}
const handle = this.#getHandle({ documentId })
const doc = await handle.doc([READY, UNLOADED, DELETED, UNAVAILABLE])
if (doc) {
Expand Down
8 changes: 8 additions & 0 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,14 @@ describe("Repo", () => {
await repo.removeFromCache(handle.documentId)
assert(repo.handles[handle.documentId] === undefined)
})

it("removeFromCache for documentId not found", async () => {
const { repo } = setup()
const badDocumentId = "badbadbad" as DocumentId
const handleCacheSize = Object.keys(repo.handles).length
await repo.removeFromCache(badDocumentId)
assert(Object.keys(repo.handles).length === handleCacheSize)
})
})
})

Expand Down

0 comments on commit 96a7f78

Please sign in to comment.