@voltro/client 0.89.2 → 0.91.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +1943 -0
  2. package/README.md +22 -0
  3. package/THIRD-PARTY-NOTICES.md +1 -1
  4. package/dist/analytics.d.ts +68 -0
  5. package/dist/analytics.js +1 -0
  6. package/dist/apiRuntime.js +50 -50
  7. package/dist/apiSupervisor.js +25 -25
  8. package/dist/awaitResolvedApi.js +42 -16
  9. package/dist/bootstrap.d.ts +0 -115
  10. package/dist/bootstrap.js +8 -8
  11. package/dist/connectionPrerequisite.js +18 -18
  12. package/dist/connectionSetupController.js +123 -91
  13. package/dist/createHooks.js +11 -11
  14. package/dist/form.js +7 -7
  15. package/dist/index.d.ts +148 -162
  16. package/dist/index.js +88 -86
  17. package/dist/invocationWait.js +14 -0
  18. package/dist/mutationsBus.js +92 -35
  19. package/dist/outbox.js +137 -108
  20. package/dist/outboxState.js +7 -0
  21. package/dist/platform.js +10 -10
  22. package/dist/query.js +3 -10
  23. package/dist/rpcCallOnce.js +30 -0
  24. package/dist/rpcDeclared.js +7 -0
  25. package/dist/runRpc.js +9 -6
  26. package/dist/runtime.d.ts +16 -116
  27. package/dist/runtime.js +16 -16
  28. package/dist/runtimeContext.js +1 -1
  29. package/dist/serviceClient.js +23 -23
  30. package/dist/store.js +3 -3
  31. package/dist/subscriptionCache.js +1 -1
  32. package/dist/traceBus.js +7 -1
  33. package/dist/unknownRpcTag.js +7 -0
  34. package/dist/uploadBytes.js +83 -0
  35. package/dist/uploadClient.js +153 -0
  36. package/dist/useAction.js +148 -74
  37. package/dist/useAgentChat.js +3 -3
  38. package/dist/useCapabilityManifest.js +7 -7
  39. package/dist/useConnectionStatus.js +5 -5
  40. package/dist/useDataTable.js +4 -4
  41. package/dist/useEvent.js +22 -22
  42. package/dist/useFormBinding.js +10 -10
  43. package/dist/useMutation.js +221 -134
  44. package/dist/useOnRpcError.js +5 -5
  45. package/dist/usePreloadedSubscription.js +6 -6
  46. package/dist/useRpcCaller.js +75 -13
  47. package/dist/useRpcInvocationOwner.js +29 -0
  48. package/dist/useSubscription.js +52 -49
  49. package/dist/useTranscription.js +112 -40
  50. package/dist/useUpload.js +160 -242
  51. package/dist/useWorkflow.js +111 -246
  52. package/dist/workflowClient.js +126 -0
  53. package/package.json +8 -3
@@ -1,24 +1,30 @@
1
- import { publishClientTrace as e } from "./traceBus.js";
2
- import { rpcErrorKind as t } from "./rpcErrorKind.js";
3
- import { recoveryCoordinatorFor as n } from "./recoveryCoordinator.js";
4
- import { useFrameworkApi as r } from "./runtimeContext.js";
5
- import { connectionScopeForRuntime as i } from "./connectionScope.js";
6
- import { resolveByTag as a } from "./tagWalk.js";
7
- import { runRpcPromise as o } from "./runRpc.js";
8
- import { awaitResolvedApi as s } from "./awaitResolvedApi.js";
9
- import { settleMutation as c, startMutation as l } from "./mutationsBus.js";
10
- import { bindMutationConnectionSetup as u, captureMutationConnectionSetup as d, connectionSetupOwner as f } from "./mutationConnectionSetup.js";
11
- import { freshIdempotencyKey as p, withIdempotencyKey as m } from "./mutationIdentity.js";
12
- import { MutationSerialExecutionError as h, makeMutationSerialExecution as g } from "./mutationSerialExecution.js";
13
- import { useCallback as _, useMemo as v, useRef as y, useState as b } from "react";
14
- import { Effect as x } from "effect";
1
+ import { useFrameworkApi as e } from "./runtimeContext.js";
2
+ import { deferRpcMethod as t, runRpcPromise as n } from "./runRpc.js";
3
+ import { resolveByTag as r } from "./tagWalk.js";
4
+ import { omitMutationInputs as i, omitMutationOutputs as a, startMutation as o } from "./mutationsBus.js";
5
+ import { publishClientTrace as s, publishClientTraceRefusal as c } from "./traceBus.js";
6
+ import { awaitResolvedApi as l, bindResolvedRpcInvocationOwner as u } from "./awaitResolvedApi.js";
7
+ import { useRpcInvocationOwner as d } from "./useRpcInvocationOwner.js";
8
+ import { ConnectionPrerequisiteInvalidated as f } from "./connectionPrerequisite.js";
9
+ import { rpcErrorKind as p } from "./rpcErrorKind.js";
10
+ import { reportUnknownRpcTag as m } from "./unknownRpcTag.js";
11
+ import { recoveryCoordinatorFor as h } from "./recoveryCoordinator.js";
12
+ import { bindMutationConnectionSetup as g, captureMutationConnectionSetup as ee, connectionSetupOwner as _ } from "./mutationConnectionSetup.js";
13
+ import { freshIdempotencyKey as v, withIdempotencyKey as y } from "./mutationIdentity.js";
14
+ import { connectionScopeForRuntime as te } from "./connectionScope.js";
15
+ import { MutationSerialExecutionError as b, makeMutationSerialExecution as x } from "./mutationSerialExecution.js";
16
+ import { useCallback as S, useEffect as C, useMemo as w, useRef as T, useState as E } from "react";
17
+ import { Effect as D } from "effect";
15
18
  // src/useMutation.ts
16
- var S = /* @__PURE__ */ new WeakMap(), C = (e, t, n) => {
17
- let r = S.get(e);
18
- r === void 0 && (r = /* @__PURE__ */ new Map(), S.set(e, r));
19
+ var O = /* @__PURE__ */ new WeakMap(), ne = (e, t, n) => {
20
+ let r = O.get(e);
21
+ r === void 0 && (r = /* @__PURE__ */ new Map(), O.set(e, r));
19
22
  let i = r.get(t);
20
- return i === void 0 && (i = g(n), r.set(t, i)), i;
21
- }, w = (e, t) => {
23
+ return i === void 0 && (i = x(n), r.set(t, i)), i;
24
+ }, re = (e) => {
25
+ let t = e;
26
+ return t?._tag === "MutationOutputNotRetained" && t.receiptStatus === 200;
27
+ }, k = (e, t) => {
22
28
  if (t === void 0 || typeof e != "object" || !e) return e;
23
29
  try {
24
30
  Object.defineProperty(e, "__voltroTraceId", {
@@ -28,186 +34,267 @@ var S = /* @__PURE__ */ new WeakMap(), C = (e, t, n) => {
28
34
  });
29
35
  } catch {}
30
36
  return e;
31
- }, T, E = (e) => {
32
- T = e;
33
- }, D = () => T, O = (e, t, n) => typeof e == "function" ? e(t, n) : e, k = 0, A = () => `mut_${++k}`, j = (e, t) => ({
37
+ }, A, j = (e) => {
38
+ A = e;
39
+ }, M = () => A, N = (e, t, n) => typeof e == "function" ? e(t, n) : e, P = 0, ie = () => `mut_${++P}`, ae = (e, t) => ({
34
40
  update: (n, r) => {
35
41
  e.update(n, r, t);
36
42
  },
37
43
  forTag: (n, r) => e.forTag(n, r, t)
38
- }), M = (h, g) => {
39
- let S = r(h), { descriptors: E } = S, D = y(S);
40
- D.current = S;
41
- let k = E[g], M = k?.targets;
44
+ }), F = (b, x) => {
45
+ let O = e(b), { descriptors: j } = O, M = T(O);
46
+ M.current = O;
47
+ let { owner: P, current: F, assertCurrent: I, assertOwnerCurrent: L } = d(M, b, x), R = j[x], z = R?.outputRetention === "none", B = R?.clientHistory?.input === "omit", V = R?.targets;
42
48
  if (process.env.NODE_ENV !== "production") {
43
- let e = k?.kind;
44
- e !== void 0 && e !== "mutation" && console.error(`[@voltro/client] useMutation('${h}', '${g}') targets a ${e}, not a mutation. Use ${e === "action" ? "useAction" : e === "workflow" ? "useWorkflow" : `the ${e} hook`} instead.`);
49
+ m("useMutation", b, x, j);
50
+ let e = R?.kind;
51
+ e !== void 0 && e !== "mutation" && console.error(`[@voltro/client] useMutation('${b}', '${x}') targets a ${e}, not a mutation. Use ${e === "action" ? "useAction" : e === "workflow" ? "useWorkflow" : `the ${e} hook`} instead.`);
45
52
  }
46
- let [N, P] = b(!1), [F, I] = b(void 0), [L, R] = b(void 0), z = y(null), B = y(!1), V = y(0), H = _(async (r, u) => {
47
- let f = u?.onError !== void 0 || u?.notify?.error !== void 0, _ = (e) => {
48
- u?.onError?.(e, r);
49
- let t = O(u?.notify?.error, e, r);
50
- t !== void 0 && T?.error(t);
53
+ let [H, U] = E(() => ({
54
+ owner: P,
55
+ pending: !1,
56
+ error: void 0,
57
+ data: void 0
58
+ }));
59
+ C(() => {
60
+ z && (U((e) => e.data === void 0 ? e : {
61
+ ...e,
62
+ data: void 0
63
+ }), a(b, x));
64
+ }, [
65
+ z,
66
+ b,
67
+ x
68
+ ]), C(() => {
69
+ B && i(b, x);
70
+ }, [
71
+ B,
72
+ b,
73
+ x
74
+ ]);
75
+ let W = T(null), G = T(!1), K = T({
76
+ owner: P,
77
+ count: 0
78
+ });
79
+ K.current.owner !== P && (K.current = {
80
+ owner: P,
81
+ count: 0
82
+ }, W.current = null, G.current = !1);
83
+ let q = S(async (e, i) => {
84
+ I();
85
+ let a = P.subject, d = K.current, m = i?.onError !== void 0 || i?.notify?.error !== void 0, g = (t) => {
86
+ I(), i?.onError?.(t, e), I();
87
+ let n = N(i?.notify?.error, t, e);
88
+ I(), n !== void 0 && A?.error(n);
51
89
  };
52
- V.current += 1;
53
- let v = () => P(--V.current > 0);
54
- P(!0), I(void 0);
55
- let y, b, S, E, k;
90
+ d.count += 1;
91
+ let _ = () => {
92
+ --d.count, F() && U((e) => e.owner === P ? {
93
+ ...e,
94
+ pending: d.count > 0
95
+ } : e);
96
+ };
97
+ U((e) => ({
98
+ owner: P,
99
+ pending: !0,
100
+ error: void 0,
101
+ data: e.owner === P ? e.data : void 0
102
+ }));
103
+ let S, C, w, T, E;
56
104
  try {
57
- E = d(H);
58
- let e = D.current.descriptors[g]?.serialize;
59
- if (e !== void 0) {
60
- let t = D.current, i = t.recovery ?? n(t.errorBus, h);
61
- k = C(t.descriptors, g, e.maxPending ?? 32).reserve(e.key(r), i.subjectSignal);
105
+ T = ee(q);
106
+ let t = M.current.descriptors[x]?.serialize;
107
+ if (t !== void 0) {
108
+ let n = M.current, r = n.recovery ?? h(n.errorBus, b);
109
+ E = ne(n.descriptors, x, t.maxPending ?? 32).reserve(t.key(e), r.subjectSignal);
62
110
  }
63
- ({runtime: y, cache: b, client: S} = await s(D, h, g));
111
+ let n = await l(M, b, x);
112
+ if (a.aborted) throw new f("inactive");
113
+ u(P, n), I(), {runtime: S, cache: C, client: w} = n;
64
114
  } catch (e) {
65
- if (k?.release(), v(), I(e), D.current.errorBus.emit({
115
+ if (E?.release(), _(), !F()) throw new f("inactive");
116
+ if (U((t) => t.owner === P ? {
117
+ ...t,
118
+ error: e
119
+ } : t), M.current.errorBus.emit({
66
120
  source: "mutation",
67
- tag: g,
121
+ tag: x,
68
122
  error: e,
69
123
  kind: "client",
70
124
  outcome: "not-started"
71
- }), f) return _(e);
125
+ }), m) return g(e);
72
126
  throw e;
73
127
  }
74
- let N = A(), F = u?.idempotencyKey ?? p();
75
- l({
76
- id: N,
77
- apiName: h,
78
- tag: g,
79
- input: r
80
- });
81
- let L = z.current, U = B.current;
128
+ let O = ie(), j = i?.idempotencyKey ?? v(), L = M.current.descriptors[x]?.outputRetention === "none", R = !M.current.descriptors[x] || M.current.descriptors[x]?.clientHistory?.input === "omit", z = o({
129
+ id: O,
130
+ apiName: b,
131
+ tag: x,
132
+ input: R ? void 0 : e,
133
+ ...R ? { inputOmitted: !0 } : {},
134
+ ...L ? { outputOmitted: !0 } : {}
135
+ }, P.subject), B = W.current, H = G.current;
82
136
  try {
83
- L ? L(j(b, N), r) : !U && M && M.length > 0 && b.autoApply(M, r, N);
137
+ I(), B ? B(ae(C, O), e) : !H && V && V.length > 0 && C.autoApply(V, e, O);
84
138
  } catch (e) {
85
- if (k?.release(), I(e), v(), b.revertByMutation(N), c(N, {
139
+ if (E?.release(), _(), C.revertByMutation(O), z.settle({
86
140
  status: "error",
87
141
  error: e
88
- }), f) return _(e);
142
+ }), !F()) throw new f("inactive");
143
+ if (U((t) => t.owner === P ? {
144
+ ...t,
145
+ error: e
146
+ } : t), m) return g(e);
89
147
  throw e;
90
148
  }
91
- let W, G = !1, K, q, J, Y = Date.now();
149
+ let J, Y = !1, X, Z, Q, $ = Date.now();
92
150
  try {
93
- if (k) {
94
- await k.ready, k.assertActive();
95
- let e = await s(D, h, g);
96
- y = e.runtime, S = e.client;
151
+ if (E) {
152
+ await E.ready, I(), E.assertActive();
153
+ let e = await l(M, b, x);
154
+ I(), S = e.runtime, w = e.client;
97
155
  }
98
- let l = async (t, n, s, c) => {
99
- let l = i(t), u = l?.getSnapshot();
100
- q = l === void 0 ? void 0 : () => D.current.runtime === t && u !== void 0 && l.getSnapshot() === u, c?.(), k?.assertActive();
101
- let d = E === void 0 ? void 0 : await E.prepare(t, n);
102
- u = l?.getSnapshot(), c?.();
103
- let f = a(n, g), p = m(f(r), F), _ = x.currentSpan.pipe(x.tap((t) => x.sync(() => {
104
- W = t.traceId, e({
105
- traceId: t.traceId,
106
- spanId: t.spanId,
156
+ let a = async (i, a, o, c) => {
157
+ I();
158
+ let l = te(i), u = l?.getSnapshot();
159
+ Z = l === void 0 ? void 0 : () => M.current.runtime === i && u !== void 0 && l.getSnapshot() === u, c?.(), E?.assertActive();
160
+ let d = T === void 0 ? void 0 : await T.prepare(i, a);
161
+ u = l?.getSnapshot(), I(), c?.();
162
+ let f = r(a, x), p = y(t(() => {
163
+ I(), c?.(), E?.assertActive();
164
+ let t = f(e);
165
+ return Y = !0, t;
166
+ }), j), m = D.currentSpan.pipe(D.tap((e) => D.sync(() => {
167
+ J = e.traceId, s({
168
+ traceId: e.traceId,
169
+ spanId: e.spanId,
107
170
  source: "mutation",
108
- tag: g
171
+ tag: x
109
172
  });
110
- })), x.catch(() => x.void), x.andThen(x.try({
173
+ })), D.catch(() => D.void), D.andThen(D.try({
111
174
  try: () => {
112
- c?.(), k?.assertActive(), G = !0;
175
+ I(), c?.(), E?.assertActive();
113
176
  },
114
177
  catch: (e) => e
115
- })), x.andThen(p)).pipe(x.withSpan(`client.mutation.${g}`, { attributes: {
116
- "voltro.api": h,
117
- "voltro.mutationId": N,
118
- "rpc.tag": g,
119
- "voltro.replay": s
178
+ })), D.andThen(p)).pipe(D.withSpan(`client.mutation.${x}`, { attributes: {
179
+ "voltro.api": b,
180
+ "voltro.mutationId": O,
181
+ "rpc.tag": x,
182
+ "voltro.replay": o
120
183
  } }));
121
- return d === void 0 ? o(t, _) : d.run(_);
122
- }, d;
184
+ return d === void 0 ? n(i, m) : d.run(m);
185
+ }, o;
123
186
  try {
124
- d = await l(y, S, !1);
187
+ o = await a(S, w, !1);
125
188
  } catch (e) {
126
- let r = t(e, G ? "handler" : "client"), i = w(e, W);
127
- r === "transport" && (D.current.errorBus.emit({
189
+ I();
190
+ let t = p(e, Y ? "handler" : "client"), n = k(e, J);
191
+ t === "transport" && (M.current.errorBus.emit({
128
192
  source: "mutation",
129
- tag: g,
130
- error: i,
131
- kind: r,
132
- ...q === void 0 ? {} : { affectsTransport: q() },
133
- outcome: G ? "unknown" : "not-started",
134
- ...W === void 0 ? {} : { traceId: W }
135
- }), K = i);
136
- let a = D.current, o = a.recovery ?? n(a.errorBus, h), c = r === "transport" ? o.reserveMutationReplay(Y) : void 0;
137
- if (c === void 0) throw i;
193
+ tag: x,
194
+ error: n,
195
+ kind: t,
196
+ ...Z === void 0 ? {} : { affectsTransport: Z() },
197
+ outcome: Y ? "unknown" : "not-started",
198
+ ...J === void 0 ? {} : { traceId: J }
199
+ }), X = n), I();
200
+ let r = M.current, i = r.recovery ?? h(r.errorBus, b), s = t === "transport" ? i.reserveMutationReplay($) : void 0;
201
+ if (s === void 0) throw n;
138
202
  try {
139
- if (!await o.waitForMutationReplaySupport(Y) || !await o.waitForProtocolReady(Y) || !c.isValid()) throw i;
203
+ if (!await i.waitForMutationReplaySupport($) || !await i.waitForProtocolReady($) || !s.isValid()) throw n;
140
204
  let e = () => {
141
- if (!c.isValid()) throw i;
205
+ if (I(), !s.isValid()) throw n;
142
206
  };
143
207
  for (;;) {
144
208
  e();
145
- let t = await s(D, h, g);
209
+ let t = await l(M, b, x);
146
210
  try {
147
- d = await l(t.runtime, t.client, !0, e);
211
+ o = await a(t.runtime, t.client, !0, e);
148
212
  break;
149
213
  } catch (t) {
150
214
  let n = t;
151
215
  if (n?._tag !== "MutationInFlight") throw t;
152
216
  let r = typeof n.retryAfterMs == "number" && Number.isFinite(n.retryAfterMs) && n.retryAfterMs > 0 ? n.retryAfterMs : 1e3;
153
- if (r >= o.replayWindowRemaining(Y)) throw t;
217
+ if (r >= i.replayWindowRemaining($)) throw t;
154
218
  await new Promise((e) => setTimeout(e, r)), e();
155
219
  }
156
220
  }
157
221
  } finally {
158
- c.release();
222
+ s.release();
159
223
  }
160
224
  }
161
- R(d), c(N, {
225
+ I();
226
+ let c = L || M.current.descriptors[x]?.outputRetention === "none";
227
+ U((e) => e.owner === P ? {
228
+ ...e,
229
+ data: c ? void 0 : o,
230
+ error: void 0
231
+ } : e), I(), z.settle({
162
232
  status: "success",
163
- output: d
164
- }), D.current.errorBus.emitSuccess({
233
+ output: c ? void 0 : o,
234
+ ...c ? { outputOmitted: !0 } : {}
235
+ }), I(), M.current.errorBus.emitSuccess({
165
236
  source: "mutation",
166
- tag: g
167
- }), b.confirmByMutation(N), u?.onSuccess?.(d, r);
168
- let f = O(u?.notify?.success, d, r);
169
- return f !== void 0 && T?.success(f), d;
237
+ tag: x
238
+ }), I(), C.confirmByMutation(O), I(), i?.onSuccess?.(o, e), I();
239
+ let u = N(i?.notify?.success, o, e);
240
+ return I(), u !== void 0 && A?.success(u), o;
170
241
  } catch (e) {
171
- G && (K !== void 0 || t(e, "handler") === "transport") && (J = "unknown");
172
- let n = w(e, W);
173
- if (I(n), c(N, {
242
+ let t = F();
243
+ !Y || re(e) || t && X === void 0 && p(e, "handler") !== "transport" || (Q = "unknown");
244
+ let n = k(t ? e : new f("inactive"), J);
245
+ if (z.settle({
174
246
  status: "error",
175
247
  error: n
176
- }), b.revertByMutation(N), Object.is(K, n) || D.current.errorBus.emit({
248
+ }), C.revertByMutation(O), I(), U((e) => e.owner === P ? {
249
+ ...e,
250
+ error: n
251
+ } : e), I(), Object.is(X, n) || (Y && J !== void 0 && p(e, "handler") === "handler" && c({
252
+ traceId: J,
253
+ tag: x
254
+ }), M.current.errorBus.emit({
177
255
  source: "mutation",
178
- tag: g,
256
+ tag: x,
179
257
  error: n,
180
- kind: G ? t(e, "handler") : "client",
181
- ...q === void 0 ? {} : { affectsTransport: q() },
182
- outcome: G && t(e, "handler") === "transport" ? "unknown" : G ? "known" : "not-started",
183
- ...W === void 0 ? {} : { traceId: W }
184
- }), f) return _(n);
258
+ kind: Y ? p(e, "handler") : "client",
259
+ ...Z === void 0 ? {} : { affectsTransport: Z() },
260
+ outcome: Y && p(e, "handler") === "transport" ? "unknown" : Y ? "known" : "not-started",
261
+ ...J === void 0 ? {} : { traceId: J }
262
+ })), I(), m) return g(n);
185
263
  throw n;
186
264
  } finally {
187
- k?.release(J), v();
265
+ E?.release(Q), _();
188
266
  }
189
267
  }, [
190
- h,
191
- g,
192
- M
193
- ]);
194
- return v(() => {
268
+ b,
269
+ x,
270
+ V,
271
+ P,
272
+ F,
273
+ I
274
+ ]), J = H.owner === P ? H : {
275
+ pending: !1,
276
+ error: void 0,
277
+ data: void 0
278
+ };
279
+ return w(() => {
195
280
  let e = {
196
- mutate: H,
197
- pending: N,
198
- error: F,
199
- data: L,
200
- withOptimistic: (t) => (z.current = t, e),
201
- withoutOptimistic: () => (B.current = !0, e),
202
- withConnectionSetup: (t) => (u(H, f(t, h)), e)
281
+ mutate: q,
282
+ pending: J.pending,
283
+ error: J.error,
284
+ data: z ? void 0 : J.data,
285
+ withOptimistic: (t) => (L(), W.current = t, e),
286
+ withoutOptimistic: () => (L(), G.current = !0, e),
287
+ withConnectionSetup: (t) => (L(), g(q, _(t, b)), e)
203
288
  };
204
289
  return e;
205
290
  }, [
206
- H,
207
- N,
208
- F,
291
+ q,
292
+ J.pending,
293
+ J.error,
294
+ J.data,
295
+ b,
209
296
  L,
210
- h
297
+ z
211
298
  ]);
212
299
  };
213
- export { h as MutationSerialExecutionError, p as freshIdempotencyKey, D as getMutationNotifier, E as setMutationNotifier, M as useMutation, m as withIdempotencyKey };
300
+ export { b as MutationSerialExecutionError, v as freshIdempotencyKey, M as getMutationNotifier, j as setMutationNotifier, F as useMutation, y as withIdempotencyKey };
@@ -1,12 +1,12 @@
1
- import { recoveryCoordinatorFor as e } from "./recoveryCoordinator.js";
2
- import { useFrameworkApi as t } from "./runtimeContext.js";
1
+ import { useFrameworkApi as e } from "./runtimeContext.js";
2
+ import { recoveryCoordinatorFor as t } from "./recoveryCoordinator.js";
3
3
  import { useEffect as n, useRef as r } from "react";
4
4
  // src/useOnRpcError.ts
5
5
  var i = (i, a) => {
6
- let o = t(i), s = o.recovery ?? e(o.errorBus, i), c = r(a);
6
+ let o = e(i), s = o.recovery ?? t(o.errorBus, i), c = r(a);
7
7
  c.current = a, n(() => s.onVisibleError((e) => c.current(e)), [s]);
8
- }, a = (e, i) => {
9
- let { errorBus: a } = t(e), o = r(i);
8
+ }, a = (t, i) => {
9
+ let { errorBus: a } = e(t), o = r(i);
10
10
  o.current = i, n(() => a.on((e) => o.current(e)), [a]);
11
11
  };
12
12
  export { a as useOnRawRpcError, i as useOnRpcError };
@@ -1,15 +1,15 @@
1
- import { useFrameworkApi as e } from "./runtimeContext.js";
2
- import { useSubscription as t } from "./useSubscription.js";
3
- import { readPreloadedSeedOutcome as n, readPreloadedSnapshot as r } from "./preloadSeed.js";
1
+ import { readPreloadedSeedOutcome as e, readPreloadedSnapshot as t } from "./preloadSeed.js";
2
+ import { useFrameworkApi as n } from "./runtimeContext.js";
3
+ import { useSubscription as r } from "./useSubscription.js";
4
4
  import { useSyncExternalStore as i } from "react";
5
5
  // src/usePreloadedSubscription.ts
6
6
  var a = () => !0, o = (e) => () => {};
7
7
  function s(s, c, l = {}, u = {}) {
8
- let { recovery: d } = e(s), f = i(d?.subscribe ?? o, d?.getPreloadSeedPermission ?? a, a), p = f ? r(s, c, l) : void 0, m = p === void 0 ? u : {
8
+ let { recovery: d } = n(s), f = i(d?.subscribe ?? o, d?.getPreloadSeedPermission ?? a, a), p = f ? t(s, c, l) : void 0, m = p === void 0 ? u : {
9
9
  ...u,
10
10
  initialSnapshot: p
11
- }, h = t(s, c, l, m);
12
- return f && n(s, c, l) === "failed" ? {
11
+ }, h = r(s, c, l, m);
12
+ return f && e(s, c, l) === "failed" ? {
13
13
  ...h,
14
14
  preloadFailed: !0
15
15
  } : h;
@@ -1,16 +1,78 @@
1
1
  import { useFrameworkApi as e } from "./runtimeContext.js";
2
- import { resolveByTag as t } from "./tagWalk.js";
3
- import { runRpcPromise as n } from "./runRpc.js";
4
- import { awaitResolvedApi as r } from "./awaitResolvedApi.js";
5
- import { useMemo as i, useRef as a } from "react";
2
+ import { awaitResolvedApi as t, bindResolvedRpcInvocationOwner as n, captureRpcInvocationOwner as r, rpcInvocationOwnerCurrent as i } from "./awaitResolvedApi.js";
3
+ import { ConnectionPrerequisiteInvalidated as a } from "./connectionPrerequisite.js";
4
+ import { rpcCallOnce as o } from "./rpcCallOnce.js";
5
+ import { resolveByTag as s } from "./tagWalk.js";
6
+ import { useEffect as c, useMemo as l, useRef as u, useState as d } from "react";
6
7
  // src/useRpcCaller.ts
7
- var o = (o) => {
8
- let s = e(o), c = a(s);
9
- return c.current = s, i(() => Object.freeze({ call: async (e, i, a) => {
10
- let { runtime: s, client: l } = await r(c, o, e, a?.signal);
11
- a?.signal?.throwIfAborted();
12
- let u = t(l, e);
13
- return n(s, u(i), { signal: a?.signal });
14
- } }), [o]);
8
+ var f = (f) => {
9
+ let p = e(f), m = u({
10
+ apiName: f,
11
+ handleRef: { current: p },
12
+ owner: void 0
13
+ });
14
+ m.current.apiName !== f && (m.current = {
15
+ apiName: f,
16
+ handleRef: { current: p },
17
+ owner: void 0
18
+ });
19
+ let h = m.current.handleRef;
20
+ h.current = p;
21
+ let g = r(p, f, "rpc.caller", m.current.owner);
22
+ m.current.owner = g;
23
+ let _ = g.subject, [, v] = d(0);
24
+ c(() => {
25
+ if (_.aborted) return;
26
+ let e = () => v((e) => e + 1);
27
+ return _.addEventListener("abort", e, { once: !0 }), () => _.removeEventListener("abort", e);
28
+ }, [_]);
29
+ let y = () => {
30
+ if (!i(g, h.current)) throw new a("inactive");
31
+ };
32
+ return l(() => Object.freeze({
33
+ call: (e, t, n) => o(h, f, e, t, n?.signal, "[rpc.call]", g),
34
+ query: async (e, n, r) => {
35
+ y();
36
+ let i = await t(h, f, e, r?.signal);
37
+ if (i.descriptors[e]?.kind !== "query") throw Error(`Voltro: query '${e}' is not a registered query on api '${f}'.`);
38
+ let a = await o({ current: i }, f, e, n ?? {}, r?.signal, "[rpc.query]", g);
39
+ return y(), a;
40
+ },
41
+ subscribe: (e, r, a) => {
42
+ y();
43
+ let o = new AbortController(), c = g.subject, l = !1, u, d = c, p = () => {
44
+ l || (l = !0, o.abort(), d.removeEventListener("abort", p), u?.());
45
+ };
46
+ return d.addEventListener("abort", p, { once: !0 }), t(h, f, e, o.signal).then((t) => {
47
+ if (l) return;
48
+ if (c.aborted) {
49
+ p();
50
+ return;
51
+ }
52
+ if (n(g, t), y(), t.descriptors[e]?.kind !== "query") throw Error(`[rpc.subscribe] ${e}: only queries can be subscribed to`);
53
+ if (d.removeEventListener("abort", p), d = g.subject, d.aborted) {
54
+ p();
55
+ return;
56
+ }
57
+ d.addEventListener("abort", p, { once: !0 });
58
+ let o, f = -2, m, _, v = () => {
59
+ if (l || !o) return;
60
+ if (!i(g, h.current)) {
61
+ p();
62
+ return;
63
+ }
64
+ let e = o.getSnapshot();
65
+ e.error !== void 0 && e.error !== m && (m = e.error, a.onError?.(e.error)), !l && e.revision >= 0 && e.data !== void 0 && (e.revision !== f || e.data !== _) && (f = e.revision, _ = e.data, a.onValue(e.data));
66
+ };
67
+ o = t.cache.subscribe([e, r], () => s(t.client, e)(r), t.runtime, v), u = o.unsubscribe, l ? u() : v();
68
+ }).catch((e) => {
69
+ !l && i(g, h.current) && a.onError?.(e);
70
+ }), Object.freeze({ unsubscribe: p });
71
+ }
72
+ }), [
73
+ f,
74
+ h,
75
+ g
76
+ ]);
15
77
  };
16
- export { o as useRpcCaller };
78
+ export { f as useRpcCaller };
@@ -0,0 +1,29 @@
1
+ import { captureRpcInvocationOwner as e, rpcInvocationOwnerCurrent as t } from "./awaitResolvedApi.js";
2
+ import { ConnectionPrerequisiteInvalidated as n } from "./connectionPrerequisite.js";
3
+ import { useCallback as r, useEffect as i, useRef as a, useState as o } from "react";
4
+ // src/useRpcInvocationOwner.ts
5
+ var s = (s, c, l) => {
6
+ let u = a(void 0), d = e(s.current, c, l, u.current);
7
+ u.current = d;
8
+ let f = d.subject, p = a(!0), [, m] = o(0);
9
+ i(() => (p.current = !0, () => {
10
+ p.current = !1;
11
+ }), []), i(() => {
12
+ let e = d.subject;
13
+ if (e.aborted) return;
14
+ let t = () => m((e) => e + 1);
15
+ return e.addEventListener("abort", t, { once: !0 }), () => e.removeEventListener("abort", t);
16
+ }, [d, f]);
17
+ let h = r(() => u.current === d && t(d, s.current), [d, s]), g = r(() => p.current && h(), [h]);
18
+ return {
19
+ owner: d,
20
+ current: g,
21
+ assertCurrent: r(() => {
22
+ if (!g()) throw new n("inactive");
23
+ }, [g]),
24
+ assertOwnerCurrent: r(() => {
25
+ if (!h()) throw new n("inactive");
26
+ }, [h])
27
+ };
28
+ };
29
+ export { s as useRpcInvocationOwner };