Skip to content

Commit

Permalink
some patch cleanup; still seeing errors in test logs for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed Jan 10, 2025
1 parent 964cd5e commit 664d3ee
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/automerge-repo-react-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ Most hooks depend on RepoContext being available.

Return a document & updater fn, by ID.

#### [useHandle](./src/useHandle.ts)
#### [useDocHandle](./src/useDocHandle.ts)

Return a handle, by ID.
4 changes: 2 additions & 2 deletions packages/automerge-repo-react-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* Most hooks depend on RepoContext being available.
*
* #### {@link useDocument}
* Return the current state of a document (or undefined) and a change function.
* Return the current state of a document and a change function.
*
* #### {@link useHandle}
* #### {@link useDocHandle}
* Return a DocHandle by passing in a DocumentURL.
*
* #### {@link useLocalAwareness} & {@link useRemoteAwareness}
Expand Down
7 changes: 4 additions & 3 deletions packages/automerge-repo-react-hooks/src/useDocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function useDocHandle<T>(
controllerRef.current?.abort()
controllerRef.current = new AbortController()

const promise = repo.find<T>(id, { signal: controllerRef.current.signal })
const promise = repo.find<T>(id, {
abortSignal: controllerRef.current.signal,
})
wrapper = wrapPromise(promise)
wrapperCache.set(id, wrapper)
}
Expand All @@ -52,8 +54,7 @@ export function useDocHandle<T>(
.then(handle => {
setHandle(handle as DocHandle<T>)
})
.catch(e => {
console.log("handle promise caught", e)
.catch(() => {
setHandle(undefined)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getRepoWrapper(repo: Repo) {
)
}

describe("useHandle", () => {
describe("useDocHandle", () => {
const repo = new Repo({
peerId: "bob" as PeerId,
})
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("useHandle", () => {
})
})

describe("useHandle with suspense: false", () => {
describe("useDocHandle with suspense: false", () => {
it("returns undefined while loading then resolves to handle", async () => {
const { handleA, repo, wrapper } = await setup()
const onHandle = vi.fn()
Expand Down
1 change: 1 addition & 0 deletions packages/automerge-repo/src/FindProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DocHandle } from "./DocHandle.js"

export type FindProgressState =
| "loading"
| "requesting"
| "ready"
| "failed"
| "aborted"
Expand Down
50 changes: 27 additions & 23 deletions packages/automerge-repo/src/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type {
} from "./types.js"
import { abortable, AbortOptions } from "./helpers/abortable.js"
import { FindProgress, FindProgressWithMethods } from "./FindProgress.js"
import { compute, createSignal, Signal } from "./helpers/signals.js"
import { createSignal, Signal } from "./helpers/signals.js"

function randomPeerId() {
return ("peer-" + Math.random().toString(36).slice(4)) as PeerId
Expand Down Expand Up @@ -509,30 +509,34 @@ export class Repo extends EventEmitter<RepoEvents> {
const { allowableStates = ["ready"], abortSignal } = options
const progressSignal = this.findWithSignalProgress<T>(id, { abortSignal })

return new Promise((resolve, reject) => {
const cleanup = progressSignal.subscribe(progress => {
// console.log("got signal", progress, allowableStates)
if (allowableStates.includes(progress.state)) {
cleanup()
this.#registerHandleWithSubsystems(progress.handle)
resolve(progress.handle)
}
switch (progress.state) {
case "failed":
cleanup()
reject(progress.error)
break
case "unavailable":
cleanup()
reject(new Error(`Document ${id} is unavailable`))
break
case "ready":
const resultPromise: Promise<DocHandle<T>> = new Promise(
(resolve, reject) => {
const cleanup = progressSignal.subscribe(progress => {
// console.log("got signal", progress, allowableStates)
if (allowableStates.includes(progress.state)) {
cleanup()
this.#registerHandleWithSubsystems(progress.handle)
resolve(progress.handle)
break
}
})
})
}
switch (progress.state) {
case "failed":
cleanup()
reject(progress.error)
break
case "unavailable":
cleanup()
reject(new Error(`Document ${id} is unavailable`))
break
case "ready":
cleanup()
resolve(progress.handle)
break
}
})
}
)

return Promise.race([resultPromise, abortable(abortSignal)])
}

findWithProgress<T>(
Expand Down

0 comments on commit 664d3ee

Please sign in to comment.