@automerge/automerge-repo-svelte-store 0.0.1

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 ADDED
@@ -0,0 +1,52 @@
1
+ # Svelte store for Automerge Repo
2
+
3
+ ## Example Usage
4
+
5
+ For a working example, see the [Svelte counter demo](../automerge-repo-demo-counter-svelte/).
6
+
7
+ `App.svelte`
8
+
9
+ ```svelte
10
+ <script lang="ts">
11
+ import { Repo } from "@automerge/automerge-repo"
12
+ import Counter from './lib/Counter.svelte'
13
+ import { setContextRepo } from "@automerge/automerge-repo-svelte-store"
14
+
15
+ const repo = new Repo({ /* repo config */ })
16
+
17
+ // Make the `Repo` available to child components (via Svelte's `setContext`).
18
+ setContextRepo(repo)
19
+
20
+ const docId = repo.create()
21
+ </script>
22
+
23
+ <main>
24
+ <div class="card">
25
+ <Counter {docId}/>
26
+ </div>
27
+ </main>
28
+ ```
29
+
30
+ `Counter.svelte`
31
+
32
+ ```svelte
33
+ <script lang="ts">
34
+ import type { DocumentId } from "@automerge/automerge-repo"
35
+ import { document } from "@automerge/automerge-repo-svelte-store"
36
+
37
+ export let docId: DocumentId
38
+
39
+ // `document` calls `getContextRepo` internally to access the closest `Repo`.
40
+ const doc = document<{count?: number}>(docId)
41
+ const increment = () => {
42
+ doc.change((d) => d.count = (d.count || 0) + 1)
43
+ }
44
+ </script>
45
+
46
+ <button on:click={increment}>
47
+ count is {$doc?.count || 0}
48
+ </button>
49
+ ```
50
+
51
+ ## Contributers
52
+ Originally written by Dylan MacKenzie ([@ecstatic-morse](https://github.com/ecstatic-morse)).
@@ -0,0 +1,9 @@
1
+ import type { Doc, ChangeFn } from "@automerge/automerge";
2
+ import { Repo, DocumentId } from "@automerge/automerge-repo";
3
+ export declare function getContextRepo(): Repo;
4
+ export declare function setContextRepo(repo: Repo): void;
5
+ export declare function document<T>(documentId: DocumentId): {
6
+ subscribe: (this: void, run: import("svelte/store").Subscriber<Doc<T>>, invalidate?: (value?: Doc<T>) => void) => import("svelte/store").Unsubscriber;
7
+ change: (fn: ChangeFn<T>) => void;
8
+ };
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAGzD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAyB,MAAM,2BAA2B,CAAA;AAInF,wBAAgB,cAAc,IAAI,IAAI,CAErC;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,IAAI,QAExC;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU;;iBAWjC,SAAS,CAAC,CAAC;EAI3B"}
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import { setContext, getContext } from "svelte";
2
+ import { writable } from "svelte/store";
3
+ const ContextRepoKey = Symbol("svelte-context-automerge-repo");
4
+ export function getContextRepo() {
5
+ return getContext(ContextRepoKey);
6
+ }
7
+ export function setContextRepo(repo) {
8
+ setContext(ContextRepoKey, repo);
9
+ }
10
+ export function document(documentId) {
11
+ const repo = getContextRepo();
12
+ const handle = repo.find(documentId);
13
+ const { set, subscribe } = writable(null, () => {
14
+ const onPatch = (h) => set(h.after);
15
+ handle.addListener("patch", onPatch);
16
+ return () => handle.removeListener("patch", onPatch);
17
+ });
18
+ return {
19
+ subscribe,
20
+ change: (fn) => {
21
+ handle.change(fn);
22
+ },
23
+ };
24
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@automerge/automerge-repo-svelte-store",
3
+ "version": "0.0.1",
4
+ "description": "A Svelte store containing your automerge documentsj",
5
+ "repository": "https://github.com/pvh/automerge-repo",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "type": "module",
9
+ "main": "dist/index.js",
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "watch": "npm-watch"
13
+ },
14
+ "dependencies": {
15
+ "@automerge/automerge-repo": "^0.0.1",
16
+ "svelte": "^3.0.0"
17
+ },
18
+ "watch": {
19
+ "build": {
20
+ "patterns": "./src/**/*",
21
+ "extensions": [
22
+ ".ts"
23
+ ]
24
+ }
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "gitHead": "e572f26ae416140b025c3ba557d9f781abbdada1"
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ import type { Doc, ChangeFn } from "@automerge/automerge"
2
+ import { setContext, getContext } from "svelte"
3
+ import { writable } from "svelte/store"
4
+ import { Repo, DocumentId, DocHandlePatchPayload } from "@automerge/automerge-repo"
5
+
6
+ const ContextRepoKey = Symbol("svelte-context-automerge-repo")
7
+
8
+ export function getContextRepo(): Repo {
9
+ return getContext<Repo>(ContextRepoKey)
10
+ }
11
+
12
+ export function setContextRepo(repo: Repo) {
13
+ setContext(ContextRepoKey, repo)
14
+ }
15
+
16
+ export function document<T>(documentId: DocumentId) {
17
+ const repo = getContextRepo()
18
+ const handle = repo.find<T>(documentId)
19
+ const { set, subscribe } = writable<Doc<T>>(null, () => {
20
+ const onPatch = (h: DocHandlePatchPayload<T>) => set(h.after)
21
+ handle.addListener("patch", onPatch)
22
+ return () => handle.removeListener("patch", onPatch)
23
+ })
24
+
25
+ return {
26
+ subscribe,
27
+ change: (fn: ChangeFn<T>) => {
28
+ handle.change(fn)
29
+ },
30
+ }
31
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "jsx": "react",
5
+ "module": "ESNext",
6
+ "moduleResolution": "node",
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "outDir": "./dist",
10
+ "esModuleInterop": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "strict": false,
13
+ "skipLibCheck": true
14
+ },
15
+ "include": ["src/**/*.ts"]
16
+ }