@voltro/client 0.84.0 → 0.86.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 (111) hide show
  1. package/CHANGELOG.md +1215 -0
  2. package/THIRD-PARTY-NOTICES.md +5 -89
  3. package/dist/analyticsCatalog.js +47 -0
  4. package/dist/apiConnectivity.js +33 -0
  5. package/dist/apiRuntime.js +135 -0
  6. package/dist/apiSupervisor.js +125 -0
  7. package/dist/authRefresh.js +20 -0
  8. package/dist/awaitResolvedApi.js +23 -0
  9. package/dist/bootstrap.d.ts +1144 -0
  10. package/dist/bootstrap.js +8 -0
  11. package/dist/cellMerger.js +6 -0
  12. package/dist/clientDiagnostics.js +94 -0
  13. package/dist/clientErrorBus.js +16 -0
  14. package/dist/clientScheduler.js +31 -0
  15. package/dist/connectionPrerequisite.js +70 -0
  16. package/dist/connectionScope.js +35 -0
  17. package/dist/connectionSetupController.js +206 -0
  18. package/dist/createHooks.js +15 -0
  19. package/dist/deepLink.js +69 -0
  20. package/dist/filters.js +58 -0
  21. package/dist/form.d.ts +8 -11
  22. package/dist/form.js +5 -2
  23. package/dist/formBinding.js +325 -0
  24. package/dist/formData.js +65 -0
  25. package/dist/formFlashId.js +4 -0
  26. package/dist/frameworkRuntimesContext.js +8 -0
  27. package/dist/globalContext.js +10 -0
  28. package/dist/index.d.ts +285 -279
  29. package/dist/index.js +83 -3686
  30. package/dist/liveRowSort.js +66 -0
  31. package/dist/mutationConnectionSetup.js +16 -0
  32. package/dist/mutationIdentity.js +10 -0
  33. package/dist/mutationSerialExecution.js +53 -0
  34. package/dist/mutationsBus.js +40 -0
  35. package/dist/onlineSource.js +13 -0
  36. package/dist/outbox.js +213 -0
  37. package/dist/outboxHttp.js +104 -0
  38. package/dist/outboxState.js +43 -0
  39. package/dist/permissions.js +20 -0
  40. package/dist/platform.js +121 -0
  41. package/dist/preloadSeed.js +47 -0
  42. package/dist/provider.js +22 -0
  43. package/dist/query.js +12 -2
  44. package/dist/recoveryCoordinator.js +195 -0
  45. package/dist/recoveryRetry.js +24 -0
  46. package/dist/richText.js +176 -0
  47. package/dist/rpcErrorBus.js +39 -0
  48. package/dist/rpcErrorKind.js +13 -0
  49. package/dist/runRpc.js +12 -0
  50. package/dist/runtime.d.ts +79 -15
  51. package/dist/runtime.js +16 -129
  52. package/dist/runtimeCapabilities.js +44 -0
  53. package/dist/runtimeContext.js +49 -0
  54. package/dist/schemaFields.js +219 -0
  55. package/dist/scopeMembership.js +12 -0
  56. package/dist/seedResolvers.js +7 -0
  57. package/dist/sequence.js +65 -0
  58. package/dist/serverRequest.js +24 -0
  59. package/dist/serviceClient.js +128 -0
  60. package/dist/squashRpcCause.js +8 -0
  61. package/dist/stableKey.js +21 -0
  62. package/dist/store.js +288 -0
  63. package/dist/storeStorage.js +28 -0
  64. package/dist/subscriptionCache.js +724 -0
  65. package/dist/tagWalk.js +23 -0
  66. package/dist/traceBus.js +10 -0
  67. package/dist/tracking.js +40 -0
  68. package/dist/uploadProgress.js +21 -0
  69. package/dist/useAction.js +101 -0
  70. package/dist/useAgent.js +53 -0
  71. package/dist/useAgentChat.js +43 -0
  72. package/dist/useAgentStream.js +64 -0
  73. package/dist/useApiOnline.js +14 -0
  74. package/dist/useCapabilityManifest.js +101 -0
  75. package/dist/useConnectionSetup.js +28 -0
  76. package/dist/useConnectionStatus.js +38 -0
  77. package/dist/useConnections.js +107 -0
  78. package/dist/useDataCopilot.js +24 -0
  79. package/dist/useDataTable.js +34 -0
  80. package/dist/useDerived.js +22 -0
  81. package/dist/useEvent.js +88 -0
  82. package/dist/useFormBinding.js +476 -0
  83. package/dist/useLocalQuery.js +59 -0
  84. package/dist/useMutation.js +214 -0
  85. package/dist/useOnRpcError.js +13 -0
  86. package/dist/usePreloadedSubscription.js +18 -0
  87. package/dist/usePreview.js +14 -0
  88. package/dist/useProvenance.js +51 -0
  89. package/dist/useQueryField.js +34 -0
  90. package/dist/useRecord.js +12 -0
  91. package/dist/useRefreshSubscriptions.js +22 -0
  92. package/dist/useResourceCan.js +18 -0
  93. package/dist/useResumableAgentStream.js +75 -0
  94. package/dist/useSequence.js +25 -0
  95. package/dist/useSkeleton.js +13 -0
  96. package/dist/useSubscription.js +89 -0
  97. package/dist/useSubscriptionHealth.js +32 -0
  98. package/dist/useTranscription.js +51 -0
  99. package/dist/useUndo.js +33 -0
  100. package/dist/useUndoLog.js +44 -0
  101. package/dist/useUpload.js +296 -0
  102. package/dist/useWindowedSubscription.js +65 -0
  103. package/dist/useWorkflow.js +284 -0
  104. package/dist/useWorkflowRunState.js +93 -0
  105. package/dist/validation.js +39 -0
  106. package/dist/validationMessages.js +5 -0
  107. package/package.json +8 -5
  108. package/dist/authRefresh-C4Ow8HH7.js +0 -346
  109. package/dist/formData-dGz5aoUR.js +0 -649
  110. package/dist/queryOnce-CvdopzOj.js +0 -55
  111. package/dist/runRpc-DvHpFZAZ.js +0 -1034
@@ -1,1034 +0,0 @@
1
- import { Cause as e, Effect as t, Exit as n, Fiber as r, Option as i, ParseResult as a, Schedule as o, Stream as s } from "effect";
2
- import * as c from "@effect/platform/Headers";
3
- import { RpcClient as l, RpcClientError as u } from "@effect/rpc";
4
- import { applyRowPatch as d } from "@voltro/protocol";
5
- //#region src/cellMerger.ts
6
- var f = Symbol.for("voltro.client.cellMerger"), p = (e) => {
7
- globalThis[f] = e;
8
- }, m = () => globalThis[f], h = (t) => {
9
- let n = e.squash(t);
10
- return i.isNone(e.failureOption(t)) && a.isParseError(n) && console.error("[voltro] a value failed its schema in the client — if it is the request payload, the request never reached the server, so no server log has it:\n" + n.message), n;
11
- }, g = /* @__PURE__ */ new Set(), _ = (e) => {
12
- for (let t of g) try {
13
- t(e);
14
- } catch {}
15
- }, v = (e) => (g.add(e), () => {
16
- g.delete(e);
17
- }), y = /* @__PURE__ */ new WeakMap(), b = (e, t) => ((typeof e == "object" && e || typeof e == "function") && y.set(e, t), e), x = (e, t) => {
18
- if ((typeof e != "object" || !e) && typeof e != "function") return t;
19
- let n = y.get(e);
20
- if (n !== void 0) return n;
21
- try {
22
- return u.TypeId in e ? "transport" : t;
23
- } catch {
24
- return t;
25
- }
26
- }, S = "voltro.client-diagnostics.v1", C = 400, w = 240, T = Symbol.for("@voltro/client/diagnostics"), E = globalThis, D = () => typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now(), O = E[T] ?? (E[T] = {
27
- sessionStartedAt: Date.now(),
28
- monotonicStartedAt: D(),
29
- sequence: 0,
30
- dropped: 0,
31
- storage: void 0,
32
- events: [],
33
- resolved: {},
34
- loadedStorage: !1,
35
- listeners: /* @__PURE__ */ new Set()
36
- }), k = (e) => e.length <= w ? e : `${e.slice(0, w)}…`, ee = (e) => {
37
- let t = {};
38
- for (let [n, r] of Object.entries(e)) r !== void 0 && (t[n] = typeof r == "string" ? k(r) : r);
39
- return t;
40
- }, te = (e) => {
41
- if (typeof e != "object" || !e) return !1;
42
- let t = e;
43
- return typeof t.sequence == "number" && typeof t.at == "number" && typeof t.elapsedMs == "number" && typeof t.api == "string" && typeof t.kind == "string" && typeof t.fields == "object" && t.fields !== null;
44
- }, A = () => {
45
- if (O.storage !== void 0) try {
46
- O.storage.setItem(S, JSON.stringify({
47
- version: 1,
48
- sessionStartedAt: O.sessionStartedAt,
49
- sequence: O.sequence,
50
- dropped: O.dropped,
51
- events: O.events
52
- }));
53
- } catch {}
54
- }, j = () => {
55
- for (let e of O.listeners) try {
56
- e();
57
- } catch {}
58
- }, ne = (e) => {
59
- if (O.storage = e, !O.loadedStorage) {
60
- O.loadedStorage = !0;
61
- try {
62
- let t = e.getItem(S);
63
- if (t !== null) {
64
- let e = JSON.parse(t), n = Array.isArray(e.events) ? e.events.filter(te) : [], r = Math.max(typeof e.sequence == "number" ? e.sequence : 0, 0, ...n.map((e) => e.sequence)), i = O.events.map((e, t) => ({
65
- ...e,
66
- sequence: r + t + 1
67
- })), a = [...n, ...i], o = Math.max(0, a.length - C), s = typeof e.dropped == "number" ? e.dropped : 0;
68
- O.dropped = O.dropped + s + o, O.events = a.slice(-400), O.sequence = Math.max(r + i.length, ...O.events.map((e) => e.sequence)), typeof e.sessionStartedAt == "number" && (O.sessionStartedAt = Math.min(O.sessionStartedAt, e.sessionStartedAt));
69
- }
70
- } catch {}
71
- }
72
- A();
73
- }, M = () => {
74
- O.storage = void 0;
75
- }, N = (e, t, n = {}) => {
76
- let r = {
77
- sequence: ++O.sequence,
78
- at: Date.now(),
79
- elapsedMs: Math.max(0, Math.round(D() - O.monotonicStartedAt)),
80
- api: k(e),
81
- kind: t,
82
- fields: ee(n)
83
- };
84
- return t === "runtime.resolved" && (O.resolved = {
85
- ...O.resolved,
86
- [r.api]: r.fields
87
- }), O.events.push(r), O.events.length > C && (O.events.splice(0, O.events.length - C), O.dropped += 1), A(), j(), r;
88
- }, re = (e) => ({
89
- version: 1,
90
- generatedAt: Date.now(),
91
- sessionStartedAt: O.sessionStartedAt,
92
- persisted: O.storage !== void 0,
93
- dropped: O.dropped,
94
- events: e === void 0 ? [...O.events] : O.events.filter((t) => t.api === e),
95
- runtime: e === void 0 ? { ...O.resolved } : O.resolved[e] === void 0 ? {} : { [e]: O.resolved[e] }
96
- }), ie = () => {
97
- O.events = [], O.dropped = 0;
98
- try {
99
- O.storage?.removeItem(S);
100
- } catch {}
101
- j();
102
- }, ae = (e) => (O.listeners.add(e), () => {
103
- O.listeners.delete(e);
104
- }), P = (e) => {
105
- let t = (e) => e == null ? "null" : typeof e == "string" ? JSON.stringify(e) : typeof e == "number" || typeof e == "boolean" ? String(e) : Array.isArray(e) ? `[${e.map(t).join(",")}]` : typeof e == "object" ? `{${Object.keys(e).sort().map((n) => `${JSON.stringify(n)}:${t(e[n])}`).join(",")}}` : String(e);
106
- return t(e);
107
- }, F = (e) => e === void 0 ? /* @__PURE__ */ Error("[voltro] the subscription failed without an error value") : e, oe = Object.freeze({
108
- data: void 0,
109
- revision: -1,
110
- emittedAt: void 0,
111
- error: void 0,
112
- pendingPatches: 0
113
- }), I = 5e3, L = (e, t) => e._tag === "snapshot" && t !== void 0 && e.reconciles === t, se = 0, R = 0, ce = () => `patch_${++R}`, z = (e, t) => {
114
- let n = e;
115
- for (let e of t.split(".")) {
116
- if (typeof n != "object" || !n) return;
117
- n = n[e];
118
- }
119
- return n;
120
- }, B = (e, t, n) => {
121
- let r = t.split("."), i = (e) => typeof e == "object" && e && !Array.isArray(e) ? { ...e } : {}, a = i(e), o = a;
122
- for (let e = 0; e < r.length - 1; e += 1) o[r[e]] = i(o[r[e]]), o = o[r[e]];
123
- return o[r[r.length - 1]] = n, a;
124
- }, V = (e, t) => {
125
- let n = e.by ?? "id", r = e.identify ? e.identify(t) : typeof t[n] == "string" ? t[n] : typeof t.id == "string" ? t.id : void 0, i = r === void 0 ? [] : Array.isArray(r) ? r : [r];
126
- return i.length === 0 ? null : new Set(i);
127
- }, le = (e, t, n) => {
128
- let r = e.path, i = e.by ?? "id";
129
- if (e.op === "insert") {
130
- let a = e.shapeItem ?? e.shape, o = a ? {
131
- [i]: n,
132
- ...a(t, n),
133
- optimistic: !0
134
- } : {
135
- [i]: n,
136
- ...t,
137
- optimistic: !0
138
- }, s = e.order ?? "append";
139
- return (e) => {
140
- let t = Array.isArray(z(e, r)) ? z(e, r) : [];
141
- return B(e ?? {}, r, s === "append" ? [...t, o] : [o, ...t]);
142
- };
143
- }
144
- let a = V(e, t);
145
- if (a === null) return null;
146
- if (e.op === "update") {
147
- let n = e.shapeItem ?? e.shape, o = n ? (e) => ({
148
- ...n(t, e),
149
- [i]: e[i],
150
- optimistic: !0
151
- }) : (e) => ({
152
- ...e,
153
- ...t,
154
- [i]: e[i],
155
- optimistic: !0
156
- });
157
- return (e) => {
158
- let t = z(e, r);
159
- if (!Array.isArray(t)) {
160
- if (typeof t != "object" || !t) return e;
161
- let n = t;
162
- return a.has(n[i]) ? B(e, r, o(n)) : e;
163
- }
164
- return B(e, r, t.map((e) => a.has(e[i]) ? o(e) : e));
165
- };
166
- }
167
- return (e) => {
168
- let t = Array.isArray(z(e, r)) ? z(e, r) : [];
169
- return B(e, r, t.filter((e) => !a.has(e[i])));
170
- };
171
- }, ue = (e, t, n) => {
172
- if (e.op === "insert") return n;
173
- let r = t.id;
174
- return typeof r == "string" && r.length > 0 ? r : null;
175
- }, de = (e, t, n, r) => {
176
- let { anchorColumn: i, targetColumn: a } = e, o = new Set(n.map((e) => String(e)));
177
- return (e) => {
178
- if (!Array.isArray(e)) return e;
179
- let n = [], s = /* @__PURE__ */ new Set(), c = !1;
180
- for (let r of e) {
181
- if (typeof r != "object" || !r) {
182
- n.push(r);
183
- continue;
184
- }
185
- let e = r;
186
- if (String(e[i]) !== t) {
187
- n.push(r);
188
- continue;
189
- }
190
- let l = String(e[a]);
191
- if (!o.has(l)) {
192
- c = !0;
193
- continue;
194
- }
195
- s.add(l), n.push(r);
196
- }
197
- let l = 0;
198
- for (let e of o) s.has(e) || (l += 1, c = !0, n.push({
199
- id: `${r}-link-${l}`,
200
- [i]: t,
201
- [a]: e,
202
- optimistic: !0
203
- }));
204
- return c ? n : e;
205
- };
206
- }, fe = (e, t, n) => {
207
- if (e.path !== void 0) return le(e, t, n);
208
- switch (e.op) {
209
- case "insert": {
210
- let r = e.shape ? {
211
- id: n,
212
- ...e.shape(t, n),
213
- optimistic: !0
214
- } : {
215
- id: n,
216
- ...t,
217
- optimistic: !0
218
- }, i = e.order ?? "prepend";
219
- return (e) => {
220
- let t = Array.isArray(e) ? e : [];
221
- return i === "append" ? [...t, r] : [r, ...t];
222
- };
223
- }
224
- case "update": {
225
- let n = V(e, t);
226
- if (n === null) return null;
227
- let r = e.shape, i = r ? (e) => ({
228
- ...r(t, e),
229
- id: e.id,
230
- optimistic: !0
231
- }) : (e) => ({
232
- ...e,
233
- ...t,
234
- optimistic: !0
235
- });
236
- return (e) => {
237
- if (!Array.isArray(e)) {
238
- let t = e;
239
- return typeof t != "object" || !t ? e : n.has(t.id) ? i(t) : e;
240
- }
241
- return e.map((e) => {
242
- let t = e;
243
- return n.has(t.id) ? i(t) : t;
244
- });
245
- };
246
- }
247
- case "delete": {
248
- let n = V(e, t);
249
- return n === null ? null : (e) => {
250
- if (!Array.isArray(e)) {
251
- let t = e;
252
- return typeof t != "object" || !t ? e : n.has(t.id) ? null : e;
253
- }
254
- return e.filter((e) => !n.has(e.id));
255
- };
256
- }
257
- }
258
- }, pe = 5e3, me = 15e3, he = 2e3, H = () => globalThis.document?.visibilityState === "hidden", U = class {
259
- entries = /* @__PURE__ */ new Map();
260
- intents = /* @__PURE__ */ new Map();
261
- tableRows = /* @__PURE__ */ new Map();
262
- anyListeners = /* @__PURE__ */ new Set();
263
- refreshing = /* @__PURE__ */ new WeakSet();
264
- successor;
265
- inactiveTtlMs;
266
- onChangeHook;
267
- errorBus;
268
- mirror;
269
- mergeCell;
270
- apiName;
271
- recoveryStuckMs;
272
- constructor(e = {}) {
273
- this.inactiveTtlMs = e.inactiveTtlMs ?? pe, this.onChangeHook = e.onChange, this.errorBus = e.errorBus, this.mirror = e.mirror, this.mergeCell = e.mergeCell, this.apiName = e.apiName, this.recoveryStuckMs = e.recoveryStuckMs ?? me;
274
- }
275
- diagnose(e, t, n = {}) {
276
- this.apiName !== void 0 && N(this.apiName, e, {
277
- tag: t.tag ?? "unknown",
278
- revision: t.state.baseRevision,
279
- subscribers: t.subscribers.size,
280
- attempt: t.recoveryAttempt,
281
- ...n
282
- });
283
- }
284
- subscribe(e, t, n, r, i = {}) {
285
- let a = P(e), o = typeof e[0] == "string" ? e[0] : void 0, s = this.entries.get(a);
286
- return s && s.evictionTimer !== null && (clearTimeout(s.evictionTimer), s.evictionTimer = null), s ? (s.fiber === null && (s.fetch = t, s.runtime = n, this.startFiber(s, t, n)), s.source === void 0 && i.source !== void 0 && (s.source = i.source, this.applyStandingIntents(s))) : (s = this.create(a, o, t, n, i.source), this.applyStandingIntents(s)), s.subscribers.add(r), {
287
- getSnapshot: () => this.getSnapshot(a),
288
- unsubscribe: () => this.unsubscribe(a, r)
289
- };
290
- }
291
- update(e, t, n) {
292
- let r = P(e), i = typeof e[0] == "string" ? e[0] : void 0, a = this.entries.get(r);
293
- a ||= this.createStub(r, i);
294
- let o = {
295
- id: ce(),
296
- mutationId: n,
297
- reducer: t
298
- };
299
- return a.state.patches.push(o), this.invalidate(a), this.successor?.update(e, t, n), {
300
- patchId: o.id,
301
- revert: () => this.revertPatch(r, o.id)
302
- };
303
- }
304
- forTag(e, t, n) {
305
- let r = 0;
306
- for (let i of this.entries.values()) {
307
- if (i.tag !== e) continue;
308
- let a = {
309
- id: `forTag-${++R}`,
310
- mutationId: n,
311
- reducer: t
312
- };
313
- i.state.patches.push(a), this.invalidate(i), r++;
314
- }
315
- return this.successor?.forTag(e, t, n), { patchedCount: r };
316
- }
317
- revertByMutation(e) {
318
- this.intents.delete(e);
319
- for (let t of this.entries.values()) {
320
- let n = t.state.patches.length;
321
- t.state.patches = t.state.patches.filter((t) => t.mutationId !== e), t.state.patches.length !== n && this.invalidate(t);
322
- }
323
- this.successor?.revertByMutation(e);
324
- }
325
- confirmByMutation(e) {
326
- this.intents.delete(e);
327
- let t = !1;
328
- for (let n of this.entries.values()) {
329
- let r = !1;
330
- for (let i of n.state.patches) i.mutationId === e && i.confirmed !== !0 && (i.confirmed = !0, r = !0, t = !0);
331
- if (r && !n.reconciliationScheduled) {
332
- n.reconciliationScheduled = !0;
333
- let e = n.generation;
334
- queueMicrotask(() => {
335
- n.reconciliationScheduled = !1, this.entries.get(n.key) === n && n.fetch !== null && n.runtime !== null && n.generation === e && n.state.patches.some((e) => e.confirmed === !0) && this.reissue(n, n.runtime, !1);
336
- });
337
- }
338
- }
339
- t && setTimeout(() => this.resyncUnechoedMutation(e), I), this.successor?.confirmByMutation(e);
340
- }
341
- resyncUnechoedMutation(e) {
342
- for (let t of this.entries.values()) {
343
- if (!t.state.patches.some((t) => t.mutationId === e && t.confirmed === !0)) continue;
344
- let n = t.tag ?? t.key, r = `optimistic patch for ${n} got no superseding server event within ${I}ms — the write is confirmed, so the preview is KEPT. Check the post-ACK primary subscription read and matching client/server versions.`;
345
- this.errorBus?.emit({
346
- source: "subscription",
347
- tag: n,
348
- error: Error(r),
349
- kind: "client",
350
- outcome: "not-applicable",
351
- ...t.traceId === void 0 ? {} : { traceId: t.traceId }
352
- }), console.warn(`[voltro] ${r}`, {
353
- mutationId: e,
354
- tag: n
355
- }), t.fetch !== null && t.runtime !== null && this.reissue(t, t.runtime, !1);
356
- }
357
- }
358
- autoApply(e, t, n, r = {}) {
359
- let i = {
360
- targets: e,
361
- input: t,
362
- opticId: `optimistic-${n}-${++R}`
363
- };
364
- r.standing === !0 && this.intents.set(n, i);
365
- for (let e of this.entries.values()) this.applyIntent(e, n, i);
366
- this.successor?.autoApply(e, t, n, r);
367
- }
368
- hasIntent(e) {
369
- return this.intents.has(e);
370
- }
371
- applyIntent(e, t, n) {
372
- if (e.source === void 0 || e.state.patches.some((e) => e.mutationId === t)) return;
373
- let { targets: r, input: i, opticId: a } = n, o = (t) => Array.isArray(e.source) ? e.source.includes(t) : e.source === t, s = !1;
374
- for (let n of r) {
375
- if (o(n.table)) {
376
- let r = !0;
377
- if (n.match !== void 0) try {
378
- r = n.match(e.state.base, i);
379
- } catch {
380
- r = !1;
381
- }
382
- if (r && !(e.computed && n.op === "insert" && n.path === void 0)) {
383
- let r = fe(n, i, a);
384
- r !== null && (e.state.patches.push({
385
- id: `${a}-${e.key}`,
386
- mutationId: t,
387
- reducer: r
388
- }), s = !0);
389
- }
390
- }
391
- let r = n.relations;
392
- if (r !== void 0 && !e.computed) for (let [c, l] of Object.entries(r)) {
393
- let r = i[c];
394
- if (!Array.isArray(r) || !o(l.junction)) continue;
395
- let u = ue(n, i, a);
396
- if (u === null) continue;
397
- let d = de(l, u, r.map((e) => String(e)), `${a}-${c}`);
398
- e.state.patches.push({
399
- id: `${a}-${c}-${e.key}`,
400
- mutationId: t,
401
- reducer: d
402
- }), s = !0;
403
- }
404
- }
405
- s && this.invalidate(e);
406
- }
407
- applyStandingIntents(e) {
408
- for (let [t, n] of this.intents) this.applyIntent(e, t, n);
409
- }
410
- revertPatch(e, t) {
411
- let n = this.entries.get(e);
412
- if (!n) return;
413
- let r = n.state.patches.length;
414
- n.state.patches = n.state.patches.filter((e) => e.id !== t), n.state.patches.length !== r && this.invalidate(n);
415
- }
416
- peek(e) {
417
- return this.getSnapshot(P(e));
418
- }
419
- getEntry(e) {
420
- return this.entries.get(P(e));
421
- }
422
- size() {
423
- return this.entries.size;
424
- }
425
- peekAll() {
426
- let e = [];
427
- for (let t of this.entries.values()) e.push({
428
- key: t.key,
429
- tag: t.tag,
430
- source: t.source,
431
- subscriberCount: t.subscribers.size,
432
- hasFiber: t.fiber !== null,
433
- evicting: t.evictionTimer !== null,
434
- recovery: {
435
- phase: t.recoveryPhase,
436
- attempt: t.recoveryAttempt,
437
- lastTransportFailureAt: t.lastTransportFailureAt,
438
- lastReissueAt: t.lastReissueAt,
439
- lastDeliveryAt: t.lastDeliveryAt,
440
- basePreserved: t.recoveryBasePreserved
441
- },
442
- snapshot: this.getSnapshot(t.key)
443
- });
444
- return e;
445
- }
446
- destroy(e) {
447
- for (let t of this.entries.values()) t.evictionTimer !== null && clearTimeout(t.evictionTimer), t.recoveryTimer !== null && clearTimeout(t.recoveryTimer), t.fiber && e.runFork(r.interrupt(t.fiber));
448
- this.entries.clear();
449
- }
450
- refreshAll(e) {
451
- for (let t of this.entries.values()) t.fetch && this.reissue(t, e);
452
- }
453
- refresh(e) {
454
- let t = this.entries.get(P(e));
455
- return !t?.fetch || !t.runtime || t.subscribers.size === 0 || this.refreshing.has(t) || t.state.base === void 0 && t.state.baseError === void 0 ? !1 : (this.refreshing.add(t), this.reissue(t, t.runtime, !1), !0);
456
- }
457
- resumeTransportFailures(e) {
458
- let t = 0;
459
- for (let n of this.entries.values()) if (n.resumeAfterTransportOpen) {
460
- if (n.fiber !== null || n.fetch === null || n.subscribers.size === 0) {
461
- this.diagnose("subscription.reissue-skipped", n, { reason: n.fiber === null ? n.fetch === null ? "missing-fetch" : "no-subscribers" : "fiber-running" });
462
- continue;
463
- }
464
- n.recoveryAttempt += 1, n.recoveryPhase = "awaiting-delivery", n.lastReissueAt = Date.now(), n.recoveryBasePreserved = n.state.base !== void 0, this.diagnose("subscription.reissue-started", n, { basePreserved: n.recoveryBasePreserved }), this.reissue(n, e, !1), this.armRecoveryWatchdog(n), t += 1;
465
- }
466
- return t;
467
- }
468
- armRecoveryWatchdog(e) {
469
- let t = e.recoveryAttempt;
470
- e.recoveryTimer !== null && clearTimeout(e.recoveryTimer);
471
- let n = 0, r = () => {
472
- let i = Date.now();
473
- e.recoveryTimer = setTimeout(() => {
474
- if (e.recoveryPhase !== "awaiting-delivery" || e.recoveryAttempt !== t) return;
475
- let a = Date.now() - i, o = Math.max(0, a - this.recoveryStuckMs), s = H();
476
- if (o > he || s) {
477
- n += 1, n === 1 && this.diagnose("subscription.recovery-timer-late", e, {
478
- waitedMs: a,
479
- timerLateMs: o,
480
- hidden: s
481
- }), r();
482
- return;
483
- }
484
- e.recoveryPhase = "stuck", e.recoveryTimer = null, this.diagnose("subscription.recovery-stuck", e, {
485
- waitedMs: a,
486
- deferrals: n,
487
- help: "run voltroDiagnostics.download() in the browser console"
488
- }), console.warn(`[voltro] subscription recovery stalled for ${e.tag ?? "unknown"} after ${a}ms. Run voltroDiagnostics.download() in the browser console and attach the JSON support snapshot.`), this.invalidate(e);
489
- }, this.recoveryStuckMs);
490
- };
491
- r();
492
- }
493
- reissue(e, t, n = !0) {
494
- if (!e.fetch) return;
495
- e.fiber && t.runFork(r.interrupt(e.fiber)), e.fiber = null;
496
- let i = e.state.patches, a = e.state.base, o = e.state.baseRevision, s = e.state.baseEmittedAt;
497
- e.state = this.freshState(), e.state.patches = i, n || (e.state.base = a, e.state.baseRevision = o, e.state.baseEmittedAt = s), this.startFiber(e, e.fetch, t);
498
- }
499
- seedStaleFrom(e) {
500
- e.successor = this;
501
- for (let [t, n] of e.intents) this.intents.set(t, n);
502
- let t = 0, n = /* @__PURE__ */ new Set();
503
- for (let r of e.entries.values()) {
504
- if (r.state.base === void 0 || this.entries.has(r.key)) continue;
505
- let e = {
506
- key: r.key,
507
- tag: r.tag,
508
- state: {
509
- base: r.state.base,
510
- baseRevision: r.state.baseRevision,
511
- baseEmittedAt: r.state.baseEmittedAt,
512
- baseError: void 0,
513
- patches: r.state.patches.map((e) => (e.confirmed === !0 && n.add(e.mutationId), { ...e })),
514
- cachedSnapshot: null
515
- },
516
- generation: 0,
517
- reconciliationScheduled: !1,
518
- primaryReads: r.primaryReads,
519
- subscribers: /* @__PURE__ */ new Set(),
520
- fiber: null,
521
- resumeAfterTransportOpen: !1,
522
- recoveryPhase: "steady",
523
- recoveryAttempt: 0,
524
- lastTransportFailureAt: void 0,
525
- lastReissueAt: void 0,
526
- lastDeliveryAt: void 0,
527
- recoveryBasePreserved: !1,
528
- recoveryTimer: null,
529
- evictionTimer: null,
530
- source: r.source,
531
- computed: r.computed,
532
- fetch: null,
533
- runtime: null
534
- };
535
- this.entries.set(e.key, e), this.scheduleEviction(e), t += 1;
536
- }
537
- for (let e of n) setTimeout(() => this.resyncUnechoedMutation(e), I);
538
- return t;
539
- }
540
- create(e, t, n, r, i) {
541
- let a = {
542
- key: e,
543
- tag: t,
544
- state: this.freshState(),
545
- generation: 0,
546
- reconciliationScheduled: !1,
547
- primaryReads: !1,
548
- subscribers: /* @__PURE__ */ new Set(),
549
- fiber: null,
550
- resumeAfterTransportOpen: !1,
551
- recoveryPhase: "steady",
552
- recoveryAttempt: 0,
553
- lastTransportFailureAt: void 0,
554
- lastReissueAt: void 0,
555
- lastDeliveryAt: void 0,
556
- recoveryBasePreserved: !1,
557
- recoveryTimer: null,
558
- evictionTimer: null,
559
- source: i,
560
- computed: !1,
561
- fetch: n,
562
- runtime: r
563
- };
564
- return this.entries.set(e, a), this.startFiber(a, n, r), this.mirror !== void 0 && this.mirror.load(e).then((t) => {
565
- t !== void 0 && this.entries.get(e) === a && a.state.base === void 0 && a.state.baseRevision === -1 && (a.state.base = t.data, a.state.baseRevision = t.revision, this.invalidate(a));
566
- }).catch(() => {}), a;
567
- }
568
- createStub(e, t) {
569
- let n = {
570
- key: e,
571
- tag: t,
572
- state: this.freshState(),
573
- generation: 0,
574
- reconciliationScheduled: !1,
575
- primaryReads: !1,
576
- subscribers: /* @__PURE__ */ new Set(),
577
- fiber: null,
578
- resumeAfterTransportOpen: !1,
579
- recoveryPhase: "steady",
580
- recoveryAttempt: 0,
581
- lastTransportFailureAt: void 0,
582
- lastReissueAt: void 0,
583
- lastDeliveryAt: void 0,
584
- recoveryBasePreserved: !1,
585
- recoveryTimer: null,
586
- evictionTimer: null,
587
- source: void 0,
588
- computed: !1,
589
- fetch: null,
590
- runtime: null
591
- };
592
- return this.entries.set(e, n), n;
593
- }
594
- freshState() {
595
- return {
596
- base: void 0,
597
- baseRevision: -1,
598
- baseEmittedAt: void 0,
599
- baseError: void 0,
600
- patches: [],
601
- cachedSnapshot: null
602
- };
603
- }
604
- startFiber(r, a, o) {
605
- let u = r.tag ?? r.key, f = ++r.generation, p = new Set(r.state.patches.filter((e) => e.confirmed === !0).map((e) => e.id));
606
- p.size > 0 && (r.primaryReads = !0);
607
- let g = r.primaryReads ? `reconcile-${++se}` : void 0;
608
- r.resumeAfterTransportOpen = !1;
609
- let v = a(), y = s.unwrapScoped(t.currentSpan.pipe(t.catchAll(() => t.succeed(void 0)), t.map((e) => (e && (r.traceId = e.traceId, _({
610
- traceId: e.traceId,
611
- spanId: e.spanId,
612
- source: "subscription",
613
- tag: u
614
- })), v)))).pipe(s.withSpan(`client.subscription.${u}`, { attributes: { "rpc.tag": u } })), b = g === void 0 && r.state.baseRevision >= 0 ? r.state.baseRevision : void 0, S = y.pipe(s.runForEach((e) => t.sync(() => {
615
- if (r.generation === f && this.entries.get(r.key) === r) {
616
- if (e._tag === "snapshot") r.state.base = e.data, r.state.baseRevision = e.revision, r.state.baseEmittedAt = void 0, r.state.baseError = void 0, e.computed === !0 && (r.computed = !0);
617
- else if (e._tag === "error") {
618
- let t = F(e.error);
619
- r.state.baseError = t, typeof e.revision == "number" && (r.state.baseRevision = e.revision), this.errorBus?.emit({
620
- source: "subscription",
621
- tag: r.tag ?? r.key,
622
- error: t,
623
- kind: "handler",
624
- outcome: "not-applicable",
625
- ...r.traceId === void 0 ? {} : { traceId: r.traceId }
626
- });
627
- } else {
628
- let t = r.state.base, n = this.mergeCell ?? m();
629
- Array.isArray(t) && (r.state.base = d(t, e.patch, n === void 0 ? {} : { mergeCell: n })), r.state.baseRevision = e.revision, r.state.baseEmittedAt = e.emittedAt, r.state.baseError = void 0;
630
- }
631
- this.refreshing.delete(r), e._tag !== "error" && (r.lastDeliveryAt = Date.now(), r.recoveryPhase !== "steady" && (r.recoveryTimer !== null && clearTimeout(r.recoveryTimer), r.recoveryTimer = null, this.diagnose("subscription.first-delivery", r, {
632
- delivery: e._tag,
633
- basePreserved: r.recoveryBasePreserved
634
- }), r.recoveryPhase = "steady", this.diagnose("subscription.health-cleared", r, { delivery: e._tag })), this.errorBus?.emitSuccess({
635
- source: "subscription",
636
- tag: r.tag ?? r.key
637
- })), L(e, g) && (r.state.patches = r.state.patches.filter((e) => !p.has(e.id))), this.mirror !== void 0 && e._tag !== "error" && this.mirror.save(r.key, {
638
- tag: r.tag,
639
- data: r.state.base,
640
- revision: r.state.baseRevision
641
- }).catch(() => {}), this.invalidate(r);
642
- }
643
- }))), C = o.runFork(g === void 0 ? b === void 0 ? S : S.pipe(t.locallyWith(l.currentHeaders, (e) => c.set(e, "voltro-resume-from", String(b)))) : S.pipe(t.locallyWith(l.currentHeaders, (e) => c.set(c.remove(e, "voltro-resume-from"), "voltro-reconcile", g))));
644
- r.fiber = C, C.addObserver((t) => {
645
- if (r.generation === f && this.entries.get(r.key) === r && (this.refreshing.delete(r), r.fiber === C && (r.fiber = null), n.isFailure(t) && !n.isInterrupted(t))) {
646
- let n = F(h(t.cause));
647
- r.state.baseRevision === -1 && (r.state.baseError = n, this.invalidate(r));
648
- let a = e.failureOption(t.cause), o = i.isSome(a) ? x(a.value, "handler") : "unknown";
649
- o === "transport" && (r.recoveryTimer !== null && clearTimeout(r.recoveryTimer), r.recoveryTimer = null, r.resumeAfterTransportOpen = !0, r.recoveryPhase = "awaiting-protocol", r.lastTransportFailureAt = Date.now(), this.diagnose("subscription.transport-failed", r)), this.errorBus && this.errorBus.emit({
650
- source: "subscription",
651
- tag: r.tag ?? r.key,
652
- error: n,
653
- kind: o,
654
- outcome: "not-applicable",
655
- ...r.traceId === void 0 ? {} : { traceId: r.traceId }
656
- });
657
- }
658
- });
659
- }
660
- invalidate(e) {
661
- e.state.cachedSnapshot = null, this.tableRows.clear();
662
- for (let t of e.subscribers) t();
663
- this.onChangeHook?.(e.key);
664
- for (let e of this.anyListeners) try {
665
- e();
666
- } catch {}
667
- }
668
- rowsOf(e) {
669
- let t = this.tableRows.get(e);
670
- if (t !== void 0) return t;
671
- let n = /* @__PURE__ */ new Map();
672
- for (let t of this.entries.values()) {
673
- if (t.source !== e && !(Array.isArray(t.source) && t.source.length === 1 && t.source[0] === e)) continue;
674
- let r = this.getSnapshot(t.key).data;
675
- if (Array.isArray(r)) for (let e of r) {
676
- if (typeof e != "object" || !e) continue;
677
- let t = e.id;
678
- if (t === void 0) continue;
679
- let r = n.get(t);
680
- (r === void 0 || r.optimistic === !0 || e.optimistic !== !0) && n.set(t, e);
681
- }
682
- }
683
- let r = [...n.values()];
684
- return this.tableRows.set(e, r), r;
685
- }
686
- subscribeAny(e) {
687
- return this.anyListeners.add(e), () => {
688
- this.anyListeners.delete(e);
689
- };
690
- }
691
- getSnapshot(e) {
692
- let t = this.entries.get(e);
693
- if (!t) return oe;
694
- if (t.state.cachedSnapshot !== null) return t.state.cachedSnapshot;
695
- let n = t.state.base;
696
- if (n !== void 0 && t.state.patches.length > 0) for (let e of t.state.patches) n = e.reducer(n);
697
- let r = {
698
- data: n,
699
- revision: t.state.baseRevision,
700
- emittedAt: t.state.baseEmittedAt,
701
- error: t.state.baseError,
702
- pendingPatches: t.state.patches.length
703
- };
704
- return t.state.cachedSnapshot = r, r;
705
- }
706
- unsubscribe(e, t) {
707
- let n = this.entries.get(e);
708
- n && (n.subscribers.delete(t), !(n.subscribers.size > 0) && this.scheduleEviction(n));
709
- }
710
- scheduleEviction(e) {
711
- e.evictionTimer !== null && clearTimeout(e.evictionTimer), e.evictionTimer = setTimeout(() => {
712
- let n = this.entries.get(e.key);
713
- n && n.subscribers.size === 0 && (n.recoveryTimer !== null && clearTimeout(n.recoveryTimer), n.fiber && t.runFork(r.interrupt(n.fiber)), this.entries.delete(e.key));
714
- }, this.inactiveTtlMs);
715
- }
716
- }, ge = class extends U {
717
- subscribe(e, t, n, r, i = {}) {
718
- return {
719
- getSnapshot: () => this.peek(e),
720
- unsubscribe: () => {}
721
- };
722
- }
723
- }, W = /* @__PURE__ */ new Set(), G = (e) => {
724
- for (let t of W) try {
725
- t(e);
726
- } catch {}
727
- }, _e = (e) => (W.add(e), () => {
728
- W.delete(e);
729
- }), ve = (e, t) => {
730
- G({
731
- error: e,
732
- source: "manual",
733
- ...t ? { context: t } : {}
734
- });
735
- }, ye = class {
736
- #e = /* @__PURE__ */ new Set();
737
- #t = /* @__PURE__ */ new Set();
738
- on(e) {
739
- return this.#e.add(e), () => {
740
- this.#e.delete(e);
741
- };
742
- }
743
- emit(e) {
744
- for (let t of this.#e) try {
745
- t(e);
746
- } catch {}
747
- G({
748
- error: e.error,
749
- source: `rpc.${e.source}`,
750
- context: {
751
- rpcTag: e.tag,
752
- rpcErrorKind: e.kind,
753
- ...e.affectsTransport === void 0 ? {} : { affectsTransport: e.affectsTransport },
754
- ...e.outcome === void 0 ? {} : { rpcCallOutcome: e.outcome },
755
- ...e.traceId === void 0 ? {} : { traceId: e.traceId }
756
- }
757
- });
758
- }
759
- onSuccess(e) {
760
- return this.#t.add(e), () => {
761
- this.#t.delete(e);
762
- };
763
- }
764
- emitSuccess(e) {
765
- for (let t of this.#t) try {
766
- t(e);
767
- } catch {}
768
- }
769
- }, be = Symbol.for("voltro.client.connectivity"), xe = () => {
770
- let e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Set(), n = () => {
771
- for (let e of t) try {
772
- e();
773
- } catch {}
774
- };
775
- return {
776
- online: (t) => {
777
- let n = t === void 0 ? [...e.values()].flatMap((e) => [...e]) : [...e.get(t) ?? []];
778
- if (n.length !== 0) return n.some((e) => e.connected());
779
- },
780
- subscribe: (e) => (t.add(e), () => {
781
- t.delete(e);
782
- }),
783
- register: (t, r) => {
784
- let i = e.get(t) ?? /* @__PURE__ */ new Set();
785
- return i.add(r), e.set(t, i), n(), () => {
786
- i.delete(r), i.size === 0 && e.delete(t), n();
787
- };
788
- },
789
- notify: n
790
- };
791
- }, K = () => {
792
- let e = globalThis;
793
- return e[be] ??= xe();
794
- }, Se = (e, t) => K().register(e, { connected: t }), Ce = () => {
795
- K().notify();
796
- }, we = () => K(), q = (e, t, n, r) => Math.min(r, Math.max(n, e === void 0 || !Number.isFinite(e) ? t : e)), Te = (e) => {
797
- let t = Math.trunc(q(e?.maxMs, 5e3, 50, 6e4));
798
- return {
799
- initialMs: Math.trunc(q(e?.initialMs, 0, 0, t)),
800
- thenMs: Math.trunc(q(e?.thenMs, 500, 50, t)),
801
- factor: q(e?.factor, 1.5, 1, 10),
802
- maxMs: t
803
- };
804
- }, J = (e, t) => t <= 1 ? e.initialMs : Math.min(e.maxMs, e.thenMs * e.factor ** (t - 2)), Ee = (e, n) => {
805
- let r = 0;
806
- return {
807
- reset: () => {
808
- r = 0;
809
- },
810
- schedule: o.forever.pipe(o.addDelayEffect(() => t.sync(() => {
811
- let t = J(e, ++r);
812
- return n(r, t), t;
813
- })))
814
- };
815
- }, Y = (e, t, n, r) => e === void 0 || !Number.isFinite(e) ? t : Math.min(r, Math.max(n, Math.trunc(e))), X = (e) => ({
816
- graceMs: Y(e?.graceMs, 1500, 0, 3e4),
817
- retry: Te(e?.retry),
818
- maxPendingMutations: Y(e?.maxPendingMutations, 32, 0, 256),
819
- replayWindowMs: Y(e?.replayWindowMs, 1e4, 250, 6e4),
820
- capabilitiesUrl: e?.capabilitiesUrl
821
- }), Z = Object.freeze({
822
- rawTransport: "connected",
823
- status: "connected",
824
- failureCount: 0,
825
- lastFailureAt: void 0,
826
- failedSubscriptions: Object.freeze([]),
827
- pendingMutationReplays: 0,
828
- mutationReplay: "unknown"
829
- }), Q = class {
830
- #e;
831
- #t;
832
- #n;
833
- #r = /* @__PURE__ */ new Set();
834
- #i = /* @__PURE__ */ new Set();
835
- #a = /* @__PURE__ */ new Set();
836
- #o = /* @__PURE__ */ new Set();
837
- #s = /* @__PURE__ */ new Set();
838
- #c = "connected";
839
- #l = "connected";
840
- #u = 0;
841
- #d;
842
- #f = 0;
843
- #p = "unknown";
844
- #m = -1;
845
- #h = 0;
846
- #g = new AbortController();
847
- #_;
848
- #v;
849
- #y = !1;
850
- #b;
851
- #x;
852
- constructor(e, t, n) {
853
- this.#e = e, this.#t = X(t), this.#n = {
854
- now: n?.now ?? Date.now,
855
- setTimeout: n?.setTimeout ?? ((e, t) => setTimeout(e, t)),
856
- clearTimeout: n?.clearTimeout ?? ((e) => clearTimeout(e))
857
- }, this.#b = this.#S(), this.#x = Se(e, () => this.#c === "connected");
858
- }
859
- get options() {
860
- return this.#t;
861
- }
862
- get subjectSignal() {
863
- return this.#g.signal;
864
- }
865
- getSnapshot = () => this.#b;
866
- getServerSnapshot = () => Z;
867
- subscribe = (e) => (this.#r.add(e), () => {
868
- this.#r.delete(e);
869
- });
870
- onVisibleError(e) {
871
- return this.#i.add(e), () => {
872
- this.#i.delete(e);
873
- };
874
- }
875
- #S() {
876
- return {
877
- rawTransport: this.#c,
878
- status: this.#l,
879
- failureCount: this.#u,
880
- lastFailureAt: this.#d,
881
- failedSubscriptions: [...this.#s].sort(),
882
- pendingMutationReplays: this.#f,
883
- mutationReplay: this.#p
884
- };
885
- }
886
- #C() {
887
- let e = this.#b.rawTransport !== this.#c;
888
- this.#b = this.#S();
889
- for (let e of this.#r) try {
890
- e();
891
- } catch {}
892
- e && Ce(), N(this.#e, "recovery.state", {
893
- rawTransport: this.#b.rawTransport,
894
- status: this.#b.status,
895
- failureCount: this.#b.failureCount,
896
- failedSubscriptions: this.#b.failedSubscriptions.length,
897
- pendingMutationReplays: this.#b.pendingMutationReplays,
898
- mutationReplay: this.#b.mutationReplay
899
- });
900
- }
901
- #w(e) {
902
- for (let t of this.#i) try {
903
- t(e);
904
- } catch {}
905
- }
906
- #T() {
907
- if (this.#l === "connected") {
908
- if (this.#l = this.#t.graceMs === 0 ? "degraded" : "recovering", this.#t.graceMs === 0) {
909
- this.#v !== void 0 && (this.#y = !0, this.#w(this.#v));
910
- return;
911
- }
912
- this.#_ = this.#n.setTimeout(() => {
913
- this.#_ = void 0, this.#l === "recovering" && (this.#l = "degraded", this.#v !== void 0 && (this.#y = !0, this.#w(this.#v)), this.#C());
914
- }, this.#t.graceMs);
915
- }
916
- }
917
- #E() {
918
- this.#c !== "connected" || this.#s.size > 0 || (this.#_ !== void 0 && (this.#n.clearTimeout(this.#_), this.#_ = void 0), this.#l = "connected", this.#u = 0, this.#v = void 0, this.#y = !1);
919
- }
920
- transportIssue() {
921
- this.#c = "disconnected", this.#u += 1, this.#d = this.#n.now(), this.#T(), this.#C();
922
- }
923
- protocolReady() {
924
- this.#c = "connected";
925
- for (let e of this.#a) e();
926
- this.#a.clear(), this.#E(), this.#C();
927
- }
928
- observeError(e) {
929
- if (e.kind !== "transport" || e.affectsTransport !== !1) {
930
- if (e.kind !== "transport") {
931
- this.#w(e);
932
- return;
933
- }
934
- this.#v === void 0 && (this.#v = e), e.source === "subscription" && this.#s.add(e.tag), this.#c !== "disconnected" && (this.#c = "disconnected", this.#u += 1, this.#d = this.#n.now()), this.#T(), this.#l === "degraded" && !this.#y && (this.#y = !0, this.#w(e)), this.#C();
935
- }
936
- }
937
- observeSuccess(e) {
938
- e.source === "subscription" && this.#s.delete(e.tag), this.#E(), this.#C();
939
- }
940
- reportExternalSuccess() {
941
- this.#c = "connected";
942
- for (let e of this.#a) e();
943
- this.#a.clear(), this.#E(), this.#C();
944
- }
945
- resetForSubjectChange(e = 0) {
946
- this.#_ !== void 0 && this.#n.clearTimeout(this.#_), this.#_ = void 0, this.#c = "disconnected", this.#l = "connected", this.#u = 0, this.#d = void 0, this.#s.clear(), this.#v = void 0, this.#y = !1, this.#p = "unknown", this.#h += 1;
947
- let t = this.#g;
948
- this.#g = new AbortController(), t.abort(), this.#m = e;
949
- for (let e of this.#o) e(!1);
950
- this.#o.clear();
951
- for (let e of this.#a) e();
952
- this.#a.clear(), this.#T(), this.#C();
953
- }
954
- setMutationReplaySupported(e, t = 0) {
955
- if (!(t < this.#m)) {
956
- this.#m = t, this.#p = e ? "enabled" : "disabled";
957
- for (let t of this.#o) t(e);
958
- this.#o.clear(), this.#C();
959
- }
960
- }
961
- replayWindowRemaining(e) {
962
- return Math.max(0, this.#t.replayWindowMs - (this.#n.now() - e));
963
- }
964
- waitForMutationReplaySupport(e) {
965
- if (this.#p !== "unknown") return Promise.resolve(this.#p === "enabled");
966
- let t = this.#t.replayWindowMs - (this.#n.now() - e);
967
- return t <= 0 ? Promise.resolve(!1) : new Promise((e) => {
968
- let n = !1, r, i = (t) => {
969
- n || (n = !0, this.#o.delete(i), r !== void 0 && this.#n.clearTimeout(r), e(t));
970
- };
971
- this.#o.add(i), r = this.#n.setTimeout(() => i(!1), t);
972
- });
973
- }
974
- reserveMutationReplay(e) {
975
- if (this.#p === "disabled" || this.#n.now() - e > this.#t.replayWindowMs || this.#f >= this.#t.maxPendingMutations) return;
976
- this.#f += 1, this.#C();
977
- let t = this.#h, n = !1;
978
- return {
979
- isValid: () => !n && t === this.#h && this.#p === "enabled" && this.#c === "connected" && this.#n.now() - e <= this.#t.replayWindowMs,
980
- release: () => {
981
- n || (n = !0, --this.#f, this.#C());
982
- }
983
- };
984
- }
985
- waitForProtocolReady(e) {
986
- if (this.#c === "connected") return Promise.resolve(!0);
987
- let t = this.#t.replayWindowMs - (this.#n.now() - e);
988
- return t <= 0 ? Promise.resolve(!1) : new Promise((e) => {
989
- let n = !1, r = (t) => {
990
- n || (n = !0, this.#a.delete(i), this.#n.clearTimeout(a), e(t && this.#c === "connected"));
991
- }, i = () => r(!0);
992
- this.#a.add(i);
993
- let a = this.#n.setTimeout(() => r(!1), t);
994
- });
995
- }
996
- dispose() {
997
- this.#x(), this.#g.abort(), this.#_ !== void 0 && this.#n.clearTimeout(this.#_), this.#_ = void 0;
998
- for (let e of this.#a) e();
999
- this.#a.clear();
1000
- for (let e of this.#o) e(!1);
1001
- this.#o.clear(), this.#r.clear(), this.#i.clear();
1002
- }
1003
- }, $ = /* @__PURE__ */ new WeakMap(), De = (e, t, n) => {
1004
- let r = $.get(e);
1005
- if (r !== void 0) return r;
1006
- let i = new Q(t, n);
1007
- return e.on((e) => i.observeError(e)), typeof e.onSuccess == "function" && e.onSuccess((e) => i.observeSuccess(e)), $.set(e, i), i;
1008
- }, Oe = (e, t) => {
1009
- let n = t.split("."), r = e;
1010
- for (let e = 0; e < n.length; e++) {
1011
- let i = n[e];
1012
- if (r === null || typeof r != "object" && typeof r != "function") throw Error(`@voltro/client: rpc tag ${JSON.stringify(t)} not reachable on client. Stopped at segment ${JSON.stringify(i)}. The api's descriptor map is likely out of sync with the rpc group; restart \`voltro dev\` to regenerate.`);
1013
- let a = r;
1014
- if (i in a) {
1015
- r = a[i];
1016
- continue;
1017
- }
1018
- let o = n.slice(e).join(".");
1019
- if (o in a) {
1020
- r = a[o];
1021
- break;
1022
- }
1023
- throw Error(`@voltro/client: rpc tag ${JSON.stringify(t)} not reachable on client. Stopped at segment ${JSON.stringify(i)}. The api's descriptor map is likely out of sync with the rpc group; restart \`voltro dev\` to regenerate.`);
1024
- }
1025
- if (typeof r != "function") throw Error(`@voltro/client: rpc tag ${JSON.stringify(t)} did not resolve to a callable method.`);
1026
- return r;
1027
- }, ke = async (t, r, a) => {
1028
- let o = await t.runPromiseExit(r, a?.signal === void 0 ? void 0 : { signal: a.signal });
1029
- if (n.isSuccess(o)) return o.value;
1030
- let s = h(o.cause), c = e.failureOption(o.cause);
1031
- throw b(s, i.isSome(c) ? x(c.value, "handler") : "unknown");
1032
- };
1033
- //#endregion
1034
- export { m as A, ae as C, v as D, _ as E, h as O, N as S, x as T, L as _, De as a, M as b, J as c, G as d, ve as f, P as g, U as h, Q as i, p as k, we as l, ge as m, Oe as n, X as o, _e as p, Z as r, Ee as s, ke as t, ye as u, ie as v, b as w, ne as x, re as y };