Skip to content

Commit

Permalink
Repo.find: find documents by documentId in addition to url
Browse files Browse the repository at this point in the history
  • Loading branch information
HerbCaudill committed Oct 30, 2023
1 parent d99be68 commit 55ecd45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
36 changes: 12 additions & 24 deletions packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { next as Automerge } from "@automerge/automerge"
import debug from "debug"
import { EventEmitter } from "eventemitter3"
import { DocHandle, DocHandleEncodedChangePayload } from "./DocHandle.js"
import {
generateAutomergeUrl,
isValidAutomergeUrl,
interpretAsDocumentId,
parseAutomergeUrl,
parseLegacyUUID,
} from "./AutomergeUrl.js"
import { DocHandle, DocHandleEncodedChangePayload } from "./DocHandle.js"
import { throttle } from "./helpers/throttle.js"
import { NetworkAdapter } from "./network/NetworkAdapter.js"
import { NetworkSubsystem } from "./network/NetworkSubsystem.js"
import { StorageAdapter } from "./storage/StorageAdapter.js"
import { StorageSubsystem } from "./storage/StorageSubsystem.js"
import { CollectionSynchronizer } from "./synchronizer/CollectionSynchronizer.js"
import { DocumentId, PeerId, type AutomergeUrl } from "./types.js"
import type { AnyDocumentId, DocumentId, PeerId } from "./types.js"

/** A Repo is a collection of documents with networking, syncing, and storage capabilities. */
/** The `Repo` is the main entry point of this library
Expand Down Expand Up @@ -247,22 +246,11 @@ export class Repo extends EventEmitter<RepoEvents> {
* event to advertise interest in the document.
*/
find<T>(
/** The documentId of the handle to retrieve */
automergeUrl: AutomergeUrl
/** The url or documentId of the handle to retrieve */
id: AnyDocumentId
): DocHandle<T> {
if (!isValidAutomergeUrl(automergeUrl)) {
const maybeAutomergeUrl = parseLegacyUUID(automergeUrl)
if (maybeAutomergeUrl) {
console.warn(
"Legacy UUID document ID detected, converting to AutomergeUrl. This will be removed in a future version."
)
automergeUrl = maybeAutomergeUrl
} else {
throw new Error(`Invalid AutomergeUrl: '${automergeUrl}'`)
}
}
const documentId = interpretAsDocumentId(id)

const { documentId } = parseAutomergeUrl(automergeUrl)
// If we have the handle cached, return it
if (this.#handleCache[documentId]) {
if (this.#handleCache[documentId].isUnavailable()) {
Expand All @@ -282,16 +270,16 @@ export class Repo extends EventEmitter<RepoEvents> {
}

delete(
/** The documentId of the handle to delete */
id: DocumentId | AutomergeUrl
/** The url or documentId of the handle to delete */
id: AnyDocumentId
) {
if (isValidAutomergeUrl(id)) id = parseAutomergeUrl(id).documentId
const documentId = interpretAsDocumentId(id)

const handle = this.#getHandle(id, false)
const handle = this.#getHandle(documentId, false)
handle.delete()

delete this.#handleCache[id]
this.emit("delete-document", { documentId: id })
delete this.#handleCache[documentId]
this.emit("delete-document", { documentId })
}
}

Expand Down
21 changes: 17 additions & 4 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
AutomergeUrl,
DocHandle,
DocumentId,
LegacyDocumentId,
PeerId,
SharePolicy,
} from "../src/index.js"
Expand Down Expand Up @@ -54,7 +55,7 @@ describe("Repo", () => {
assert.equal(handle.isReady(), true)
})

it("can find a document once it's created", () => {
it("can find a document by url", () => {
const { repo } = setup()
const handle = repo.create<TestDoc>()
handle.change((d: TestDoc) => {
Expand All @@ -66,7 +67,19 @@ describe("Repo", () => {
assert.deepEqual(handle2.docSync(), { foo: "bar" })
})

it("can find a document using a legacy UUID (for now)", () => {
it("can find a document by its unprefixed document ID", () => {
const { repo } = setup()
const handle = repo.create<TestDoc>()
handle.change((d: TestDoc) => {
d.foo = "bar"
})

const handle2 = repo.find(handle.documentId)
assert.equal(handle, handle2)
assert.deepEqual(handle2.docSync(), { foo: "bar" })
})

it("can find a document by legacy UUID (for now)", () => {
disableConsoleWarn()

const { repo } = setup()
Expand All @@ -77,9 +90,9 @@ describe("Repo", () => {

const url = handle.url
const { binaryDocumentId } = parseAutomergeUrl(url)
const legacyDocumentId = Uuid.stringify(binaryDocumentId) as AutomergeUrl // a white lie
const legacyDocId = Uuid.stringify(binaryDocumentId) as LegacyDocumentId

const handle2 = repo.find(legacyDocumentId)
const handle2 = repo.find(legacyDocId)
assert.equal(handle, handle2)
assert.deepEqual(handle2.docSync(), { foo: "bar" })

Expand Down

0 comments on commit 55ecd45

Please sign in to comment.