@homeostate/crdt-automerge 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @homeostate/crdt-automerge
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 8bd452e: Init release
8
+ - Updated dependencies [8bd452e]
9
+ - @homeostate/core@0.1.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aliaksandr Mialeshka
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # @homeostate/crdt-automerge
2
+
3
+ > **Draft / placeholder release.** `0.0.0` reserves the name while the API is still
4
+ > being designed. Nothing here is stable — do not depend on it yet.
5
+
6
+ [Automerge](https://automerge.org) backend for
7
+ [`@homeostate/core`](https://github.com/mixedrays/homeostate/tree/main/packages/core).
8
+ It maps the synced state onto one key of an Automerge document (nested objects become
9
+ maps, arrays lists, strings text) and writes fine-grained operations derived from
10
+ `getChanges`, one Automerge change per `write`.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install @homeostate/core @homeostate/crdt-automerge @automerge/automerge
16
+ ```
17
+
18
+ `@automerge/automerge` 3.x is a peer dependency.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import * as A from "@automerge/automerge";
24
+ import { createSyncEngine } from "@homeostate/core";
25
+ import {
26
+ createAutomergeBackend,
27
+ createAutomergeHandle,
28
+ } from "@homeostate/crdt-automerge";
29
+
30
+ const handle = createAutomergeHandle(A.init());
31
+ const engine = createSyncEngine(
32
+ createAutomergeBackend(handle, "shared"),
33
+ adapter,
34
+ );
35
+ engine.connect();
36
+ ```
37
+
38
+ Automerge documents are immutable values: every change returns a new document and leaves
39
+ the old one behind. `createAutomergeHandle` holds the current document so the backend and
40
+ your replication code share it:
41
+
42
+ - `handle.doc()` returns the current document.
43
+ - `handle.change(fn)` applies a local change through `A.change`.
44
+ - `handle.update((doc) => ...)` replaces the document with the result of `A.merge`,
45
+ `A.applyChanges`, `A.loadIncremental`, `A.receiveSyncMessage`, or any other Automerge call.
46
+ - `handle.subscribe(({ doc, local }) => ...)` fires whenever the heads change; `local` is
47
+ true for `change` and false for `update`.
48
+
49
+ The synced state lives in `handle.doc().shared`; a middle-of-array delete, a toggle, or a
50
+ keystroke each produce one small change. Replication is yours to wire, for example:
51
+
52
+ ```ts
53
+ handle.subscribe(({ doc, local }) => {
54
+ if (local) send(A.getLastLocalChange(doc));
55
+ });
56
+ onMessage((change) => handle.update((doc) => A.applyChanges(doc, [change])[0]));
57
+ ```
58
+
59
+ The engine hears about every change that did not come through its own `write`, imports
60
+ and local edits alike.
61
+
62
+ `read()` returns plain JSON. Automerge keeps every object that the last change did not
63
+ touch, so the backend caches copies per source object and a read after a toggle copies only
64
+ the containers on the path to the toggled item. Snapshots are shared between reads; treat
65
+ them as immutable.
66
+
67
+ `undefined` is not JSON and Automerge rejects it, so the backend drops object entries whose
68
+ value is `undefined` and stores `undefined` array items as `null`, as `JSON.stringify` does.
69
+
70
+ `@automerge/automerge` ships WebAssembly. Node loads it directly; for bundlers see
71
+ [Automerge's documentation](https://automerge.org/docs/), or import
72
+ `@automerge/automerge/slim` and initialize the module yourself.
73
+
74
+ ## License
75
+
76
+ MIT — see [LICENSE](./LICENSE). Includes code derived from
77
+ [zustand-middleware-yjs](https://github.com/joebobmiles/zustand-middleware-yjs);
78
+ see [THIRD_PARTY_NOTICES.md](./THIRD_PARTY_NOTICES.md).
@@ -0,0 +1,33 @@
1
+ # Third-Party Notices
2
+
3
+ ## zustand-middleware-yjs
4
+
5
+ `@homeostate/crdt-automerge` includes code derived from
6
+ [zustand-middleware-yjs](https://github.com/joebobmiles/zustand-middleware-yjs).
7
+
8
+ Copyright (c) 2021 Joseph R Miles
9
+
10
+ The upstream project is licensed under the MIT License:
11
+
12
+ ```text
13
+ The MIT License (MIT)
14
+
15
+ Copyright (c) 2021 Joseph R Miles
16
+
17
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
18
+ this software and associated documentation files (the "Software"), to deal in
19
+ the Software without restriction, including without limitation the rights to
20
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
21
+ the Software, and to permit persons to whom the Software is furnished to do so,
22
+ subject to the following conditions:
23
+
24
+ The above copyright notice and this permission notice shall be included in all
25
+ copies or substantial portions of the Software.
26
+
27
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
29
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
30
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
31
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
+ ```
@@ -0,0 +1,5 @@
1
+ import type { CrdtBackend } from "@homeostate/core";
2
+ import type { AutomergeHandle } from "./handle.js";
3
+ import { type Container } from "./patching.js";
4
+ export declare const createAutomergeBackend: <T extends Container>(handle: AutomergeHandle<T>, name: string) => CrdtBackend;
5
+ //# sourceMappingURL=automerge-backend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"automerge-backend.d.ts","sourceRoot":"","sources":["../src/automerge-backend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,kBAAkB,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,eAAe,CAAC;AAG3E,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,SAAS,EACxD,QAAQ,eAAe,CAAC,CAAC,CAAC,EAC1B,MAAM,MAAM,KACX,WA4BF,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { applyChanges, diff, toJson } from "./patching.js";
2
+ import { createSnapshot } from "./snapshot.js";
3
+ export const createAutomergeBackend = (handle, name) => {
4
+ const snapshot = createSnapshot();
5
+ let writing = false;
6
+ const current = () => handle.doc()[name];
7
+ return {
8
+ read: () => snapshot(current()) ?? {},
9
+ write: (next) => {
10
+ const changes = diff(current(), next);
11
+ if (changes?.length === 0)
12
+ return;
13
+ writing = true;
14
+ try {
15
+ handle.change((doc) => {
16
+ if (changes === null)
17
+ doc[name] = toJson(next);
18
+ else
19
+ applyChanges(doc, [name], changes);
20
+ });
21
+ }
22
+ finally {
23
+ writing = false;
24
+ }
25
+ },
26
+ subscribe: (onRemoteChange) => handle.subscribe(() => {
27
+ if (!writing)
28
+ onRemoteChange();
29
+ }),
30
+ };
31
+ };
32
+ //# sourceMappingURL=automerge-backend.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"automerge-backend.js","sourceRoot":"","sources":["../src/automerge-backend.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAkB,MAAM,eAAe,CAAC;AAC3E,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,MAA0B,EAC1B,IAAY,EACC,EAAE;IACf,MAAM,QAAQ,GAAG,cAAc,EAAE,CAAC;IAClC,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,MAAM,OAAO,GAAG,GAAY,EAAE,CAAE,MAAM,CAAC,GAAG,EAAgB,CAAC,IAAI,CAAC,CAAC;IAEjE,OAAO;QACL,IAAI,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE;QAErC,KAAK,EAAE,CAAC,IAAI,EAAE,EAAE;YACd,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,CAAC;YACtC,IAAI,OAAO,EAAE,MAAM,KAAK,CAAC;gBAAE,OAAO;YAClC,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC;gBACH,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;oBACpB,IAAI,OAAO,KAAK,IAAI;wBAAG,GAAiB,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;;wBACzD,YAAY,CAAC,GAAgB,EAAE,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;gBACvD,CAAC,CAAC,CAAC;YACL,CAAC;oBAAS,CAAC;gBACT,OAAO,GAAG,KAAK,CAAC;YAClB,CAAC;QACH,CAAC;QAED,SAAS,EAAE,CAAC,cAAc,EAAe,EAAE,CACzC,MAAM,CAAC,SAAS,CAAC,GAAG,EAAE;YACpB,IAAI,CAAC,OAAO;gBAAE,cAAc,EAAE,CAAC;QACjC,CAAC,CAAC;KACL,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ import * as A from "@automerge/automerge";
2
+ import type { Unsubscribe } from "@homeostate/core";
3
+ export interface AutomergeHandleEvent<T> {
4
+ doc: A.Doc<T>;
5
+ local: boolean;
6
+ }
7
+ export interface AutomergeHandle<T = Record<string, unknown>> {
8
+ doc: () => A.Doc<T>;
9
+ change: (fn: A.ChangeFn<T>) => void;
10
+ update: (fn: (doc: A.Doc<T>) => A.Doc<T>) => void;
11
+ subscribe: (listener: (event: AutomergeHandleEvent<T>) => void) => Unsubscribe;
12
+ }
13
+ export declare const createAutomergeHandle: <T = Record<string, unknown>>(initial?: A.Doc<T>) => AutomergeHandle<T>;
14
+ //# sourceMappingURL=handle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,sBAAsB,CAAC;AAC1C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAEpD,MAAM,WAAW,oBAAoB,CAAC,CAAC;IACrC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACd,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC1D,GAAG,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACpB,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;IACpC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;IAClD,SAAS,EAAE,CACT,QAAQ,EAAE,CAAC,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAAC,KAAK,IAAI,KAC/C,WAAW,CAAC;CAClB;AAKD,eAAO,MAAM,qBAAqB,GAAI,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/D,UAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAe,KAC9B,eAAe,CAAC,CAAC,CA4BnB,CAAC"}
package/dist/handle.js ADDED
@@ -0,0 +1,29 @@
1
+ import * as A from "@automerge/automerge";
2
+ const sameHeads = (a, b) => a.length === b.length && a.every((hash, i) => hash === b[i]);
3
+ export const createAutomergeHandle = (initial = A.init()) => {
4
+ let current = initial;
5
+ const listeners = new Set();
6
+ const replace = (operation, local) => {
7
+ const before = A.getHeads(current);
8
+ const next = operation(current);
9
+ if (next === current)
10
+ return;
11
+ current = next;
12
+ if (sameHeads(before, A.getHeads(next)))
13
+ return;
14
+ const event = { doc: next, local };
15
+ listeners.forEach((listener) => listener(event));
16
+ };
17
+ return {
18
+ doc: () => current,
19
+ change: (fn) => replace((doc) => A.change(doc, fn), true),
20
+ update: (fn) => replace(fn, false),
21
+ subscribe: (listener) => {
22
+ listeners.add(listener);
23
+ return () => {
24
+ listeners.delete(listener);
25
+ };
26
+ },
27
+ };
28
+ };
29
+ //# sourceMappingURL=handle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handle.js","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,sBAAsB,CAAC;AAiB1C,MAAM,SAAS,GAAG,CAAC,CAAU,EAAE,CAAU,EAAW,EAAE,CACpD,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAE/D,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAC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-backend.js";
2
+ export { createAutomergeHandle } from "./handle.js";
3
+ export type { AutomergeHandle, AutomergeHandleEvent } from "./handle.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,YAAY,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createAutomergeBackend } from "./automerge-backend.js";
2
+ export { createAutomergeHandle } from "./handle.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,7 @@
1
+ import * as A from "@automerge/automerge";
2
+ import { type Change } from "@homeostate/core";
3
+ export type Container = Record<string, unknown>;
4
+ export declare const toJson: (value: unknown) => unknown;
5
+ export declare const diff: (current: unknown, next: unknown) => Change[] | null;
6
+ export declare const applyChanges: (doc: Container, path: A.Prop[], changes: Change[]) => void;
7
+ //# sourceMappingURL=patching.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patching.d.ts","sourceRoot":"","sources":["../src/patching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,sBAAsB,CAAC;AAC1C,OAAO,EAA0B,KAAK,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAEvE,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAKhD,eAAO,MAAM,MAAM,GAAI,OAAO,OAAO,KAAG,OAQvC,CAAC;AAEF,eAAO,MAAM,IAAI,GAAI,SAAS,OAAO,EAAE,MAAM,OAAO,KAAG,MAAM,EAAE,GAAG,IAGjE,CAAC;AAEF,eAAO,MAAM,YAAY,GACvB,KAAK,SAAS,EACd,MAAM,CAAC,CAAC,IAAI,EAAE,EACd,SAAS,MAAM,EAAE,KAChB,IAaF,CAAC"}
@@ -0,0 +1,70 @@
1
+ import * as A from "@automerge/automerge";
2
+ import { ChangeType, getChanges } from "@homeostate/core";
3
+ const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
4
+ export const toJson = (value) => {
5
+ if (Array.isArray(value))
6
+ return value.map((item) => (item === undefined ? null : toJson(item)));
7
+ if (!isRecord(value))
8
+ return value;
9
+ const result = {};
10
+ for (const [key, item] of Object.entries(value))
11
+ if (item !== undefined)
12
+ result[key] = toJson(item);
13
+ return result;
14
+ };
15
+ export const diff = (current, next) => {
16
+ if (!isRecord(next))
17
+ return [];
18
+ return isRecord(current) ? getChanges(current, next) : null;
19
+ };
20
+ export const applyChanges = (doc, path, changes) => {
21
+ const container = path.reduce((value, prop) => value[prop], doc);
22
+ for (const [type, key, value] of changes) {
23
+ if (typeof container === "string")
24
+ applyToText(doc, path, type, key, value);
25
+ else if (Array.isArray(container))
26
+ applyToList(doc, path, container, type, key, value);
27
+ else
28
+ applyToMap(doc, path, container, type, key, value);
29
+ }
30
+ };
31
+ const applyToMap = (doc, path, map, type, key, value) => {
32
+ switch (type) {
33
+ case ChangeType.INSERT:
34
+ case ChangeType.UPDATE:
35
+ if (value !== undefined)
36
+ map[key] = toJson(value);
37
+ else if (key in map)
38
+ delete map[key];
39
+ break;
40
+ case ChangeType.DELETE:
41
+ delete map[key];
42
+ break;
43
+ case ChangeType.PENDING:
44
+ applyChanges(doc, [...path, key], value);
45
+ break;
46
+ }
47
+ };
48
+ const applyToList = (doc, path, list, type, index, value) => {
49
+ switch (type) {
50
+ case ChangeType.INSERT:
51
+ A.insertAt(list, index, toJson(value));
52
+ break;
53
+ case ChangeType.UPDATE:
54
+ list[index] = toJson(value);
55
+ break;
56
+ case ChangeType.DELETE:
57
+ A.deleteAt(list, index, 1);
58
+ break;
59
+ case ChangeType.PENDING:
60
+ applyChanges(doc, [...path, index], value);
61
+ break;
62
+ }
63
+ };
64
+ const applyToText = (doc, path, type, index, value) => {
65
+ if (type === ChangeType.INSERT)
66
+ A.splice(doc, path, index, 0, value);
67
+ else if (type === ChangeType.DELETE)
68
+ A.splice(doc, path, index, 1);
69
+ };
70
+ //# sourceMappingURL=patching.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patching.js","sourceRoot":"","sources":["../src/patching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,sBAAsB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,UAAU,EAAe,MAAM,kBAAkB,CAAC;AAIvE,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAsB,EAAE,CACtD,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAEvE,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,KAAc,EAAW,EAAE;IAChD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACtB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACnC,MAAM,MAAM,GAAc,EAAE,CAAC;IAC7B,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;QAC7C,IAAI,IAAI,KAAK,SAAS;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,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,MAAM;QAC5B,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAe,CAAC,CAAC;SAC5C,IAAI,IAAI,KAAK,UAAU,CAAC,MAAM;QAAE,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;AACrE,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const createSnapshot: () => ((value: unknown) => unknown);
2
+ //# sourceMappingURL=snapshot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,QAAO,CAAC,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAiB7D,CAAC"}
@@ -0,0 +1,17 @@
1
+ export const createSnapshot = () => {
2
+ const copies = new WeakMap();
3
+ const copy = (value) => {
4
+ if (value === null || typeof value !== "object")
5
+ return value;
6
+ const cached = copies.get(value);
7
+ if (cached !== undefined)
8
+ return cached;
9
+ const result = Array.isArray(value)
10
+ ? value.map(copy)
11
+ : Object.fromEntries(Object.entries(value).map(([key, item]) => [key, copy(item)]));
12
+ copies.set(value, result);
13
+ return result;
14
+ };
15
+ return copy;
16
+ };
17
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.js","sourceRoot":"","sources":["../src/snapshot.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,cAAc,GAAG,GAAkC,EAAE;IAChE,MAAM,MAAM,GAAG,IAAI,OAAO,EAAmB,CAAC;IAE9C,MAAM,IAAI,GAAG,CAAC,KAAc,EAAW,EAAE;QACvC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;QAC9D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,IAAI,MAAM,KAAK,SAAS;YAAE,OAAO,MAAM,CAAC;QACxC,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACjC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;YACjB,CAAC,CAAC,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAC9D,CAAC;QACN,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC1B,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@homeostate/crdt-automerge",
3
+ "version": "0.1.1",
4
+ "description": "Automerge CRDT backend for @homeostate/core",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/mixedrays/homeostate.git",
10
+ "directory": "packages/crdt-automerge"
11
+ },
12
+ "homepage": "https://github.com/mixedrays/homeostate#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/mixedrays/homeostate/issues"
15
+ },
16
+ "keywords": [
17
+ "automerge",
18
+ "crdt",
19
+ "sync",
20
+ "state-management",
21
+ "collaborative"
22
+ ],
23
+ "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ }
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "README.md",
34
+ "LICENSE",
35
+ "THIRD_PARTY_NOTICES.md",
36
+ "CHANGELOG.md"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "dependencies": {
42
+ "@homeostate/core": "^0.1.1"
43
+ },
44
+ "peerDependencies": {
45
+ "@automerge/automerge": "^3.0.0"
46
+ },
47
+ "devDependencies": {
48
+ "@automerge/automerge": "^3.4.1"
49
+ },
50
+ "scripts": {
51
+ "build": "tsc -b"
52
+ }
53
+ }