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