@voltro/client 0.89.1 → 0.90.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.
package/dist/outbox.js CHANGED
@@ -2,211 +2,240 @@ import { recoveryCoordinatorFor as e } from "./recoveryCoordinator.js";
2
2
  import { FrameworkRuntimesContext as t, isUnresolvedApi as n } from "./frameworkRuntimesContext.js";
3
3
  import { resolveByTag as r } from "./tagWalk.js";
4
4
  import { runRpcPromise as i } from "./runRpc.js";
5
- import { awaitResolvedApi as a } from "./awaitResolvedApi.js";
6
- import { withIdempotencyKey as o } from "./mutationIdentity.js";
7
- import { useApiOnline as s } from "./useApiOnline.js";
8
- import { OutboxState as c } from "./outboxState.js";
9
- import { useCallback as l, useContext as u, useEffect as d, useMemo as f, useRef as p, useState as m } from "react";
5
+ import { awaitResolvedApi as ee } from "./awaitResolvedApi.js";
6
+ import { withIdempotencyKey as te } from "./mutationIdentity.js";
7
+ import { useApiOnline as a } from "./useApiOnline.js";
8
+ import { OutboxState as o } from "./outboxState.js";
9
+ import { useCallback as s, useContext as c, useEffect as l, useMemo as ne, useRef as u, useState as d } from "react";
10
10
  // src/outbox.ts
11
- var h = (e, t, n, r, i) => [...e, {
11
+ var f = (e, t, n, r, i) => [...e, {
12
12
  id: t,
13
13
  tag: n,
14
14
  input: r,
15
15
  status: "pending",
16
16
  attempts: 0,
17
17
  ...i === void 0 ? {} : { stamp: i }
18
- }], g = (e, t, n) => e.map((e) => e.id === t ? {
18
+ }], p = (e, t, n) => e.map((e) => e.id === t ? {
19
19
  ...e,
20
20
  ...n
21
- } : e), _ = (e, t) => g(e, t, { status: "sent" }), v = (e, t, n) => e.map((e) => e.id === t ? {
21
+ } : e), m = (e, t) => p(e, t, { status: "sent" }), h = (e, t, n) => e.map((e) => e.id === t ? {
22
22
  ...e,
23
23
  status: "failed",
24
24
  error: n,
25
25
  attempts: e.attempts + 1
26
- } : e), y = (e, t, n) => g(e, t, {
26
+ } : e), g = (e, t, n) => p(e, t, {
27
27
  status: "conflict",
28
28
  error: n
29
- }), b = (e, t, n) => e.map((e) => e.id === t && e.status === "conflict" ? {
29
+ }), _ = (e, t, n) => e.map((e) => e.id === t && e.status === "conflict" ? {
30
30
  ...e,
31
31
  input: n,
32
32
  status: "pending",
33
33
  error: void 0
34
- } : e), x = (e) => e.filter((e) => e.status !== "sent"), S = (e) => {
34
+ } : e), v = (e) => e.filter((e) => e.status !== "sent"), y = (e) => {
35
35
  let t = [];
36
36
  for (let n of e) {
37
37
  if (n.status === "conflict") break;
38
38
  (n.status === "pending" || n.status === "failed") && t.push(n);
39
39
  }
40
40
  return t;
41
- }, ee = (e) => {
41
+ }, b = (e) => {
42
42
  let t = e;
43
43
  return t?.conflict === !0 || t?._tag === "UndoBoundaryError" && t?.boundary === "conflict";
44
- }, C = (e) => {
44
+ }, x = (e) => {
45
45
  let t = Math.min(3e4, 1e3 * 2 ** Math.max(0, e - 1));
46
46
  return t / 2 + t / 2 * Math.random();
47
- }, w = (h) => {
48
- let { persistence: g, isConflict: _ = ee, idFor: v, api: y } = h;
49
- if (h.send === void 0 && y === void 0) throw Error("useOutbox: pass `send`, or `api` to send through that api's own mutations.");
50
- let b = u(t), S = b?.demand;
51
- d(() => {
52
- y !== void 0 && S?.(y);
53
- }, [S, y]);
54
- let w = y === void 0 || b === null ? void 0 : b.get(y), T = p(w);
55
- T.current = w;
56
- let [E, D] = m(0), [O, k] = m(0), A = s(y), j = h.online ?? A, M = p(void 0);
57
- M.current === void 0 && (M.current = new c());
58
- let N = f(() => M.current.snapshot(), [E]), P = l((e) => {
59
- e(M.current), D((e) => e + 1);
60
- }, []), F = p(h.retryDelay ?? C);
61
- F.current = h.retryDelay ?? C;
62
- let I = p(h.stamp);
63
- I.current = h.stamp;
64
- let L = p(async () => void 0);
65
- L.current = h.send ?? (async (e) => {
66
- if (T.current === void 0) throw Error(`useOutbox: no mounted api '${y}' — render it inside the framework's runtime provider.`);
67
- let t = await a(T, y, e.tag), n = r(t.client, e.tag);
68
- await i(t.runtime, o(n(e.input), e.id));
47
+ }, S = (f) => {
48
+ let { persistence: p, isConflict: m = b, idFor: h, api: g } = f;
49
+ if (f.send === void 0 && g === void 0) throw Error("useOutbox: pass `send`, or `api` to send through that api's own mutations.");
50
+ let _ = c(t), y = _?.demand;
51
+ l(() => {
52
+ g !== void 0 && y?.(g);
53
+ }, [y, g]);
54
+ let S = g === void 0 || _ === null ? void 0 : _.get(g), C = u(S);
55
+ C.current = S;
56
+ let [re, w] = d(0), [T, E] = d(0), D = a(g), O = f.online ?? D, k = u(void 0), A = u(0);
57
+ k.current === void 0 && (k.current = new o());
58
+ let j = ne(() => k.current.snapshot(), [re]), M = s((e) => {
59
+ e(k.current), w((e) => e + 1);
60
+ }, []), N = u(f.retryDelay ?? x);
61
+ N.current = f.retryDelay ?? x;
62
+ let P = u(f.stamp);
63
+ P.current = f.stamp;
64
+ let F = u(async () => void 0);
65
+ F.current = f.send ?? (async (t) => {
66
+ if (C.current === void 0) throw Error(`useOutbox: no mounted api '${g}' — render it inside the framework's runtime provider.`);
67
+ let n = C.current, a = n.subjectSignal ?? (n.recovery ?? e(n.errorBus, g)).subjectSignal, o = await ee(C, g, t.tag, a);
68
+ if (a?.throwIfAborted(), n.recovery !== void 0 && n.recovery !== o.recovery) throw Error("Outbox API ownership changed before the queued write started");
69
+ let s = r(o.client, t.tag);
70
+ await i(o.runtime, te(s(t.input), t.id), { signal: a });
69
71
  });
70
- let R = p(_);
71
- R.current = _;
72
- let z = p(void 0), B = p(!1), V = p(!0);
73
- d(() => (V.current = !0, () => {
74
- V.current = !1;
72
+ let I = u(m);
73
+ I.current = m;
74
+ let L = u(void 0), R = u(!1), z = u(!0);
75
+ l(() => (z.current = !0, () => {
76
+ z.current = !1;
75
77
  }), []);
76
- let H = p(0), U = p(Math.random().toString(36).slice(2, 8)), W = p(g === void 0), [G, K] = m(0), q = w === void 0 ? void 0 : w.recovery ?? e(w.errorBus, y);
77
- d(() => {
78
- if (q === void 0) return;
79
- let e = q.subjectSignal;
78
+ let B = u(0), V = u(Math.random().toString(36).slice(2, 8)), H = u(p === void 0), [U, W] = d(0), G = S === void 0 ? void 0 : S.recovery ?? e(S.errorBus, g), K = G?.subjectSignal;
79
+ l(() => {
80
+ if (G === void 0) return;
81
+ let e = G.subjectSignal;
80
82
  if (e.aborted) return;
81
83
  let t = () => {
82
- M.current.reset(), D((e) => e + 1), W.current = g === void 0, B.current = !1, K((e) => e + 1);
84
+ A.current++, L.current = void 0, Z(), k.current.reset(), w((e) => e + 1), H.current = p === void 0, R.current = !1, W((e) => e + 1);
83
85
  };
84
86
  return e.addEventListener("abort", t, { once: !0 }), () => {
85
87
  e.removeEventListener("abort", t);
86
88
  };
87
89
  }, [
88
- q,
89
- g,
90
- G
91
- ]), d(() => {
92
- if (g === void 0) return;
90
+ G,
91
+ p,
92
+ U
93
+ ]), l(() => {
94
+ if (p === void 0) return;
93
95
  let e = !1;
94
- return W.current = !1, g.load().then((t) => {
95
- e || (W.current = !0, t.length > 0 && P((e) => e.prepend(t)), k((e) => e + 1));
96
+ return H.current = !1, p.load().then((t) => {
97
+ e || (H.current = !0, t.length > 0 && M((e) => e.prepend(t.map((e) => {
98
+ if (e.status !== "conflict" || !(e.error instanceof Error)) return e;
99
+ let t = !1;
100
+ try {
101
+ t = I.current(e.error);
102
+ } catch {}
103
+ return t ? e : {
104
+ ...e,
105
+ status: "failed"
106
+ };
107
+ }))), E((e) => e + 1));
96
108
  }, () => {
97
- e || (W.current = !0, k((e) => e + 1));
109
+ e || (H.current = !0, E((e) => e + 1));
98
110
  }), () => {
99
111
  e = !0;
100
112
  };
101
113
  }, [
102
- g,
103
- P,
104
- G
105
- ]), d(() => {
106
- g !== void 0 && W.current && g.save(x(N)).catch(() => {});
114
+ p,
115
+ M,
116
+ U
117
+ ]), l(() => {
118
+ p !== void 0 && H.current && p.save(v(j)).catch(() => {});
107
119
  }, [
108
- g,
109
- N,
110
- O
120
+ p,
121
+ j,
122
+ T
111
123
  ]);
112
- let J = p(/* @__PURE__ */ new Set()), Y = w === void 0 || n(w) ? void 0 : w.cache;
113
- d(() => {
114
- if (Y !== void 0 && w !== void 0) for (let e of N) {
115
- let t = w.descriptors[e.tag]?.targets;
124
+ let q = u(/* @__PURE__ */ new Set()), J = S === void 0 || n(S) ? void 0 : S.cache;
125
+ l(() => {
126
+ if (J !== void 0 && S !== void 0) for (let e of j) {
127
+ let t = S.descriptors[e.tag]?.targets;
116
128
  if (e.status === "pending" || e.status === "failed") {
117
- if (J.current.delete(e.id), t === void 0 || t.length === 0 || Y.hasIntent(e.id) || typeof e.input != "object" || e.input === null) continue;
118
- Y.autoApply(t, e.input, e.id, { standing: !0 });
119
- } else J.current.has(e.id) || (J.current.add(e.id), e.status === "sent" ? Y.confirmByMutation(e.id) : Y.revertByMutation(e.id));
129
+ if (q.current.delete(e.id), t === void 0 || t.length === 0 || J.hasIntent(e.id) || typeof e.input != "object" || e.input === null) continue;
130
+ J.autoApply(t, e.input, e.id, { standing: !0 });
131
+ } else q.current.has(e.id) || (q.current.add(e.id), e.status === "sent" ? J.confirmByMutation(e.id) : J.revertByMutation(e.id));
120
132
  }
121
133
  }, [
122
- Y,
123
- w,
124
- N
134
+ J,
135
+ S,
136
+ j
125
137
  ]);
126
- let te = l((e, t) => {
127
- let n = v?.() ?? `obx_${U.current}_${H.current++}_${e}`;
128
- P((r) => r.append({
129
- id: n,
138
+ let Y = s((e, t, n) => {
139
+ if (!z.current || K?.aborted || S !== C.current) throw Error("Outbox ownership changed before enqueue");
140
+ let r = n?.stamp ?? P.current?.();
141
+ if (r !== void 0 && typeof r != "string") throw TypeError("Outbox stamp must be a string");
142
+ let i = h?.() ?? `obx_${V.current}_${B.current++}_${e}`;
143
+ M((n) => n.append({
144
+ id: i,
130
145
  tag: e,
131
146
  input: t,
132
147
  status: "pending",
133
148
  attempts: 0,
134
- ...I.current === void 0 ? {} : { stamp: I.current() }
135
- })), k((e) => e + 1);
136
- }, [v, P]), X = p(void 0), Z = l(() => {
149
+ ...r === void 0 ? {} : { stamp: r }
150
+ })), E((e) => e + 1);
151
+ }, [
152
+ h,
153
+ M,
154
+ K,
155
+ S
156
+ ]), X = u(void 0), Z = s(() => {
137
157
  X.current !== void 0 && clearTimeout(X.current), X.current = void 0;
138
158
  }, []);
139
- d(() => Z, [Z]);
140
- let Q = l(() => {
141
- if (!W.current || !V.current) return Promise.resolve();
142
- if (z.current !== void 0) return z.current;
143
- B.current = !1, Z();
144
- let e = Promise.resolve().then(async () => {
145
- for (; V.current;) {
146
- let e = M.current.next();
147
- if (e === void 0) break;
159
+ l(() => Z, [Z]);
160
+ let Q = s(() => {
161
+ if (!H.current || !z.current) return Promise.resolve();
162
+ if (L.current !== void 0) return L.current;
163
+ R.current = !1, Z();
164
+ let e = A.current, t = Promise.resolve().then(async () => {
165
+ for (; z.current && A.current === e;) {
166
+ let t = k.current.next();
167
+ if (t === void 0) break;
148
168
  try {
149
- if (await L.current(e), !V.current) break;
150
- P((t) => t.update(e.id, (e) => ({
169
+ if (await F.current(t), !z.current || A.current !== e) break;
170
+ M((e) => e.update(t.id, (e) => ({
151
171
  ...e,
152
172
  status: "sent"
153
173
  })));
154
- } catch (t) {
155
- if (!V.current) break;
156
- let n = R.current(t);
157
- P((r) => r.update(e.id, (e) => n ? {
174
+ } catch (n) {
175
+ if (!z.current || A.current !== e) break;
176
+ let r = I.current(n);
177
+ M((e) => e.update(t.id, (e) => r ? {
158
178
  ...e,
159
179
  status: "conflict",
160
- error: t
180
+ error: n
161
181
  } : {
162
182
  ...e,
163
183
  status: "failed",
164
- error: t,
184
+ error: n,
165
185
  attempts: e.attempts + 1
166
- })), B.current = !0, n || (X.current = setTimeout(() => {
186
+ })), R.current = !0, r || (X.current = setTimeout(() => {
167
187
  X.current = void 0, $.current();
168
- }, F.current(e.attempts + 1)));
188
+ }, N.current(t.attempts + 1)));
169
189
  break;
170
190
  }
171
191
  }
172
192
  }).finally(() => {
173
- z.current === e && (z.current = void 0);
193
+ L.current === t && (L.current = void 0);
174
194
  });
175
- return z.current = e, e;
176
- }, [P, Z]), $ = p(Q);
177
- $.current = Q, d(() => {
178
- j ? Q() : Z();
195
+ return L.current = t, t;
196
+ }, [M, Z]), $ = u(Q);
197
+ $.current = Q, l(() => {
198
+ O ? Q() : Z();
179
199
  }, [
180
- j,
200
+ O,
181
201
  Q,
182
202
  Z
183
- ]), d(() => {
184
- j && !B.current && Q();
203
+ ]), l(() => {
204
+ O && !R.current && Q();
185
205
  }, [
206
+ T,
186
207
  O,
187
- j,
188
208
  Q
189
209
  ]);
190
- let ne = l((e, t) => {
191
- P((n) => n.update(e, (e) => e.status === "conflict" ? {
210
+ let ie = s((e, t) => {
211
+ z.current && !K?.aborted && S === C.current && (M((n) => n.update(e, (e) => e.status === "conflict" ? {
192
212
  ...e,
193
213
  input: t,
194
214
  status: "pending",
195
215
  error: void 0
196
- } : e)), j && Q();
216
+ } : e)), O && Q());
197
217
  }, [
198
- j,
218
+ O,
219
+ Q,
220
+ M,
221
+ K,
222
+ S
223
+ ]), ae = s((e) => !z.current || K?.aborted || S !== C.current || !k.current.snapshot().some((t) => t.id === e && t.status === "conflict") || (J?.revertByMutation(e), !z.current || K?.aborted || S !== C.current) || !k.current.discardConflict(e) ? !1 : (q.current.delete(e), R.current = !1, w((e) => e + 1), E((e) => e + 1), O && Q(), !0), [
224
+ O,
199
225
  Q,
200
- P
226
+ J,
227
+ K,
228
+ S
201
229
  ]);
202
230
  return {
203
- queue: N,
204
- enqueue: te,
231
+ queue: j,
232
+ enqueue: Y,
205
233
  replay: Q,
206
- online: j,
207
- pending: N.reduce((e, t) => e + +(t.status === "pending" || t.status === "failed"), 0),
208
- conflicts: N.filter((e) => e.status === "conflict"),
209
- resolveConflict: ne
234
+ online: O,
235
+ pending: j.reduce((e, t) => e + +(t.status === "pending" || t.status === "failed"), 0),
236
+ conflicts: j.filter((e) => e.status === "conflict"),
237
+ resolveConflict: ie,
238
+ discardConflict: ae
210
239
  };
211
240
  };
212
- export { x as clearSent, C as defaultOutboxRetryDelay, h as enqueueEntry, y as markConflict, v as markFailed, _ as markSent, S as replayable, b as resolveConflictEntry, w as useOutbox };
241
+ export { v as clearSent, x as defaultOutboxRetryDelay, f as enqueueEntry, g as markConflict, h as markFailed, m as markSent, y as replayable, _ as resolveConflictEntry, S as useOutbox };
@@ -30,6 +30,13 @@ var e = class {
30
30
  e !== r && (this.entries[n] = r, r.status !== "sent" && n < this.cursor && (this.cursor = n), this.cached = void 0);
31
31
  }
32
32
  }
33
+ discardConflict(e) {
34
+ if (!(this.positions.get(e) ?? []).some((e) => this.entries[e].status === "conflict")) return !1;
35
+ let t = this.entries.filter((t) => t.id !== e || t.status !== "conflict");
36
+ this.reset();
37
+ for (let e of t) this.append(e);
38
+ return !0;
39
+ }
33
40
  next() {
34
41
  for (; this.cursor < this.entries.length;) {
35
42
  let e = this.entries[this.cursor];
package/dist/query.js CHANGED
@@ -1,11 +1,4 @@
1
- import { resolveByTag as e } from "./tagWalk.js";
2
- import { runRpcPromise as t } from "./runRpc.js";
3
- import { awaitResolvedApi as n } from "./awaitResolvedApi.js";
4
- import { Effect as r, Option as i, Stream as a } from "effect";
5
- import { firstEventValue as o } from "@voltro/protocol";
1
+ import { rpcCallOnce as e } from "./rpcCallOnce.js";
6
2
  // src/queryOnce.ts
7
- var s = async (s, c, l, u, d) => {
8
- let { runtime: f, client: p } = await n(s, c, l), m = e(p, l)(u ?? {}), h = r.isEffect(m) ? m : a.runHead(m).pipe(r.map(i.getOrUndefined)), g = await t(f, h, { signal: d });
9
- return o(l, g, "[loader.query]");
10
- };
11
- export { s as queryOnce };
3
+ var t = async (t, n, r, i, a) => e(t, n, r, i ?? {}, a, "[loader.query]");
4
+ export { t as queryOnce };
@@ -0,0 +1,30 @@
1
+ import { resolveByTag as e } from "./tagWalk.js";
2
+ import { runRpcPromise as t } from "./runRpc.js";
3
+ import { ConnectionPrerequisiteInvalidated as n } from "./connectionPrerequisite.js";
4
+ import { awaitResolvedApi as r, bindResolvedRpcInvocationOwner as i, rpcInvocationOwnerCurrent as a } from "./awaitResolvedApi.js";
5
+ import { Effect as o, Option as s, Stream as c } from "effect";
6
+ import { firstEventValue as l } from "@voltro/protocol";
7
+ // src/rpcCallOnce.ts
8
+ var u = async (u, d, f, p, m, h, g) => {
9
+ let _ = () => {
10
+ if (g && !a(g, u.current)) throw new n("inactive");
11
+ };
12
+ _();
13
+ let v = g?.subject, y = await r(u, d, f, m);
14
+ if (v?.aborted) throw new n("inactive");
15
+ g && i(g, y), _();
16
+ let { runtime: b, client: x } = y;
17
+ m?.throwIfAborted();
18
+ let S = g ? new AbortController() : void 0, C = g ? [.../* @__PURE__ */ new Set([g.subject, ...m ? [m] : []])] : [], w = () => S?.abort();
19
+ for (let e of C) e.aborted ? w() : e.addEventListener("abort", w, { once: !0 });
20
+ try {
21
+ _();
22
+ let n = e(x, f)(p), r = o.isEffect(n), i = r ? n : c.runHead(n).pipe(o.flatMap((e) => s.isNone(e) ? o.fail(/* @__PURE__ */ Error(`${h} ${f}: the query ended before its first snapshot`)) : o.succeed(e.value))), a = await t(b, o.suspend(() => (_(), i)), { signal: S?.signal ?? m });
23
+ return _(), r ? a : l(f, a, h);
24
+ } catch (e) {
25
+ throw _(), e;
26
+ } finally {
27
+ for (let e of C) e.removeEventListener("abort", w);
28
+ }
29
+ };
30
+ export { u as rpcCallOnce };
@@ -0,0 +1,7 @@
1
+ import { useFrameworkApi as e } from "./runtimeContext.js";
2
+ // src/rpcDeclared.ts
3
+ var t = (t, n) => {
4
+ let { descriptors: r } = e(t);
5
+ return Object.hasOwn(r, n) ? !0 : Object.keys(r).length === 0 && void 0;
6
+ };
7
+ export { t as useRpcDeclared };
package/dist/runRpc.js CHANGED
@@ -1,11 +1,14 @@
1
1
  import { squashRpcCause as e } from "./squashRpcCause.js";
2
2
  import { rememberRpcErrorKind as t, rpcErrorKind as n } from "./rpcErrorKind.js";
3
- import { Cause as r, Exit as i, Option as a } from "effect";
3
+ import { Cause as r, Effect as i, Exit as a, Option as o } from "effect";
4
4
  // src/runRpc.ts
5
- var o = async (o, s, c) => {
6
- let l = await o.runPromiseExit(s, c?.signal === void 0 ? void 0 : { signal: c.signal });
7
- if (i.isSuccess(l)) return l.value;
8
- let u = e(l.cause), d = r.findErrorOption(l.cause), f = a.isSome(d) ? n(d.value, "handler") : "unknown";
5
+ var s = (e) => i.try({
6
+ try: e,
7
+ catch: (e) => t(e, n(e, "client"))
8
+ }).pipe(i.flatten), c = async (i, s, c) => {
9
+ let l = await i.runPromiseExit(s, c?.signal === void 0 ? void 0 : { signal: c.signal });
10
+ if (a.isSuccess(l)) return l.value;
11
+ let u = e(l.cause), d = r.findErrorOption(l.cause), f = o.isSome(d) ? n(d.value, "handler") : "unknown";
9
12
  throw t(u, f);
10
13
  };
11
- export { o as runRpcPromise };
14
+ export { s as deferRpcMethod, c as runRpcPromise };