Skip to content

Commit

Permalink
Add support for removing doc handle from handleCache (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgewsu authored Sep 11, 2024
1 parent 73361bb commit 3817343
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
42 changes: 41 additions & 1 deletion packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import {
interpretAsDocumentId,
parseAutomergeUrl,
} from "./AutomergeUrl.js"
import { DocHandle, DocHandleEncodedChangePayload } from "./DocHandle.js"
import {
DELETED,
DocHandle,
DocHandleEncodedChangePayload,
READY,
UNAVAILABLE,
UNLOADED,
} from "./DocHandle.js"
import { RemoteHeadsSubscriptions } from "./RemoteHeadsSubscriptions.js"
import { headsAreSame } from "./helpers/headsAreSame.js"
import { throttle } from "./helpers/throttle.js"
Expand Down Expand Up @@ -539,6 +546,39 @@ 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) {
if (handle.isReady()) {
handle.unload()
} else {
this.#log(
`WARN: removeFromCache called but handle for documentId: ${documentId} in unexpected state: ${handle.state}`
)
}
delete this.#handleCache[documentId]
// TODO: remove document from synchronizer when removeDocument is implemented
// this.synchronizer.removeDocument(documentId)
} else {
this.#log(
`WARN: removeFromCache called but doc undefined for documentId: ${documentId}`
)
}
}

shutdown(): Promise<void> {
this.networkSubsystem.adapters.forEach(adapter => {
adapter.disconnect()
Expand Down
33 changes: 33 additions & 0 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,39 @@ describe("Repo", () => {
const doc = await handle.doc()
expect(doc).toEqual({})
})

describe("handle cache", () => {
it("contains doc handle", async () => {
const { repo } = setup()
const handle = repo.create({ foo: "bar" })
await handle.doc()
assert(repo.handles[handle.documentId])
})

it("delete removes doc handle", async () => {
const { repo } = setup()
const handle = repo.create({ foo: "bar" })
await handle.doc()
await repo.delete(handle.documentId)
assert(repo.handles[handle.documentId] === undefined)
})

it("removeFromCache removes doc handle", async () => {
const { repo } = setup()
const handle = repo.create({ foo: "bar" })
await handle.doc()
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)
})
})
})

describe("flush behaviour", () => {
Expand Down

0 comments on commit 3817343

Please sign in to comment.