@voltro/local-first 0.53.0 → 0.55.0

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.
@@ -1,72 +1,72 @@
1
1
  import * as e from "yjs";
2
2
  //#region src/crdt/yjsBackend.ts
3
- var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
3
+ var t = Symbol.for("@voltro/local-first:remote-fold"), n = "t", r = (t) => e.encodeStateAsUpdate(t), i = (t) => {
4
4
  let n = new e.Doc();
5
5
  return t.length > 0 && e.applyUpdate(n, t), n;
6
- }, i = (r) => {
7
- let i = r.getText(t);
6
+ }, a = (t) => {
7
+ let i = t.getText(n);
8
8
  return {
9
9
  toString: () => i.toString(),
10
10
  insert: (e, t) => i.insert(e, t),
11
11
  delete: (e, t) => i.delete(e, t),
12
- encodeState: () => n(r),
13
- applyState: (t) => {
14
- t.length > 0 && e.applyUpdate(r, t);
12
+ encodeState: () => r(t),
13
+ applyState: (n) => {
14
+ n.length > 0 && e.applyUpdate(t, n);
15
15
  }
16
16
  };
17
- }, a = {
17
+ }, o = {
18
18
  name: "yjs",
19
- createText: (n) => {
19
+ createText: (t) => {
20
20
  let r = new e.Doc();
21
- return n !== void 0 && n.length > 0 && r.getText(t).insert(0, n), i(r);
21
+ return t !== void 0 && t.length > 0 && r.getText(n).insert(0, t), a(r);
22
22
  },
23
23
  merge: (t, n) => e.mergeUpdates([t, n]),
24
24
  decodeText: (e) => {
25
- let n = r(e), i = n.getText(t).toString();
26
- return n.destroy(), i;
25
+ let t = i(e), r = t.getText(n).toString();
26
+ return t.destroy(), r;
27
27
  },
28
- emptyState: () => n(new e.Doc()),
29
- createDoc: (t) => {
30
- let n = new e.Doc();
31
- return t !== void 0 && t.length > 0 && e.applyUpdate(n, t), {
32
- raw: n,
33
- encodeState: () => e.encodeStateAsUpdate(n),
34
- stateVector: () => e.encodeStateVector(n),
35
- encodeUpdateSince: (t) => e.encodeStateAsUpdate(n, t),
36
- applyState: (t) => {
37
- t.length > 0 && e.applyUpdate(n, t);
28
+ emptyState: () => r(new e.Doc()),
29
+ createDoc: (n) => {
30
+ let r = new e.Doc();
31
+ return n !== void 0 && n.length > 0 && e.applyUpdate(r, n), {
32
+ raw: r,
33
+ encodeState: () => e.encodeStateAsUpdate(r),
34
+ stateVector: () => e.encodeStateVector(r),
35
+ encodeUpdateSince: (t) => e.encodeStateAsUpdate(r, t),
36
+ applyState: (n) => {
37
+ n.length > 0 && e.applyUpdate(r, n, t);
38
38
  },
39
39
  onUpdate: (e) => {
40
- let t = (t) => {
41
- e(t);
40
+ let n = (n, r) => {
41
+ e(n, { local: r !== t });
42
42
  };
43
- return n.on("update", t), () => {
44
- n.off("update", t);
43
+ return r.on("update", n), () => {
44
+ r.off("update", n);
45
45
  };
46
46
  },
47
- encodeAnchor: (t, r) => {
48
- let i = e.createRelativePositionFromTypeIndex(n.getText(t), r);
47
+ encodeAnchor: (t, n) => {
48
+ let i = e.createRelativePositionFromTypeIndex(r.getText(t), n);
49
49
  return e.encodeRelativePosition(i);
50
50
  },
51
51
  resolveAnchor: (t) => {
52
- let r = e.createAbsolutePositionFromRelativePosition(e.decodeRelativePosition(t), n);
53
- return r === null ? void 0 : r.index;
52
+ let n = e.createAbsolutePositionFromRelativePosition(e.decodeRelativePosition(t), r);
53
+ return n === null ? void 0 : n.index;
54
54
  }
55
55
  };
56
56
  },
57
57
  compact: (t) => {
58
- let n = r(t), i = e.encodeStateAsUpdate(n);
59
- return n.destroy(), i;
58
+ let n = i(t), r = e.encodeStateAsUpdate(n);
59
+ return n.destroy(), r;
60
60
  },
61
- rebaseText: (n) => {
62
- let i = r(n), a = i.getText(t).toString();
63
- i.destroy();
61
+ rebaseText: (t) => {
62
+ let r = i(t), a = r.getText(n).toString();
63
+ r.destroy();
64
64
  let o = new e.Doc();
65
- a.length > 0 && o.getText(t).insert(0, a);
65
+ a.length > 0 && o.getText(n).insert(0, a);
66
66
  let s = e.encodeStateAsUpdate(o);
67
67
  return o.destroy(), s;
68
68
  }
69
- }, o = a, s = (e, t = o) => {
69
+ }, s = o, c = (e, t = s) => {
70
70
  let n = t.createText(e), r = {
71
71
  backend: t.name,
72
72
  toString: () => n.toString(),
@@ -76,22 +76,22 @@ var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
76
76
  encode: () => n.encodeState()
77
77
  };
78
78
  return r;
79
- }, c = (e, t, n) => {
79
+ }, l = (e, t, n) => {
80
80
  if (!(n instanceof Uint8Array) || !(t instanceof Uint8Array)) return n;
81
81
  try {
82
- return l(t, n);
82
+ return u(t, n);
83
83
  } catch {
84
84
  return n;
85
85
  }
86
- }, l = (e, t, n = o) => n.merge(e, t), u = (e, t = o) => t.decodeText(e), d = (e = o) => e.emptyState(), f = (e = !0) => ({
86
+ }, u = (e, t, n = s) => n.merge(e, t), d = (e, t = s) => t.decodeText(e), f = (e = s) => e.emptyState(), p = (e = !0) => ({
87
87
  online: e,
88
88
  pending: [],
89
89
  inFlight: void 0,
90
90
  lastError: void 0
91
- }), p = (e) => e.online && e.inFlight === void 0 && e.pending.length > 0, m = (e) => e.pending.length + (e.inFlight === void 0 ? 0 : 1), h = (e) => ({
91
+ }), m = (e) => e.online && e.inFlight === void 0 && e.pending.length > 0, h = (e) => e.pending.length + (e.inFlight === void 0 ? 0 : 1), g = (e) => ({
92
92
  ...e,
93
93
  attempts: e.attempts + 1
94
- }), g = (e, t) => {
94
+ }), _ = (e, t) => {
95
95
  switch (t.type) {
96
96
  case "enqueue": return {
97
97
  ...e,
@@ -111,7 +111,7 @@ var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
111
111
  inFlight: void 0
112
112
  };
113
113
  case "drain-start": {
114
- if (!p(e)) return e;
114
+ if (!m(e)) return e;
115
115
  let [t, ...n] = e.pending;
116
116
  return t === void 0 ? e : {
117
117
  ...e,
@@ -127,15 +127,15 @@ var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
127
127
  };
128
128
  case "fail": return e.inFlight === void 0 || e.inFlight.id !== t.id ? e : {
129
129
  ...e,
130
- pending: [h(e.inFlight), ...e.pending],
130
+ pending: [g(e.inFlight), ...e.pending],
131
131
  inFlight: void 0,
132
132
  lastError: t.error
133
133
  };
134
134
  }
135
- }, _ = (e = "online") => ({
135
+ }, v = (e = "online") => ({
136
136
  phase: e,
137
137
  attempt: 0
138
- }), v = (e, t) => {
138
+ }), y = (e, t) => {
139
139
  switch (t.type) {
140
140
  case "network-down": return e.phase === "offline" ? e : {
141
141
  phase: "offline",
@@ -154,102 +154,102 @@ var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
154
154
  attempt: e.attempt + 1
155
155
  } : e;
156
156
  }
157
- }, y = (e, t) => e === "offline" ? "offline" : e === "reconnecting" || t > 0 ? "syncing" : "synced", b = (e) => `${e.table}\u0000${e.id}\u0000${e.column}`, x = 0, S = () => {
157
+ }, b = (e, t) => e === "offline" ? "offline" : e === "reconnecting" || t > 0 ? "syncing" : "synced", x = (e) => `${e.table}\u0000${e.id}\u0000${e.column}`, S = 0, C = () => {
158
158
  let e = typeof crypto < "u" ? crypto : void 0;
159
- return e && typeof e.randomUUID == "function" ? e.randomUUID() : `w_${Date.now().toString(36)}_${x += 1}`;
160
- }, C = (e) => {
161
- let { transport: t, adapter: n } = e, r = e.backend ?? o, i = e.now ?? Date.now, a = e.maxAttempts ?? 5, s = f(e.online ?? !0), c = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Set(), d = () => {
162
- for (let e of u) try {
159
+ return e && typeof e.randomUUID == "function" ? e.randomUUID() : `w_${Date.now().toString(36)}_${S += 1}`;
160
+ }, w = (e) => {
161
+ let { transport: t, adapter: n } = e, r = e.backend ?? s, i = e.now ?? Date.now, a = e.maxAttempts ?? 5, o = p(e.online ?? !0), c = /* @__PURE__ */ new Map(), l = /* @__PURE__ */ new Set(), d = () => {
162
+ for (let e of l) try {
163
163
  e();
164
164
  } catch {}
165
- }, h = (e, t) => {
166
- let i = b(e), a = c.get(i), o = a === void 0 ? t : l(a, t, r);
165
+ }, f = (e, t) => {
166
+ let i = x(e), a = c.get(i), o = a === void 0 ? t : u(a, t, r);
167
167
  c.set(i, o), n?.saveCrdtState(e.table, e.id, o).catch(() => void 0);
168
- }, _ = () => {
168
+ }, g = () => {
169
169
  if (n === void 0) return;
170
- let e = s.inFlight === void 0 ? s.pending : [s.inFlight, ...s.pending];
170
+ let e = o.inFlight === void 0 ? o.pending : [o.inFlight, ...o.pending];
171
171
  n.saveQueue(e).catch(() => void 0);
172
172
  }, v = !1, y = async () => {
173
173
  if (!v) {
174
174
  v = !0;
175
175
  try {
176
- for (; p(s);) {
177
- s = g(s, { type: "drain-start" });
178
- let e = s.inFlight;
176
+ for (; m(o);) {
177
+ o = _(o, { type: "drain-start" });
178
+ let e = o.inFlight;
179
179
  if (e === void 0) break;
180
180
  try {
181
- await t.push(e), s = g(s, {
181
+ await t.push(e), o = _(o, {
182
182
  type: "ack",
183
183
  id: e.id
184
- }), _(), d();
184
+ }), g(), d();
185
185
  } catch (t) {
186
- if (s = g(s, {
186
+ if (o = _(o, {
187
187
  type: "fail",
188
188
  id: e.id,
189
189
  error: t instanceof Error ? t.message : String(t)
190
- }), _(), d(), e.attempts + 1 >= a) break;
190
+ }), g(), d(), e.attempts + 1 >= a) break;
191
191
  }
192
192
  }
193
193
  } finally {
194
194
  v = !1;
195
195
  }
196
196
  }
197
- }, x = t.onRemoteState((e) => {
198
- h(e, e.state), d();
197
+ }, b = t.onRemoteState((e) => {
198
+ f(e, e.state), d();
199
199
  });
200
200
  return {
201
201
  enqueue: (e) => {
202
- h(e, e.update);
203
- let t = b(e), n = s.pending.findIndex((e) => b(e.payload) === t);
202
+ f(e, e.update);
203
+ let t = x(e), n = o.pending.findIndex((e) => x(e.payload) === t);
204
204
  if (n !== -1) {
205
- let t = s.pending[n], i = {
205
+ let t = o.pending[n], i = {
206
206
  ...t,
207
207
  payload: {
208
208
  ...t.payload,
209
- update: l(t.payload.update, e.update, r)
209
+ update: u(t.payload.update, e.update, r)
210
210
  }
211
211
  };
212
- s = {
213
- ...s,
214
- pending: s.pending.map((e, t) => t === n ? i : e)
215
- }, _(), d(), y();
212
+ o = {
213
+ ...o,
214
+ pending: o.pending.map((e, t) => t === n ? i : e)
215
+ }, g(), d(), y();
216
216
  return;
217
217
  }
218
218
  let a = {
219
- id: S(),
219
+ id: C(),
220
220
  payload: e,
221
221
  enqueuedAt: i(),
222
222
  attempts: 0
223
223
  };
224
- s = g(s, {
224
+ o = _(o, {
225
225
  type: "enqueue",
226
226
  write: a
227
- }), _(), d(), y();
227
+ }), g(), d(), y();
228
228
  },
229
229
  setOnline: (e) => {
230
- s = g(s, e ? { type: "online" } : { type: "offline" }), _(), d(), e && y();
230
+ o = _(o, e ? { type: "online" } : { type: "offline" }), g(), d(), e && y();
231
231
  },
232
- queueState: () => s,
233
- outstanding: () => m(s),
232
+ queueState: () => o,
233
+ outstanding: () => h(o),
234
234
  getState: (e) => {
235
- let t = c.get(b(e));
235
+ let t = c.get(x(e));
236
236
  return t === void 0 ? void 0 : t.slice();
237
237
  },
238
238
  getText: (e) => {
239
- let t = c.get(b(e));
239
+ let t = c.get(x(e));
240
240
  return t === void 0 ? "" : r.decodeText(t);
241
241
  },
242
- subscribe: (e) => (u.add(e), () => {
243
- u.delete(e);
242
+ subscribe: (e) => (l.add(e), () => {
243
+ l.delete(e);
244
244
  }),
245
245
  flush: async () => {
246
246
  await y();
247
247
  },
248
248
  close: () => {
249
- x(), u.clear();
249
+ b(), l.clear();
250
250
  }
251
251
  };
252
- }, w = (e) => {
252
+ }, T = (e) => {
253
253
  let { channel: t, roomId: n, peerId: r, initial: i } = e, a = e.ttlMs ?? 3e4, o = e.now ?? Date.now, s = i, c = 0, l = !1, u = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Set(), p = () => {
254
254
  for (let e of f) try {
255
255
  e();
@@ -310,4 +310,4 @@ var t = "t", n = (t) => e.encodeStateAsUpdate(t), r = (t) => {
310
310
  };
311
311
  };
312
312
  //#endregion
313
- export { _ as a, m as c, s as d, u as f, a as g, l as h, y as i, g as l, d as m, C as n, p as o, o as p, v as r, f as s, w as t, c as u };
313
+ export { v as a, h as c, c as d, d as f, o as g, u as h, b as i, _ as l, f as m, w as n, m as o, s as p, y as r, p as s, T as t, l as u };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@voltro/local-first",
3
- "version": "0.53.0",
4
- "description": "Local-first / CRDT primitives (opt-in): a Yjs-backed crdtText() field and its deterministic merge, the bi-directional sync wire (createSyncClient + the useCrdtText React binding), durable IndexedDB persistence, presence rooms, connection-status state machine, and last-write-wins conflict policy. Remaining seams: the two app-level tags the sync wire is pointed at (the mutation that writes the column, the query that streams the row), the presence channel's broker binding, and an optional wa-sqlite/Turso adapter.",
3
+ "version": "0.55.0",
4
+ "description": "The local-first sync engine (opt-in): Yjs-backed crdtText()/crdtDoc() fields with a deterministic, server-authoritative merge and the Tiptap rich-text binding (useCrdtEditor); a subject+tenant-partitioned query mirror over IndexedDB that renders the last materialised rows offline and delta-resumes on reconnect; a durable offline outbox with declared conflict resolution (resolveWithPolicy) and a per-partition Web-Locks single drainer; the bi-directional sync wire (createSyncClient + useCrdtText); presence rooms over the framework's own presence lane; durable persistence and the connection-status state machine. Pure and browser-safe React bindings behind /react, the editor behind /editor.",
5
5
  "keywords": [
6
6
  "voltro",
7
7
  "typescript",
@@ -28,6 +28,11 @@
28
28
  "import": "./dist/react.js",
29
29
  "default": "./dist/react.js"
30
30
  },
31
+ "./editor": {
32
+ "types": "./dist/editor.d.ts",
33
+ "import": "./dist/editor.js",
34
+ "default": "./dist/editor.js"
35
+ },
31
36
  "./package.json": "./package.json"
32
37
  },
33
38
  "main": "./dist/index.js",