Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for removing doc handle from handleCache #380

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading