bireactive 0.3.2 → 0.3.4
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/dist/automerge/doc-cell.d.ts +28 -11
- package/dist/automerge/doc-cell.js +20 -13
- package/dist/automerge/index.d.ts +3 -2
- package/dist/automerge/index.js +6 -5
- package/dist/automerge/reconcile.d.ts +14 -2
- package/dist/automerge/reconcile.js +119 -15
- package/package.json +12 -4
|
@@ -1,20 +1,37 @@
|
|
|
1
1
|
import type { DocHandle } from "@automerge/automerge-repo";
|
|
2
2
|
import { type Cell, type Writable } from "../core/cell.js";
|
|
3
3
|
import { type Store } from "../core/store.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import { type By, type Replace } from "./reconcile.js";
|
|
5
|
+
/** Bridge options shared by every `connect*` entry. */
|
|
6
|
+
export interface DocOptions {
|
|
7
|
+
/** Identity key for list elements, enabling keyed reconciliation (see `reconcile`). */
|
|
8
|
+
by?: By;
|
|
9
|
+
/** Keys whose values are written wholesale (a `put`) rather than deep-merged —
|
|
10
|
+
* for opaque blobs a downstream bridge can only apply as whole-object puts
|
|
11
|
+
* (see `reconcile`'s `Replace`). */
|
|
12
|
+
replace?: Replace;
|
|
13
|
+
}
|
|
14
|
+
/** Lifecycle shared by every bridge: retarget the doc, detach both directions. */
|
|
15
|
+
export interface DocLifecycle<T> {
|
|
10
16
|
/** Point the same cell at a different doc, keeping every bound lens/view alive. */
|
|
11
17
|
retarget: (handle: DocHandle<T>) => void;
|
|
12
18
|
/** Detach both directions (call from `disconnectedCallback`). */
|
|
13
19
|
dispose: () => void;
|
|
14
20
|
}
|
|
21
|
+
/** Doc bridged to a writable cell; writes (direct or via a lens) flow to the CRDT. */
|
|
22
|
+
export interface CellBridge<T> extends DocLifecycle<T> {
|
|
23
|
+
cell: Writable<Cell<T>>;
|
|
24
|
+
}
|
|
25
|
+
/** Doc bridged to a deep `store` — `bridge.store.a.b.value = x` commits to the doc. */
|
|
26
|
+
export interface StoreBridge<T> extends DocLifecycle<T> {
|
|
27
|
+
store: Store<T>;
|
|
28
|
+
}
|
|
29
|
+
/** Doc bridged to both a cell and a deep store. */
|
|
30
|
+
export interface DocBridge<T> extends CellBridge<T>, StoreBridge<T> {
|
|
31
|
+
}
|
|
32
|
+
/** Connect a `DocHandle` to a reactive cell, syncing both ways. */
|
|
33
|
+
export declare function connectCell<T extends object>(handle: DocHandle<T>, opts?: DocOptions): CellBridge<T>;
|
|
34
|
+
/** Connect a `DocHandle` to a deep `store`, syncing both ways. */
|
|
35
|
+
export declare function connectStore<T extends object>(handle: DocHandle<T>, opts?: DocOptions): StoreBridge<T>;
|
|
15
36
|
/** Connect a `DocHandle` to a reactive cell + store, syncing both ways. */
|
|
16
|
-
export declare function connectDoc<T extends object>(handle: DocHandle<T
|
|
17
|
-
/** Doc as a writable cell (page-lifetime; use {@link connectDoc} when you need disposal). */
|
|
18
|
-
export declare function docCell<T extends object>(handle: DocHandle<T>): Writable<Cell<T>>;
|
|
19
|
-
/** Doc as a deep store (page-lifetime; use {@link connectDoc} when you need disposal). */
|
|
20
|
-
export declare function docStore<T extends object>(handle: DocHandle<T>): Store<T>;
|
|
37
|
+
export declare function connectDoc<T extends object>(handle: DocHandle<T>, opts?: DocOptions): DocBridge<T>;
|
|
@@ -39,42 +39,49 @@ function deepEqual(a, b) {
|
|
|
39
39
|
return true;
|
|
40
40
|
}
|
|
41
41
|
/** Wire an existing cell to a handle in both directions; returns an unbind. */
|
|
42
|
-
function bind(c, handle) {
|
|
42
|
+
function bind(c, handle, by, replace) {
|
|
43
43
|
const onChange = () => {
|
|
44
44
|
c.value = structuredClone(handle.doc());
|
|
45
45
|
};
|
|
46
46
|
handle.on("change", onChange);
|
|
47
47
|
const stop = effect(() => {
|
|
48
48
|
const next = c.value;
|
|
49
|
-
handle.change((d) => reconcile(d, next));
|
|
49
|
+
handle.change((d) => reconcile(d, next, by, replace));
|
|
50
50
|
});
|
|
51
51
|
return () => {
|
|
52
52
|
stop();
|
|
53
53
|
handle.off("change", onChange);
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
-
/**
|
|
57
|
-
|
|
56
|
+
/** Core: a doc-backed cell plus lifecycle. The cell projections layer on top. */
|
|
57
|
+
function connect(handle, opts) {
|
|
58
|
+
const by = opts?.by;
|
|
59
|
+
const replace = opts?.replace;
|
|
58
60
|
const c = cell(structuredClone(handle.doc()), { equals: deepEqual, name: "doc" });
|
|
59
|
-
let unbind = bind(c, handle);
|
|
61
|
+
let unbind = bind(c, handle, by, replace);
|
|
60
62
|
return {
|
|
61
63
|
cell: c,
|
|
62
|
-
store: store(c),
|
|
63
64
|
retarget: next => {
|
|
64
65
|
unbind();
|
|
65
66
|
// Seed the cell from the new doc *before* re-binding, so the cell→doc
|
|
66
67
|
// effect doesn't push the old value into the freshly targeted doc.
|
|
67
68
|
c.value = structuredClone(next.doc());
|
|
68
|
-
unbind = bind(c, next);
|
|
69
|
+
unbind = bind(c, next, by, replace);
|
|
69
70
|
},
|
|
70
71
|
dispose: () => unbind(),
|
|
71
72
|
};
|
|
72
73
|
}
|
|
73
|
-
/**
|
|
74
|
-
export function
|
|
75
|
-
return
|
|
74
|
+
/** Connect a `DocHandle` to a reactive cell, syncing both ways. */
|
|
75
|
+
export function connectCell(handle, opts) {
|
|
76
|
+
return connect(handle, opts);
|
|
77
|
+
}
|
|
78
|
+
/** Connect a `DocHandle` to a deep `store`, syncing both ways. */
|
|
79
|
+
export function connectStore(handle, opts) {
|
|
80
|
+
const { cell: c, retarget, dispose } = connect(handle, opts);
|
|
81
|
+
return { store: store(c), retarget, dispose };
|
|
76
82
|
}
|
|
77
|
-
/**
|
|
78
|
-
export function
|
|
79
|
-
|
|
83
|
+
/** Connect a `DocHandle` to a reactive cell + store, syncing both ways. */
|
|
84
|
+
export function connectDoc(handle, opts) {
|
|
85
|
+
const b = connect(handle, opts);
|
|
86
|
+
return { ...b, store: store(b.cell) };
|
|
80
87
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export type { DocBridge } from "./doc-cell.js";
|
|
2
|
-
export {
|
|
1
|
+
export type { CellBridge, DocBridge, DocLifecycle, DocOptions, StoreBridge } from "./doc-cell.js";
|
|
2
|
+
export { connectCell, connectDoc, connectStore } from "./doc-cell.js";
|
|
3
|
+
export type { By, Replace } from "./reconcile.js";
|
|
3
4
|
export { reconcile } from "./reconcile.js";
|
package/dist/automerge/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// @bireactive/automerge — view an Automerge CRDT through the reactive graph.
|
|
2
2
|
//
|
|
3
3
|
// `connectDoc(handle)` turns a `DocHandle` into a `Writable<Cell<T>>` plus a deep
|
|
4
|
-
// `store`, synced both ways
|
|
5
|
-
//
|
|
6
|
-
// is the apex, every view is a leg.
|
|
7
|
-
// writes merge-friendly; it's exported
|
|
4
|
+
// `store`, synced both ways; `connectCell`/`connectStore` give just one projection.
|
|
5
|
+
// Lens/`store` views off that one cell give you many schemas over a single shared
|
|
6
|
+
// doc with no privileged "primary" — the CRDT is the apex, every view is a leg.
|
|
7
|
+
// `reconcile` is the doc-side diff that keeps writes merge-friendly; it's exported
|
|
8
|
+
// for custom bridges.
|
|
8
9
|
//
|
|
9
10
|
// Automerge is an optional peer dependency: import this entry only when you've
|
|
10
11
|
// installed `@automerge/automerge-repo`.
|
|
11
|
-
export {
|
|
12
|
+
export { connectCell, connectDoc, connectStore } from "./doc-cell.js";
|
|
12
13
|
export { reconcile } from "./reconcile.js";
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
type Any = any;
|
|
2
|
+
/** Stable identity key for a list element; return a primitive. `undefined` (or a
|
|
3
|
+
* collision) on any element makes that list fall back to positional. */
|
|
4
|
+
export type By = (element: unknown) => unknown;
|
|
5
|
+
/** Predicate over an object key (or list index): return `true` to *replace* that
|
|
6
|
+
* field's value wholesale (a scalar assignment → an Automerge `put`) instead of
|
|
7
|
+
* recursively merging into it. Use this for opaque JSON blobs that a downstream
|
|
8
|
+
* bridge can only consume as whole-object puts — e.g. tldraw's `richText`, whose
|
|
9
|
+
* patch applier rejects nested text `splice`s and mis-reads nested `del`s. The
|
|
10
|
+
* value is still only written when it actually differs, so unrelated commits
|
|
11
|
+
* don't churn it. */
|
|
12
|
+
export type Replace = (key: string | number) => boolean;
|
|
2
13
|
/** Minimally mutate the Automerge node `target` (inside `handle.change`) to equal
|
|
3
|
-
* the plain value `next`.
|
|
4
|
-
|
|
14
|
+
* the plain value `next`. Pass `by` for identity-keyed list reconciliation, and
|
|
15
|
+
* `replace` to assign chosen keys wholesale instead of merging into them. */
|
|
16
|
+
export declare function reconcile(target: Any, next: Any, by?: By, replace?: Replace): void;
|
|
5
17
|
export {};
|
|
@@ -9,38 +9,142 @@
|
|
|
9
9
|
// `updateText` for strings (char-level), in-place splices for lists, recursive
|
|
10
10
|
// descent for objects, scalar sets for the rest.
|
|
11
11
|
//
|
|
12
|
-
// List handling is
|
|
13
|
-
//
|
|
14
|
-
// insert
|
|
15
|
-
//
|
|
12
|
+
// List handling is positional by default: element-wise in place, with a tail
|
|
13
|
+
// push/truncate. Correct for edits/appends/truncations, but a reorder or mid
|
|
14
|
+
// insert rewrites every shifted slot's scalars — merge-hostile. Pass `by` for
|
|
15
|
+
// identity-keyed reconciliation (mirrors the `eachBy` lens's `by`): a longest
|
|
16
|
+
// common subsequence keeps shared elements in place and emits minimal keyed
|
|
17
|
+
// splices/inserts for the rest, so reorders and mid-inserts merge cleanly.
|
|
16
18
|
import { updateText } from "@automerge/automerge-repo";
|
|
17
19
|
const isPlainObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
20
|
+
/** Structural equality, used to skip a wholesale `replace` write when the field
|
|
21
|
+
* is already deep-equal (so reconciling an unrelated change doesn't rewrite it).
|
|
22
|
+
* Note `a` may be a live Automerge proxy: its *lists* don't expose indices via
|
|
23
|
+
* `Object.keys`, so arrays are walked by length/index, not key enumeration. */
|
|
24
|
+
function deepEq(a, b) {
|
|
25
|
+
if (Object.is(a, b))
|
|
26
|
+
return true;
|
|
27
|
+
if (a === null || b === null || typeof a !== "object" || typeof b !== "object")
|
|
28
|
+
return false;
|
|
29
|
+
const aArr = Array.isArray(a);
|
|
30
|
+
if (aArr !== Array.isArray(b))
|
|
31
|
+
return false;
|
|
32
|
+
if (aArr) {
|
|
33
|
+
const av = a;
|
|
34
|
+
const bv = b;
|
|
35
|
+
if (av.length !== bv.length)
|
|
36
|
+
return false;
|
|
37
|
+
for (let i = 0; i < av.length; i++)
|
|
38
|
+
if (!deepEq(av[i], bv[i]))
|
|
39
|
+
return false;
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
const ak = Object.keys(a);
|
|
43
|
+
const bk = Object.keys(b);
|
|
44
|
+
if (ak.length !== bk.length)
|
|
45
|
+
return false;
|
|
46
|
+
for (const k of ak) {
|
|
47
|
+
if (!Object.hasOwn(b, k))
|
|
48
|
+
return false;
|
|
49
|
+
if (!deepEq(a[k], b[k]))
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
18
54
|
/** Minimally mutate the Automerge node `target` (inside `handle.change`) to equal
|
|
19
|
-
* the plain value `next`.
|
|
20
|
-
|
|
55
|
+
* the plain value `next`. Pass `by` for identity-keyed list reconciliation, and
|
|
56
|
+
* `replace` to assign chosen keys wholesale instead of merging into them. */
|
|
57
|
+
export function reconcile(target, next, by, replace) {
|
|
58
|
+
const ctx = { by, replace };
|
|
21
59
|
if (Array.isArray(next) && Array.isArray(target))
|
|
22
|
-
reconcileList(target, next);
|
|
60
|
+
reconcileList(target, next, ctx);
|
|
23
61
|
else
|
|
24
|
-
reconcileObject(target, next);
|
|
62
|
+
reconcileObject(target, next, ctx);
|
|
25
63
|
}
|
|
26
|
-
function reconcileObject(target, next) {
|
|
64
|
+
function reconcileObject(target, next, ctx) {
|
|
27
65
|
for (const k of Object.keys(target))
|
|
28
66
|
if (!(k in next))
|
|
29
67
|
delete target[k];
|
|
30
68
|
for (const k of Object.keys(next))
|
|
31
|
-
setKey(target, k, target[k], next[k], false);
|
|
69
|
+
setKey(target, k, target[k], next[k], false, ctx);
|
|
32
70
|
}
|
|
33
|
-
function reconcileList(target, next) {
|
|
71
|
+
function reconcileList(target, next, ctx) {
|
|
72
|
+
if (ctx.by !== undefined && reconcileKeyed(target, next, ctx))
|
|
73
|
+
return;
|
|
34
74
|
const shared = Math.min(target.length, next.length);
|
|
35
75
|
for (let i = 0; i < shared; i++)
|
|
36
|
-
setKey(target, i, target[i], next[i], true);
|
|
76
|
+
setKey(target, i, target[i], next[i], true, ctx);
|
|
37
77
|
if (next.length < target.length)
|
|
38
78
|
target.splice(next.length);
|
|
39
79
|
else
|
|
40
80
|
for (let i = target.length; i < next.length; i++)
|
|
41
81
|
target.push(next[i]);
|
|
42
82
|
}
|
|
43
|
-
|
|
83
|
+
/** Keyed list reconcile via LCS. Returns false (→ positional fallback) when keys
|
|
84
|
+
* aren't total + unique on either side. */
|
|
85
|
+
function reconcileKeyed(target, next, ctx) {
|
|
86
|
+
const by = ctx.by;
|
|
87
|
+
const tKeys = target.map(by);
|
|
88
|
+
const nKeys = next.map(by);
|
|
89
|
+
if (!totalUnique(tKeys) || !totalUnique(nKeys))
|
|
90
|
+
return false;
|
|
91
|
+
const keep = lcs(tKeys, nKeys);
|
|
92
|
+
let i = 0; // cursor into `target`, which mutates as we splice
|
|
93
|
+
for (let n = 0; n < next.length; n++) {
|
|
94
|
+
if (keep.has(nKeys[n])) {
|
|
95
|
+
while (i < target.length && !keep.has(by(target[i])))
|
|
96
|
+
target.splice(i, 1);
|
|
97
|
+
setKey(target, i, target[i], next[n], true, ctx); // same identity → merge edits
|
|
98
|
+
i++;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
target.splice(i, 0, next[n]); // insert (new key, or a moved element re-placed)
|
|
102
|
+
i++;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (i < target.length)
|
|
106
|
+
target.splice(i);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
function totalUnique(keys) {
|
|
110
|
+
if (keys.some(k => k === undefined))
|
|
111
|
+
return false;
|
|
112
|
+
return new Set(keys).size === keys.length;
|
|
113
|
+
}
|
|
114
|
+
/** Keys of the longest common subsequence of `a` and `b` (`===` on keys). */
|
|
115
|
+
function lcs(a, b) {
|
|
116
|
+
const n = a.length;
|
|
117
|
+
const m = b.length;
|
|
118
|
+
const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
119
|
+
for (let i = n - 1; i >= 0; i--)
|
|
120
|
+
for (let j = m - 1; j >= 0; j--)
|
|
121
|
+
dp[i][j] = a[i] === b[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1]);
|
|
122
|
+
const keep = new Set();
|
|
123
|
+
let i = 0;
|
|
124
|
+
let j = 0;
|
|
125
|
+
while (i < n && j < m) {
|
|
126
|
+
if (a[i] === b[j]) {
|
|
127
|
+
keep.add(a[i]);
|
|
128
|
+
i++;
|
|
129
|
+
j++;
|
|
130
|
+
}
|
|
131
|
+
else if (dp[i + 1][j] >= dp[i][j + 1])
|
|
132
|
+
i++;
|
|
133
|
+
else
|
|
134
|
+
j++;
|
|
135
|
+
}
|
|
136
|
+
return keep;
|
|
137
|
+
}
|
|
138
|
+
function setKey(parent, key, a, b, inList, ctx) {
|
|
139
|
+
// Wholesale-replace keys bypass the merge entirely: assign the value so
|
|
140
|
+
// Automerge emits a single `put` of the (re)built subtree — no nested text
|
|
141
|
+
// `splice`s, no `del`s — which is all some bridges can apply. Guard on
|
|
142
|
+
// deep-equality so reconciling an unrelated change doesn't rewrite it.
|
|
143
|
+
if (ctx.replace?.(key)) {
|
|
144
|
+
if (!deepEq(a, b))
|
|
145
|
+
parent[key] = b;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
44
148
|
if (typeof b === "string" && typeof a === "string") {
|
|
45
149
|
// Char-level merge for object text fields; list string elements just assign
|
|
46
150
|
// (path-relative updateText targets a keyed field, not an array slot).
|
|
@@ -52,10 +156,10 @@ function setKey(parent, key, a, b, inList) {
|
|
|
52
156
|
}
|
|
53
157
|
}
|
|
54
158
|
else if (Array.isArray(b) && Array.isArray(a)) {
|
|
55
|
-
reconcileList(a, b);
|
|
159
|
+
reconcileList(a, b, ctx);
|
|
56
160
|
}
|
|
57
161
|
else if (isPlainObject(b) && isPlainObject(a)) {
|
|
58
|
-
reconcileObject(a, b);
|
|
162
|
+
reconcileObject(a, b, ctx);
|
|
59
163
|
}
|
|
60
164
|
else if (a !== b) {
|
|
61
165
|
parent[key] = b;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bireactive",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Bi-directional reactive programming.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -53,13 +53,21 @@
|
|
|
53
53
|
"postversion": "npm publish && git push --follow-tags"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@automerge/automerge-repo": "^2.6.0-subduction.34",
|
|
57
|
-
"@automerge/automerge-repo-network-broadcastchannel": "^2.6.0-subduction.34",
|
|
58
|
-
"@automerge/automerge-repo-storage-indexeddb": "^2.6.0-subduction.34",
|
|
59
56
|
"prism-esm": "^1.29.0-fix.6",
|
|
60
57
|
"temml": "^0.13.3"
|
|
61
58
|
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"@automerge/automerge-repo": "^2.6.0-subduction.34"
|
|
61
|
+
},
|
|
62
|
+
"peerDependenciesMeta": {
|
|
63
|
+
"@automerge/automerge-repo": {
|
|
64
|
+
"optional": true
|
|
65
|
+
}
|
|
66
|
+
},
|
|
62
67
|
"devDependencies": {
|
|
68
|
+
"@automerge/automerge-repo": "^2.6.0-subduction.34",
|
|
69
|
+
"@automerge/automerge-repo-network-broadcastchannel": "^2.6.0-subduction.34",
|
|
70
|
+
"@automerge/automerge-repo-storage-indexeddb": "^2.6.0-subduction.34",
|
|
63
71
|
"@biomejs/biome": "2.4.15",
|
|
64
72
|
"@preact/signals-core": "^1.14.2",
|
|
65
73
|
"@types/node": "^22.0.0",
|