@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/useAction.js CHANGED
@@ -1,16 +1,20 @@
1
- import { publishClientTrace as e } from "./traceBus.js";
2
- import { rpcErrorKind as t } from "./rpcErrorKind.js";
3
- import { useFrameworkApi as n } from "./runtimeContext.js";
4
- import { resolveByTag as r } from "./tagWalk.js";
5
- import { runRpcPromise as i } from "./runRpc.js";
6
- import { awaitReadyApi as a } from "./awaitResolvedApi.js";
7
- import { settleMutation as o, startMutation as s } from "./mutationsBus.js";
8
- import { freshIdempotencyKey as c, withIdempotencyKey as l } from "./mutationIdentity.js";
9
- import { getMutationNotifier as u } from "./useMutation.js";
10
- import { useCallback as d, useMemo as f, useRef as p, useState as m } from "react";
11
- import { Effect as h } from "effect";
1
+ import { publishClientTrace as e, publishClientTraceRefusal as t } from "./traceBus.js";
2
+ import { rpcErrorKind as n } from "./rpcErrorKind.js";
3
+ import { useFrameworkApi as r } from "./runtimeContext.js";
4
+ import { resolveByTag as i } from "./tagWalk.js";
5
+ import { reportUnknownRpcTag as a } from "./unknownRpcTag.js";
6
+ import { deferRpcMethod as o, runRpcPromise as s } from "./runRpc.js";
7
+ import { ConnectionPrerequisiteInvalidated as c } from "./connectionPrerequisite.js";
8
+ import { awaitReadyApi as l, bindResolvedRpcInvocationOwner as u } from "./awaitResolvedApi.js";
9
+ import { useRpcInvocationOwner as d } from "./useRpcInvocationOwner.js";
10
+ import { omitMutationInputs as f, omitMutationOutputs as p, startMutation as m } from "./mutationsBus.js";
11
+ import { freshIdempotencyKey as h, withIdempotencyKey as g } from "./mutationIdentity.js";
12
+ import { getMutationNotifier as _ } from "./useMutation.js";
13
+ import { waitForInvocation as v } from "./invocationWait.js";
14
+ import { useCallback as y, useEffect as b, useMemo as x, useRef as S, useState as C } from "react";
15
+ import { Effect as w } from "effect";
12
16
  // src/useAction.ts
13
- var g = (e, t) => {
17
+ var T = (e, t) => {
14
18
  if (t === void 0 || typeof e != "object" || !e) return e;
15
19
  try {
16
20
  Object.defineProperty(e, "__voltroTraceId", {
@@ -20,81 +24,151 @@ var g = (e, t) => {
20
24
  });
21
25
  } catch {}
22
26
  return e;
23
- }, _ = (e, t, n) => typeof e == "function" ? e(t, n) : e, v = 0, y = () => `act_${++v}`, b = (v, b) => {
24
- let x = n(v), { descriptors: S } = x, C = p(x);
25
- if (C.current = x, process.env.NODE_ENV !== "production") {
26
- let e = S[b]?.kind;
27
- e !== void 0 && e !== "action" && console.error(`[@voltro/client] useAction('${v}', '${b}') targets a ${e}, not an action. Use ${e === "mutation" ? "useMutation" : e === "workflow" ? "useWorkflow" : `the ${e} hook`} instead.`);
27
+ }, E = (e, t, n) => typeof e == "function" ? e(t, n) : e, D = 0, O = () => `act_${++D}`, k = (D, k) => {
28
+ let A = r(D), { descriptors: j } = A, M = j[k]?.outputRetention === "none", N = j[k]?.clientHistory?.input === "omit", P = S(A);
29
+ P.current = A;
30
+ let { owner: F, current: I, assertCurrent: L } = d(P, D, k);
31
+ if (process.env.NODE_ENV !== "production") {
32
+ a("useAction", D, k, j);
33
+ let e = j[k]?.kind;
34
+ e !== void 0 && e !== "action" && console.error(`[@voltro/client] useAction('${D}', '${k}') targets a ${e}, not an action. Use ${e === "mutation" ? "useMutation" : e === "workflow" ? "useWorkflow" : `the ${e} hook`} instead.`);
28
35
  }
29
- let [w, T] = m(!1), [E, D] = m(void 0), [O, k] = m(void 0), A = d(async (n, d) => {
30
- let f = d?.onError !== void 0 || d?.notify?.error !== void 0;
31
- T(!0), D(void 0);
32
- let p = y(), m = d?.idempotencyKey ?? c();
33
- s({
34
- id: p,
35
- apiName: v,
36
- tag: b,
37
- input: n,
38
- kind: "action"
39
- });
40
- let x, S = !1;
36
+ let [R, z] = C(() => ({
37
+ owner: F,
38
+ pending: 0,
39
+ error: void 0,
40
+ lastResult: void 0
41
+ }));
42
+ b(() => {
43
+ M && (z((e) => e.lastResult === void 0 ? e : {
44
+ ...e,
45
+ lastResult: void 0
46
+ }), p(D, k));
47
+ }, [
48
+ M,
49
+ D,
50
+ k
51
+ ]), b(() => {
52
+ N && f(D, k);
53
+ }, [
54
+ N,
55
+ D,
56
+ k
57
+ ]);
58
+ let B = y(async (r, a) => {
59
+ let d = a?.signal, f = () => {
60
+ if (L(), d?.aborted) throw new DOMException("action aborted", "AbortError");
61
+ };
62
+ f();
63
+ let p = F.subject, y = a?.onError !== void 0 || a?.notify?.error !== void 0;
64
+ z((e) => ({
65
+ owner: F,
66
+ pending: e.owner === F ? e.pending + 1 : 1,
67
+ error: void 0,
68
+ lastResult: e.owner === F ? e.lastResult : void 0
69
+ }));
70
+ let b = O(), x = a?.idempotencyKey ?? h(), S = !1, C = () => {
71
+ let e = P.current.descriptors[k], t = !e || e.clientHistory?.input === "omit";
72
+ return S = e?.outputRetention === "none", m({
73
+ id: b,
74
+ apiName: D,
75
+ tag: k,
76
+ input: t ? void 0 : r,
77
+ kind: "action",
78
+ ...t ? { inputOmitted: !0 } : {},
79
+ ...S ? { outputOmitted: !0 } : {}
80
+ }, F.subject);
81
+ }, A = F.resolved ? C() : void 0, j, M = !1;
41
82
  try {
42
- let { runtime: t, client: s } = await a(C, v, b), c = r(s, b), f = h.currentSpan.pipe(h.tap((t) => h.sync(() => {
43
- x = t.traceId, e({
83
+ let t = await l(P, D, k, d);
84
+ if (p.aborted) throw new c("inactive");
85
+ u(F, t), f(), A ??= C(), f();
86
+ let { runtime: n, client: m } = t, h = i(m, k), y = w.currentSpan.pipe(w.tap((t) => w.sync(() => {
87
+ j = t.traceId, e({
44
88
  traceId: t.traceId,
45
89
  spanId: t.spanId,
46
90
  source: "action",
47
- tag: b
91
+ tag: k
48
92
  });
49
- })), h.catch(() => h.void), h.andThen(l(c(n), m))).pipe(h.withSpan(`client.action.${b}`, { attributes: {
50
- "voltro.api": v,
51
- "voltro.actionId": p,
52
- "rpc.tag": b
53
- } }));
54
- S = !0;
55
- let g = await i(t, f);
56
- k(g), o(p, {
93
+ })), w.catch(() => w.void), w.andThen(o(() => {
94
+ f();
95
+ let e = h(r);
96
+ return M = !0, g(e, x);
97
+ }))).pipe(w.withSpan(`client.action.${k}`, { attributes: {
98
+ "voltro.api": D,
99
+ "voltro.actionId": b,
100
+ "rpc.tag": k
101
+ } })), T = await v(s(n, y), d, f);
102
+ f();
103
+ let O = S || P.current.descriptors[k]?.outputRetention === "none";
104
+ z((e) => e.owner === F ? {
105
+ ...e,
106
+ error: void 0,
107
+ lastResult: O ? void 0 : T
108
+ } : e), f(), A?.settle({
57
109
  status: "success",
58
- output: g
59
- }), C.current.errorBus.emitSuccess({
110
+ output: O ? void 0 : T,
111
+ ...O ? { outputOmitted: !0 } : {}
112
+ }), f(), P.current.errorBus.emitSuccess({
60
113
  source: "action",
61
- tag: b
62
- }), d?.onSuccess?.(g, n);
63
- let y = _(d?.notify?.success, g, n);
64
- return y !== void 0 && u()?.success(y), g;
114
+ tag: k
115
+ }), f(), a?.onSuccess?.(T, r), f();
116
+ let N = E(a?.notify?.success, T, r);
117
+ return f(), N !== void 0 && _()?.success(N), f(), T;
65
118
  } catch (e) {
66
- let r = g(e, x);
67
- if (D(r), o(p, {
119
+ let i = I() && !d?.aborted, o = T(I() ? d?.aborted ? new DOMException("action aborted", "AbortError") : e : new c("inactive"), j);
120
+ if (A?.settle({
68
121
  status: "error",
69
- error: r
70
- }), C.current.errorBus.emit({
122
+ error: o
123
+ }), !i) throw o;
124
+ if (f(), z((e) => e.owner === F ? {
125
+ ...e,
126
+ error: o
127
+ } : e), f(), M && j !== void 0 && n(e, "handler") === "handler" && t({
128
+ traceId: j,
129
+ tag: k
130
+ }), P.current.errorBus.emit({
71
131
  source: "action",
72
- tag: b,
73
- error: r,
74
- kind: S ? t(e, "handler") : "client",
75
- outcome: S && t(e, "handler") === "transport" ? "unknown" : S ? "known" : "not-started",
76
- ...x === void 0 ? {} : { traceId: x }
77
- }), f) {
78
- d?.onError?.(r, n);
79
- let e = _(d?.notify?.error, r, n);
80
- e !== void 0 && u()?.error(e);
132
+ tag: k,
133
+ error: o,
134
+ kind: M ? n(e, "handler") : "client",
135
+ outcome: M && n(e, "handler") === "transport" ? "unknown" : M ? "known" : "not-started",
136
+ ...j === void 0 ? {} : { traceId: j }
137
+ }), f(), y) {
138
+ a?.onError?.(o, r), f();
139
+ let e = E(a?.notify?.error, o, r);
140
+ f(), e !== void 0 && _()?.error(e), f();
81
141
  return;
82
142
  }
83
- throw r;
143
+ throw o;
84
144
  } finally {
85
- T(!1);
145
+ I() && z((e) => e.owner === F ? {
146
+ ...e,
147
+ pending: Math.max(0, e.pending - 1)
148
+ } : e);
86
149
  }
87
- }, [v, b]);
88
- return f(() => ({
89
- run: A,
90
- pending: w,
91
- error: E,
92
- lastResult: O
150
+ }, [
151
+ D,
152
+ k,
153
+ F,
154
+ I,
155
+ L
156
+ ]), V = R.owner === F ? R : {
157
+ pending: 0,
158
+ error: void 0,
159
+ lastResult: void 0
160
+ };
161
+ return x(() => ({
162
+ run: B,
163
+ pending: V.pending > 0,
164
+ error: V.error,
165
+ lastResult: M ? void 0 : V.lastResult
93
166
  }), [
94
- A,
95
- w,
96
- E,
97
- O
167
+ B,
168
+ V.pending,
169
+ V.error,
170
+ V.lastResult,
171
+ M
98
172
  ]);
99
173
  };
100
- export { b as useAction };
174
+ export { k as useAction };