@automerge/automerge-repo-react-hooks 1.1.4 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,81 +2,23 @@
2
2
 
3
3
  These hooks are provided as helpers for using Automerge in your React project.
4
4
 
5
- #### [useBootstrap](./src/useBootstrap.ts)
6
- This hook is used to load a document based on the URL hash, for example `//myapp/#documentId=[document ID]`.
7
- It can also load the document ID from localStorage, or create a new document if none is specified.
8
-
9
5
  #### [useLocalAwareness](./src/useLocalAwareness.ts) & [useRemoteAwareness](./src/useRemoteAwareness.ts)
6
+
10
7
  These hooks implement ephemeral awareness/presence, similar to [Yjs Awareness](https://docs.yjs.dev/getting-started/adding-awareness).
11
- They allow temporary state to be shared, such as cursor positions or peer online/offline status.
8
+ They allow temporary state to be shared, such as cursor positions or peer online/offline status.
12
9
 
13
10
  Ephemeral messages are replicated between peers, but not saved to the Automerge doc, and are used for temporary updates that will be discarded.
14
11
 
15
12
  #### [useRepo/RepoContext](./src/useRepo.ts)
13
+
16
14
  Use RepoContext to set up react context for an Automerge repo.
17
15
  Use useRepo to lookup the repo from context.
18
16
  Most hooks depend on RepoContext being available.
19
17
 
20
- #### [useDocument](./src/useDocument.ts)
18
+ #### [useDocument](useDocument.ts)
19
+
21
20
  Return a document & updater fn, by ID.
22
21
 
23
22
  #### [useHandle](./src/useHandle.ts)
24
- Return a handle, by ID.
25
-
26
- ## Example usage
27
-
28
- ### App Setup
29
-
30
- ```ts
31
- import React, { StrictMode } from "react"
32
- import ReactDOM from "react-dom/client"
33
-
34
- import { Repo, DocCollection } from "@automerge/automerge-repo"
35
23
 
36
- import { BroadcastChannelNetworkAdapter } from "@automerge/automerge-repo-network-broadcastchannel"
37
-
38
- import App, { RootDocument } from "./App.js"
39
- import { RepoContext } from "@automerge/automerge-repo-react-hooks"
40
-
41
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
42
- const sharedWorker = new SharedWorker(
43
- new URL("./shared-worker.js", import.meta.url),
44
- { type: "module", name: "@automerge/automerge-repo-shared-worker" }
45
- )
46
-
47
- async function getRepo(): Promise<DocCollection> {
48
- return await Repo({
49
- network: [
50
- new BroadcastChannelNetworkAdapter(),
51
- ],
52
- sharePolicy: peerId => peerId.includes("shared-worker"),
53
- })
54
- }
55
-
56
- const initFunction = (d: RootDocument) => {
57
- d.items = []
58
- }
59
-
60
- const queryString = window.location.search // Returns:'?q=123'
61
-
62
- // Further parsing:
63
- const params = new URLSearchParams(queryString)
64
- const hostname = params.get("host") || "automerge-storage-demo.glitch.me"
65
-
66
- getRepo().then(repo => {
67
- useBootstrap(repo, initFunction).then(rootDoc => {
68
- const rootElem = document.getElementById("root")
69
- if (!rootElem) {
70
- throw new Error("The 'root' element wasn't found in the host HTML doc.")
71
- }
72
- const root = ReactDOM.createRoot(rootElem)
73
- root.render(
74
- <StrictMode>
75
- <RepoContext.Provider value={repo}>
76
- <App rootDocumentId={rootDoc.documentId} />
77
- </RepoContext.Provider>
78
- </StrictMode>
79
- )
80
- })
81
- })
82
- ```
24
+ Return a handle, by ID.
package/dist/index.d.ts CHANGED
@@ -5,90 +5,29 @@
5
5
  *
6
6
  * These hooks are provided as helpers for using Automerge in your React project.
7
7
  *
8
- * #### {@link useBootstrap}
9
- * This hook is used to load a document based on the URL hash, for example `//myapp/#documentId=[document ID]`.
10
- * It can also load the document ID from localStorage, or create a new document if none is specified.
11
- *
12
- * #### {@link useLocalAwareness} & {@link useRemoteAwareness}
13
- * These hooks implement ephemeral awareness/presence, similar to [Yjs Awareness](https://docs.yjs.dev/getting-started/adding-awareness).
14
- * They allow temporary state to be shared, such as cursor positions or peer online/offline status.
15
- *
16
- * Ephemeral messages are replicated between peers, but not saved to the Automerge doc, and are used for temporary updates that will be discarded.
17
- *
18
8
  * #### {@link useRepo}/{@link RepoContext}
19
9
  * Use RepoContext to set up react context for an Automerge repo.
20
10
  * Use useRepo to lookup the repo from context.
21
11
  * Most hooks depend on RepoContext being available.
22
12
  *
23
- * #### {@link useDocument }
24
- * Return a document & updater fn, by ID.
25
- *
26
- * #### {@link useHandle }
27
- * Return a handle, by ID.
28
- *
29
- * ## Example usage
30
- *
31
- * ### App Setup
13
+ * #### {@link useDocument}
14
+ * Return the current state of a document (or undefined) and a change function.
32
15
  *
33
- * ```ts
34
- * import React, { StrictMode } from "react"
35
- * import ReactDOM from "react-dom/client"
16
+ * #### {@link useHandle}
17
+ * Return a DocHandle by passing in a DocumentURL.
36
18
  *
37
- * import { Repo, DocCollection } from "@automerge/automerge-repo"
38
- *
39
- * import { BroadcastChannelNetworkAdapter } from "@automerge/automerge-repo-network-broadcastchannel"
40
- *
41
- * import App, { RootDocument } from "./App.js"
42
- * import { RepoContext } from "@automerge/automerge-repo-react-hooks"
43
- *
44
- * // eslint-disable-next-line @typescript-eslint/no-unused-vars
45
- * const sharedWorker = new SharedWorker(
46
- * new URL("./shared-worker.js", import.meta.url),
47
- * { type: "module", name: "@automerge/automerge-repo-shared-worker" }
48
- * )
49
- *
50
- * async function getRepo(): Promise<DocCollection> {
51
- * return await Repo({
52
- * network: [
53
- * new BroadcastChannelNetworkAdapter(),
54
- * ],
55
- * sharePolicy: peerId => peerId.includes("shared-worker"),
56
- * })
57
- * }
58
- *
59
- * const initFunction = (d: RootDocument) => {
60
- * d.items = []
61
- * }
62
- *
63
- * const queryString = window.location.search // Returns:'?q=123'
19
+ * #### {@link useLocalAwareness} & {@link useRemoteAwareness}
20
+ * These hooks implement ephemeral awareness/presence, similar to [Yjs Awareness](https://docs.yjs.dev/getting-started/adding-awareness).
21
+ * They allow temporary state to be shared, such as cursor positions or peer online/offline status.
64
22
  *
65
- * // Further parsing:
66
- * const params = new URLSearchParams(queryString)
67
- * const hostname = params.get("host") || "automerge-storage-demo.glitch.me"
23
+ * Ephemeral messages are replicated between peers, but not saved to the Automerge doc, and are used for temporary updates that will be discarded.
68
24
  *
69
- * getRepo().then(repo => {
70
- * useBootstrap(repo, initFunction).then(rootDoc => {
71
- * const rootElem = document.getElementById("root")
72
- * if (!rootElem) {
73
- * throw new Error("The 'root' element wasn't found in the host HTML doc.")
74
- * }
75
- * const root = ReactDOM.createRoot(rootElem)
76
- * root.render(
77
- * <StrictMode>
78
- * <RepoContext.Provider value={repo}>
79
- * <App rootDocumentId={rootDoc.documentId} />
80
- * </RepoContext.Provider>
81
- * </StrictMode>
82
- * )
83
- * })
84
- * })
85
- * ```
86
25
  */
87
- export { useDocument } from "./useDocument.js";
88
- export { useDocuments } from "./useDocuments.js";
89
- export { useBootstrap, type UseBootstrapOptions } from "./useBootstrap.js";
90
- export { useHandle } from "./useHandle.js";
91
- export { RepoContext, useRepo } from "./useRepo.js";
92
- export { useLocalAwareness, type UseLocalAwarenessProps, } from "./useLocalAwareness.js";
93
- export { useRemoteAwareness, type PeerStates, type Heartbeats, type UseRemoteAwarenessProps, } from "./useRemoteAwareness.js";
26
+ export { useDocument } from './useDocument.js';
27
+ export { useDocuments } from './useDocuments.js';
28
+ export { useBootstrap, type UseBootstrapOptions } from './useBootstrap.js';
29
+ export { useHandle } from './useHandle.js';
30
+ export { RepoContext, useRepo } from './useRepo.js';
31
+ export { useLocalAwareness, type UseLocalAwarenessProps, } from './useLocalAwareness.js';
32
+ export { useRemoteAwareness, type PeerStates, type Heartbeats, type UseRemoteAwarenessProps, } from './useRemoteAwareness.js';
94
33
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqFG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EACL,iBAAiB,EACjB,KAAK,sBAAsB,GAC5B,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EACL,kBAAkB,EAClB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,uBAAuB,GAC7B,MAAM,yBAAyB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EACL,iBAAiB,EACjB,KAAK,sBAAsB,GAC5B,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EACL,kBAAkB,EAClB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,uBAAuB,GAC7B,MAAM,yBAAyB,CAAA"}