-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from automerge/fix-use-document-async
improve handling of changing URLs in useDocument and useHandle
- Loading branch information
Showing
4 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { AutomergeUrl, DocHandle } from "@automerge/automerge-repo" | ||
import { useState } from "react" | ||
import { useEffect, useState } from "react" | ||
import { useRepo } from "./useRepo.js" | ||
|
||
/** A hook which returns a {@link DocHandle} identified by a URL. | ||
* | ||
* @remarks | ||
* This requires a {@link RepoContext} to be provided by a parent component. | ||
*/ | ||
export function useHandle<T>(automergeUrl: AutomergeUrl): DocHandle<T> { | ||
export function useHandle<T>(docUrl?: AutomergeUrl): DocHandle<T> | undefined { | ||
const repo = useRepo() | ||
const [handle] = useState<DocHandle<T>>(repo.find(automergeUrl)) | ||
const [handle, setHandle] = useState<DocHandle<T>>( | ||
docUrl ? repo.find(docUrl) : undefined | ||
) | ||
|
||
useEffect(() => { | ||
setHandle(docUrl ? repo.find(docUrl) : undefined) | ||
}, [docUrl]) | ||
|
||
return handle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/automerge-repo-react-hooks/test/useHandle.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { PeerId, Repo, AutomergeUrl } from "@automerge/automerge-repo" | ||
import { DummyStorageAdapter } from "@automerge/automerge-repo/test/helpers/DummyStorageAdapter" | ||
import { describe, it } from "vitest" | ||
import { RepoContext } from "../src/useRepo" | ||
import { useHandle } from "../src/useHandle" | ||
import { renderHook } from "@testing-library/react-hooks" | ||
import React, { useState } from "react" | ||
import assert from "assert" | ||
|
||
interface ExampleDoc { | ||
foo: string | ||
} | ||
|
||
function getRepoWrapper(repo: Repo) { | ||
return ({ children }) => ( | ||
<RepoContext.Provider value={repo}>{children}</RepoContext.Provider> | ||
) | ||
} | ||
|
||
describe("useHandle", () => { | ||
const repo = new Repo({ | ||
peerId: "bob" as PeerId, | ||
network: [], | ||
storage: new DummyStorageAdapter(), | ||
}) | ||
|
||
function setup() { | ||
const handleA = repo.create<ExampleDoc>() | ||
handleA.change(doc => (doc.foo = "A")) | ||
|
||
const handleB = repo.create<ExampleDoc>() | ||
handleB.change(doc => (doc.foo = "B")) | ||
|
||
return { | ||
repo, | ||
handleA, | ||
handleB, | ||
wrapper: getRepoWrapper(repo), | ||
} | ||
} | ||
|
||
it("loads a handle", async () => { | ||
const { handleA, wrapper } = setup() | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => { | ||
const handle = useHandle(handleA.url) | ||
|
||
return { | ||
handle, | ||
} | ||
}, | ||
{ wrapper } | ||
) | ||
|
||
assert.deepStrictEqual(result.current.handle, handleA) | ||
}) | ||
|
||
it("returns undefined when no url given", async () => { | ||
const { handleA, wrapper } = setup() | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => { | ||
const handle = useHandle() | ||
|
||
return { | ||
handle, | ||
} | ||
}, | ||
{ wrapper } | ||
) | ||
|
||
assert.deepStrictEqual(result.current.handle, undefined) | ||
}) | ||
|
||
it("updates the handle when the url changes", async () => { | ||
const { wrapper, handleA, handleB } = setup() | ||
|
||
const { result, waitForNextUpdate } = renderHook( | ||
() => { | ||
const [url, setUrl] = useState<AutomergeUrl>() | ||
const handle = useHandle(url) | ||
|
||
return { | ||
setUrl, | ||
handle, | ||
} | ||
}, | ||
{ wrapper } | ||
) | ||
|
||
// initially doc is undefined | ||
assert.deepStrictEqual(result.current.handle, undefined) | ||
|
||
// set url to doc A | ||
result.current.setUrl(handleA.url) | ||
await waitForNextUpdate() | ||
assert.deepStrictEqual(result.current.handle, handleA) | ||
|
||
// set url to doc B | ||
result.current.setUrl(handleB.url) | ||
await waitForNextUpdate() | ||
assert.deepStrictEqual(result.current.handle, handleB) | ||
|
||
// set url to undefined | ||
result.current.setUrl(undefined) | ||
await waitForNextUpdate() | ||
assert.deepStrictEqual(result.current.handle, undefined) | ||
}) | ||
}) |