@effect-app/vue 4.0.0-beta.31 → 4.0.0-beta.310

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 (141) hide show
  1. package/CHANGELOG.md +2306 -0
  2. package/dist/atomQuery.d.ts +119 -0
  3. package/dist/atomQuery.d.ts.map +1 -0
  4. package/dist/atomQuery.js +422 -0
  5. package/dist/commander.d.ts +634 -0
  6. package/dist/commander.d.ts.map +1 -0
  7. package/dist/commander.js +1070 -0
  8. package/dist/{experimental/confirm.d.ts → confirm.d.ts} +6 -4
  9. package/dist/confirm.d.ts.map +1 -0
  10. package/dist/confirm.js +26 -0
  11. package/dist/dependencyMetadata.d.ts +18 -0
  12. package/dist/dependencyMetadata.d.ts.map +1 -0
  13. package/dist/dependencyMetadata.js +67 -0
  14. package/dist/errorReporter.d.ts +6 -4
  15. package/dist/errorReporter.d.ts.map +1 -1
  16. package/dist/errorReporter.js +14 -19
  17. package/dist/form.d.ts +14 -5
  18. package/dist/form.d.ts.map +1 -1
  19. package/dist/form.js +46 -13
  20. package/dist/internal/tanstackQuery.d.ts +9 -0
  21. package/dist/internal/tanstackQuery.d.ts.map +1 -0
  22. package/dist/internal/tanstackQuery.js +200 -0
  23. package/dist/intl.d.ts +15 -0
  24. package/dist/intl.d.ts.map +1 -0
  25. package/dist/intl.js +9 -0
  26. package/dist/lib.d.ts +7 -9
  27. package/dist/lib.d.ts.map +1 -1
  28. package/dist/lib.js +35 -10
  29. package/dist/liveQueryInvalidation.d.ts +16 -0
  30. package/dist/liveQueryInvalidation.d.ts.map +1 -0
  31. package/dist/liveQueryInvalidation.js +115 -0
  32. package/dist/makeClient.d.ts +236 -336
  33. package/dist/makeClient.d.ts.map +1 -1
  34. package/dist/makeClient.js +417 -408
  35. package/dist/makeContext.d.ts.map +1 -1
  36. package/dist/makeIntl.d.ts.map +1 -1
  37. package/dist/{experimental/makeUseCommand.d.ts → makeUseCommand.d.ts} +4 -3
  38. package/dist/makeUseCommand.d.ts.map +1 -0
  39. package/dist/makeUseCommand.js +13 -0
  40. package/dist/mutate.d.ts +109 -40
  41. package/dist/mutate.d.ts.map +1 -1
  42. package/dist/mutate.js +224 -58
  43. package/dist/query.d.ts +158 -45
  44. package/dist/query.d.ts.map +1 -1
  45. package/dist/query.js +380 -122
  46. package/dist/routeParams.d.ts +4 -4
  47. package/dist/routeParams.d.ts.map +1 -1
  48. package/dist/routeParams.js +4 -3
  49. package/dist/runtime.d.ts +1 -14
  50. package/dist/runtime.d.ts.map +1 -1
  51. package/dist/runtime.js +2 -26
  52. package/dist/suspense.d.ts +19 -0
  53. package/dist/suspense.d.ts.map +1 -0
  54. package/dist/suspense.js +38 -0
  55. package/dist/toast.d.ts +2 -0
  56. package/dist/toast.d.ts.map +1 -0
  57. package/dist/toast.js +2 -0
  58. package/dist/withToast.d.ts +2 -0
  59. package/dist/withToast.d.ts.map +1 -0
  60. package/dist/withToast.js +2 -0
  61. package/docs/atom-query-api-redesign.md +207 -0
  62. package/docs/query-key-invalidation.md +106 -0
  63. package/examples/streamMutation.ts +72 -0
  64. package/package.json +36 -95
  65. package/src/atomQuery.ts +588 -0
  66. package/src/commander.ts +3413 -0
  67. package/src/{experimental/confirm.ts → confirm.ts} +13 -15
  68. package/src/dependencyMetadata.ts +84 -0
  69. package/src/errorReporter.ts +65 -75
  70. package/src/form.ts +61 -18
  71. package/src/index.ts +7 -7
  72. package/src/internal/tanstackQuery.ts +304 -0
  73. package/src/intl.ts +12 -0
  74. package/src/lib.ts +49 -21
  75. package/src/liveQueryInvalidation.ts +138 -0
  76. package/src/makeClient.ts +971 -1159
  77. package/src/makeIntl.ts +2 -2
  78. package/src/{experimental/makeUseCommand.ts → makeUseCommand.ts} +9 -6
  79. package/src/mutate.ts +396 -153
  80. package/src/query.ts +763 -260
  81. package/src/routeParams.ts +7 -7
  82. package/src/runtime.ts +1 -31
  83. package/src/suspense.ts +43 -0
  84. package/src/toast.ts +1 -0
  85. package/src/withToast.ts +1 -0
  86. package/test/Mutation.test.ts +181 -24
  87. package/test/dependencyInvalidation.test.ts +397 -0
  88. package/test/dist/dependencyInvalidation.test.d.ts.map +1 -0
  89. package/test/dist/form.test.d.ts.map +1 -1
  90. package/test/dist/interrupt-refetch-repro.test.d.ts.map +1 -0
  91. package/test/dist/lib.test.d.ts.map +1 -0
  92. package/test/dist/liveQueryInvalidation.test.d.ts.map +1 -0
  93. package/test/dist/query-span.test.d.ts.map +1 -0
  94. package/test/dist/queryOptions.test.d.ts.map +1 -0
  95. package/test/dist/streamFinal.test.d.ts.map +1 -0
  96. package/test/dist/streamFn.test.d.ts.map +1 -0
  97. package/test/dist/stubs.d.ts +3756 -122
  98. package/test/dist/stubs.d.ts.map +1 -1
  99. package/test/dist/stubs.js +192 -32
  100. package/test/dist/suspense-regression.test.d.ts.map +1 -0
  101. package/test/dist/suspense.test.d.ts.map +1 -0
  102. package/test/form-validation-errors.test.ts +25 -20
  103. package/test/form.test.ts +22 -3
  104. package/test/interrupt-refetch-repro.test.ts +623 -0
  105. package/test/lib.test.ts +240 -0
  106. package/test/liveQueryInvalidation.test.ts +108 -0
  107. package/test/makeClient.test.ts +437 -38
  108. package/test/query-span.test.ts +92 -0
  109. package/test/queryOptions.test.ts +88 -0
  110. package/test/streamFinal.test.ts +64 -0
  111. package/test/streamFn.test.ts +457 -0
  112. package/test/stubs.ts +228 -43
  113. package/test/suspense-regression.test.ts +168 -0
  114. package/test/suspense.test.ts +160 -0
  115. package/tsconfig.examples.json +20 -0
  116. package/tsconfig.json +9 -2
  117. package/tsconfig.src.json +34 -34
  118. package/tsconfig.test.json +2 -2
  119. package/vitest.config.ts +5 -5
  120. package/dist/experimental/commander.d.ts +0 -359
  121. package/dist/experimental/commander.d.ts.map +0 -1
  122. package/dist/experimental/commander.js +0 -557
  123. package/dist/experimental/confirm.d.ts.map +0 -1
  124. package/dist/experimental/confirm.js +0 -28
  125. package/dist/experimental/intl.d.ts +0 -16
  126. package/dist/experimental/intl.d.ts.map +0 -1
  127. package/dist/experimental/intl.js +0 -5
  128. package/dist/experimental/makeUseCommand.d.ts.map +0 -1
  129. package/dist/experimental/makeUseCommand.js +0 -13
  130. package/dist/experimental/toast.d.ts +0 -47
  131. package/dist/experimental/toast.d.ts.map +0 -1
  132. package/dist/experimental/toast.js +0 -41
  133. package/dist/experimental/withToast.d.ts +0 -25
  134. package/dist/experimental/withToast.d.ts.map +0 -1
  135. package/dist/experimental/withToast.js +0 -45
  136. package/eslint.config.mjs +0 -24
  137. package/src/experimental/commander.ts +0 -1835
  138. package/src/experimental/intl.ts +0 -9
  139. package/src/experimental/toast.ts +0 -66
  140. package/src/experimental/withToast.ts +0 -99
  141. package/tsconfig.json.bak +0 -12
@@ -0,0 +1,588 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /**
3
+ * Shared atom core for the query/mutation engine (used by query.ts + mutate.ts).
4
+ *
5
+ * Replaces the @tanstack/vue-query engine with Effect `Atom`, keeping the public
6
+ * `.query()/.suspense()/.mutate()` contract unchanged:
7
+ * - cache identity = the atom reference (one per [handler, input], via Atom.family)
8
+ * - invalidation key = reactivity keys (= the app's existing namespace query keys)
9
+ * - SWR + focus = Atom.swr (+ windowFocusSignal)
10
+ * - gcTime = Atom.setIdleTTL / Atom.keepAlive
11
+ * - retry = Effect.retry inside the atom effect
12
+ *
13
+ * Built over the app's RPC client through the existing `RequestHandlerWithInput`
14
+ * abstraction (mirrors AtomRpc's recipe; see docs/atom-query-plan.md).
15
+ */
16
+ import { defaultRegistry } from "@effect/atom-vue"
17
+ import { DataDependencies, makeQueryKey } from "effect-app/client"
18
+ import type { ClientForOptions } from "effect-app/client/clientFor"
19
+ import { ServiceUnavailableError } from "effect-app/client/errors"
20
+ import * as Effect from "effect-app/Effect"
21
+ import * as Option from "effect-app/Option"
22
+ import * as S from "effect-app/Schema"
23
+ import * as Cause from "effect/Cause"
24
+ import * as Duration from "effect/Duration"
25
+ import * as Equal from "effect/Equal"
26
+ import * as Hash from "effect/Hash"
27
+ import type * as Layer from "effect/Layer"
28
+ import * as Ref from "effect/Ref"
29
+ import * as Stream from "effect/Stream"
30
+ import type * as Tracer from "effect/Tracer"
31
+ import { isHttpClientError } from "effect/unstable/http/HttpClientError"
32
+ import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"
33
+ import * as Atom from "effect/unstable/reactivity/Atom"
34
+ import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry"
35
+ import { clearQueryReadDependencies, getQueryReadDependencies, type QueryInvalidationMode, registerQueryInvalidationMode, setQueryReadDependencies } from "./dependencyMetadata.ts"
36
+ import { reportRuntimeError } from "./lib.ts"
37
+ import { beginLiveQueryFetch, endLiveQueryFetch, type LiveQueryOptions, registerLiveQuery } from "./liveQueryInvalidation.ts"
38
+
39
+ /** All non-empty prefixes of a key, longest last. `[a,b,c]` -> `[[a],[a,b],[a,b,c]]`. */
40
+ const prefixesOf = (key: ReadonlyArray<unknown>): ReadonlyArray<ReadonlyArray<unknown>> =>
41
+ key.map((_, i) => key.slice(0, i + 1))
42
+
43
+ const uniqueKeys = (keys: ReadonlyArray<ReadonlyArray<unknown>>): ReadonlyArray<ReadonlyArray<unknown>> => {
44
+ const out: Array<ReadonlyArray<unknown>> = []
45
+ const seen = new Set<number>()
46
+ for (const key of keys) {
47
+ const hash = Hash.hash(key)
48
+ if (seen.has(hash)) continue
49
+ seen.add(hash)
50
+ out.push(key)
51
+ }
52
+ return out
53
+ }
54
+
55
+ // --- awaitable invalidation -------------------------------------------------------------------
56
+ // keyHash -> live query atoms registered under that key. A query atom is tracked while it is alive
57
+ // in the registry (mounted OR cached within idle-ttl) and removed on GC, so invalidation reaches
58
+ // cached-but-unmounted queries too (e.g. a list you navigated away from).
59
+ const keyAtoms = new Map<number, Set<Atom.Atom<AsyncResult.AsyncResult<any, any>>>>()
60
+
61
+ const trackByKeys =
62
+ (keys: ReadonlyArray<ReadonlyArray<unknown>>) =>
63
+ <A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>): Atom.Atom<AsyncResult.AsyncResult<A, E>> =>
64
+ Atom.transform(atom, (get) => {
65
+ for (const key of keys) {
66
+ const h = Hash.hash(key)
67
+ let set = keyAtoms.get(h)
68
+ if (!set) keyAtoms.set(h, set = new Set())
69
+ set.add(atom)
70
+ get.addFinalizer(() => {
71
+ set.delete(atom)
72
+ if (set.size === 0) keyAtoms.delete(h)
73
+ })
74
+ }
75
+ return get(atom)
76
+ }, { initialValueTarget: atom })
77
+
78
+ const trackWritableByKeys =
79
+ (keys: ReadonlyArray<ReadonlyArray<unknown>>) =>
80
+ <A, E, W>(atom: Atom.Writable<AsyncResult.AsyncResult<A, E>, W>): Atom.Writable<AsyncResult.AsyncResult<A, E>, W> => {
81
+ const tracked = trackByKeys(keys)(atom)
82
+ return Atom.writable(
83
+ (get) => get(tracked),
84
+ (ctx, value) => ctx.set(atom, value),
85
+ (refresh) => refresh(tracked)
86
+ )
87
+ }
88
+
89
+ /**
90
+ * Keep a query's recorded read-dependencies registered for as long as it is alive, and drop them when
91
+ * its atom is GC'd (mirrors `trackByKeys`).
92
+ *
93
+ * `recordReads` only stores the deps on an actual fetch. A query that unmounts and later remounts from
94
+ * cache (within idle-ttl) does NOT re-run the handler, so its registry entry — cleared by the previous
95
+ * teardown's finalizer — would never be restored, and a subsequent mutation could not derive it as an
96
+ * invalidation target. Re-asserting the last-known reads on every (re)subscribe closes that gap, so the
97
+ * registry mirrors the live query even across cache-hit remounts.
98
+ */
99
+ const trackReadDependencies =
100
+ (key: ReadonlyArray<unknown>, getReads: () => DataDependencies.DataDependencies) =>
101
+ <A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>): Atom.Atom<AsyncResult.AsyncResult<A, E>> =>
102
+ Atom.transform(atom, (get) => {
103
+ const reads = getReads()
104
+ if (DataDependencies.isNonEmpty(reads)) setQueryReadDependencies(key, reads)
105
+ get.addFinalizer(() => clearQueryReadDependencies(key))
106
+ return get(atom)
107
+ }, { initialValueTarget: atom })
108
+
109
+ const atomsForKeys = (keys: ReadonlyArray<unknown>): ReadonlyArray<Atom.Atom<AsyncResult.AsyncResult<any, any>>> => {
110
+ const atoms = new Set<Atom.Atom<AsyncResult.AsyncResult<any, any>>>()
111
+ for (const key of keys) {
112
+ const set = keyAtoms.get(Hash.hash(key))
113
+ if (set) { for (const a of set) atoms.add(a) }
114
+ }
115
+ return [...atoms]
116
+ }
117
+
118
+ /** Refresh registered query atoms without making the triggering mutation await them. */
119
+ export const invalidateSoft = (keys: ReadonlyArray<unknown>): Effect.Effect<void> =>
120
+ Effect.gen(function*() {
121
+ const atoms = atomsForKeys(keys)
122
+ yield* Effect.forEach(atoms, captureAtomQueryParentSpan, { discard: true, concurrency: "inherit" })
123
+ if (atoms.length === 0) return
124
+ yield* Effect.forEach(atoms, (atom) => Effect.sync(() => defaultRegistry.refresh(atom)), {
125
+ discard: true
126
+ })
127
+ })
128
+
129
+ /**
130
+ * Invalidate the given keys and AWAIT the result. `keyAtoms` resolves all matching hierarchical
131
+ * keys to a deduplicated set of query atoms. Refresh that set directly: sending the whole key set
132
+ * through `Reactivity.invalidate` invokes one atom's registered callback once per matching key,
133
+ * repeatedly superseding the same fetch when a mutation carries many row/prefix keys.
134
+ *
135
+ * Resolves once the affected queries have settled, so a mutation can `yield*` this and know the
136
+ * affected queries are fresh. (The await reads via the module-global default registry — the one the
137
+ * vue composables resolve via `injectRegistry`'s fallback.)
138
+ */
139
+ export const invalidateAndAwait = (keys: ReadonlyArray<unknown>): Effect.Effect<void> =>
140
+ Effect.gen(function*() {
141
+ const atoms = atomsForKeys(keys)
142
+ yield* Effect.forEach(atoms, captureAtomQueryParentSpan, { discard: true, concurrency: "inherit" })
143
+ if (atoms.length === 0) return
144
+ yield* Effect.forEach(atoms, (atom) => Effect.sync(() => defaultRegistry.refresh(atom)), {
145
+ discard: true
146
+ })
147
+ yield* Effect.forEach(atoms, (a) => awaitAtomResult(defaultRegistry, a).pipe(Effect.exit))
148
+ })
149
+
150
+ const isPlainObject = (o: unknown): o is Record<string, unknown> => {
151
+ if (typeof o !== "object" || o === null) return false
152
+ const proto = Object.getPrototypeOf(o)
153
+ return proto === Object.prototype || proto === null
154
+ }
155
+
156
+ /**
157
+ * Structural sharing (tanstack `replaceEqualDeep`): walk `next` against `prev` and reuse `prev`'s
158
+ * reference for any unchanged array/object sub-tree, so unchanged data keeps referential identity
159
+ * (Vue skips re-rendering it). Leaves — including decoded Schema CLASS INSTANCES — are compared with
160
+ * Effect `Equal.equals` (structural), which reuses equal instances that tanstack's `===` could not.
161
+ */
162
+ export const replaceEqualDeep = (prev: any, next: any): any => {
163
+ if (prev === next) return prev
164
+ const bothArrays = Array.isArray(prev) && Array.isArray(next)
165
+ if (bothArrays || (isPlainObject(prev) && isPlainObject(next))) {
166
+ const a: Record<PropertyKey, any> = prev
167
+ const b: Record<PropertyKey, any> = next
168
+ const copy: Record<PropertyKey, any> = bothArrays ? [] : {}
169
+ const nextKeys: Array<PropertyKey> = bothArrays ? (b as Array<any>).map((_, i) => i) : Object.keys(b)
170
+ const nextSize = nextKeys.length
171
+ const prevSize = bothArrays ? (a as Array<any>).length : Object.keys(a).length
172
+ let equalItems = 0
173
+ for (let i = 0; i < nextSize; i++) {
174
+ const key = nextKeys[i]!
175
+ copy[key] = replaceEqualDeep(a[key], b[key])
176
+ if (copy[key] === a[key] && a[key] !== undefined) equalItems++
177
+ }
178
+ return prevSize === nextSize && equalItems === prevSize ? prev : copy
179
+ }
180
+ return Equal.equals(prev, next) ? prev : next
181
+ }
182
+
183
+ /** Atom combinator: share each new `Success` value structurally against the previous one. */
184
+ const structuralShare = <A, E>(
185
+ self: Atom.Atom<AsyncResult.AsyncResult<A, E>>
186
+ ): Atom.Atom<AsyncResult.AsyncResult<A, E>> =>
187
+ Atom.transform(self, (get) => {
188
+ const next = get(self)
189
+ if (next._tag !== "Success") return next
190
+ const prev = Option.flatMap(get.self<AsyncResult.AsyncResult<A, E>>(), AsyncResult.value)
191
+ if (Option.isNone(prev)) return next
192
+ const shared = replaceEqualDeep(prev.value, next.value)
193
+ return shared === next.value
194
+ ? next
195
+ : AsyncResult.success(shared, { waiting: next.waiting, timestamp: next.timestamp })
196
+ }, { initialValueTarget: self })
197
+
198
+ export interface AtomClientRuntime {
199
+ readonly runtime: Atom.AtomRuntime<any, never>
200
+ readonly factory: Atom.RuntimeFactory
201
+ }
202
+
203
+ /**
204
+ * Build one AtomRuntime (and its factory) from an already-built app context.
205
+ * Shares the ManagedRuntime's `memoMap` so layers are not built twice, and so
206
+ * query-registration and mutation-invalidation resolve the SAME `Reactivity`.
207
+ */
208
+ export const makeAtomClientRuntime = <R>(
209
+ getContext: () => Layer.Layer<R, never, never>,
210
+ memoMap: Layer.MemoMap
211
+ ): AtomClientRuntime => {
212
+ const factory = Atom.context({ memoMap })
213
+ const runtime = factory((_get) => getContext())
214
+ return { runtime, factory }
215
+ }
216
+
217
+ const isRetryable = (e: unknown): boolean =>
218
+ isHttpClientError(e)
219
+ || S.is(ServiceUnavailableError)(e)
220
+ || (typeof e === "object" && e !== null && (e as any)._tag === "RpcClientError")
221
+
222
+ export interface AtomQueryOptions {
223
+ /** background-refresh threshold (TanStack staleTime; default 5s) */
224
+ readonly staleTime?: Duration.Input
225
+ /** dispose-when-idle (TanStack gcTime; default 5min). "infinity" => keepAlive */
226
+ readonly gcTime?: Duration.Input | "infinity"
227
+ readonly invalidation?: QueryInvalidationMode
228
+ /**
229
+ * Revalidate a stale query on window focus AND on network reconnect (default on, matching
230
+ * tanstack refetchOnWindowFocus + refetchOnReconnect).
231
+ */
232
+ readonly revalidateOnFocus?: boolean
233
+ /**
234
+ * Reuse references of unchanged sub-trees across refetches (default on, matching tanstack
235
+ * structuralSharing). Uses Effect `Equal` so decoded Schema instances share too — more effective
236
+ * than tanstack's `===`, but a deep compare per refetch (O(rows·fields)). Set `false` for very
237
+ * large or mostly-changing result sets where the compare costs more than the saved re-renders.
238
+ */
239
+ readonly structuralSharing?: boolean
240
+ /** poll: re-fetch every N ms (tanstack refetchInterval). */
241
+ readonly refetchInterval?: number
242
+ /** Refresh when writes intersect the dependencies recorded by this query. */
243
+ readonly live?: boolean | LiveQueryOptions
244
+ }
245
+
246
+ const defaults = { staleTime: Duration.seconds(5), gcTime: Duration.minutes(5) }
247
+
248
+ export interface AtomQueryMetadata {
249
+ readonly staleTimeMs: number
250
+ }
251
+
252
+ const atomQueryMetadata = new WeakMap<Atom.Atom<AsyncResult.AsyncResult<any, any>>, AtomQueryMetadata>()
253
+ const atomQueryParentSpans = new WeakMap<Atom.Atom<AsyncResult.AsyncResult<any, any>>, Tracer.AnySpan>()
254
+ const atomQueryKeys = new WeakMap<Atom.Atom<AsyncResult.AsyncResult<any, any>>, ReadonlyArray<unknown>>()
255
+
256
+ export const queryKeyForAtom = <A, E>(
257
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
258
+ ): ReadonlyArray<unknown> | undefined => atomQueryKeys.get(atom)
259
+
260
+ const atomSpanTarget = <A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) => {
261
+ let target = atom
262
+ while (target.initialValueTarget !== undefined) {
263
+ target = target.initialValueTarget
264
+ }
265
+ return target
266
+ }
267
+
268
+ const takeAtomQueryParentSpan = <A, E>(atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) => {
269
+ const target = atomSpanTarget(atom)
270
+ const span = atomQueryParentSpans.get(target)
271
+ if (span !== undefined) atomQueryParentSpans.delete(target)
272
+ return span
273
+ }
274
+
275
+ const setAtomQueryParentSpan = <A, E>(
276
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
277
+ span: Tracer.AnySpan
278
+ ) => {
279
+ atomQueryParentSpans.set(atomSpanTarget(atom), span)
280
+ }
281
+
282
+ export const captureAtomQueryParentSpan = <A, E>(
283
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
284
+ ): Effect.Effect<void> =>
285
+ Effect.currentParentSpan.pipe(
286
+ Effect.tap((span) => Effect.sync(() => setAtomQueryParentSpan(atom, span))),
287
+ Effect.ignore
288
+ )
289
+
290
+ export const refreshAtomWithCurrentSpan = <A, E>(
291
+ registry: AtomRegistry.AtomRegistry,
292
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
293
+ ): Effect.Effect<void> =>
294
+ captureAtomQueryParentSpan(atom).pipe(
295
+ Effect.andThen(Effect.sync(() => registry.refresh(atom)))
296
+ )
297
+
298
+ const setAtomQueryMetadata = <A, E>(
299
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
300
+ opts: AtomQueryOptions = {}
301
+ ) => {
302
+ const staleTimeMs = staleTimeMsOf(opts)
303
+ const previous = atomQueryMetadata.get(atom)
304
+ atomQueryMetadata.set(atom, {
305
+ staleTimeMs: previous === undefined ? staleTimeMs : Math.min(previous.staleTimeMs, staleTimeMs)
306
+ })
307
+ return atom
308
+ }
309
+
310
+ export const getAtomQueryMetadata = <A, E>(
311
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
312
+ ): AtomQueryMetadata | undefined => atomQueryMetadata.get(atom)
313
+
314
+ /** Exported so the vue hook can do refetch-on-mount-per-observer with the same rule as swr. */
315
+ export const isStaleResult = (r: AsyncResult.AsyncResult<any, any>, staleTimeMs: number): boolean => {
316
+ if (r.waiting) return false
317
+ const ts = r._tag === "Success"
318
+ ? r.timestamp
319
+ : r._tag === "Failure"
320
+ ? Option.getOrUndefined(Option.map(r.previousSuccess, (s) => s.timestamp))
321
+ : undefined
322
+ if (ts === undefined) return r._tag !== "Initial"
323
+ return Date.now() - ts >= staleTimeMs
324
+ }
325
+
326
+ export const staleTimeMsOf = (opts: AtomQueryOptions): number =>
327
+ Duration.toMillis(Duration.fromInputUnsafe(opts.staleTime ?? defaults.staleTime))
328
+
329
+ // A query fetch that is interrupted (a subscriber lost interest / a refresh superseded it / the
330
+ // component navigated) leaves the family atom `waiting=true` with NO fiber running — effect-core
331
+ // `makeEffect` removes its result-observer before interrupting, so the interrupt is never written
332
+ // back; it stays `waitingFrom(previous)`. `swr`/`isStaleResult` both short-circuit
333
+ // `if (waiting) return false`, so that stuck `waiting` is neither completing nor considered stale —
334
+ // mount/focus/staleness all skip it. Left alone the interrupt is TERMINAL: any current or future
335
+ // subscriber inherits the `waiting` forever (cold → `latestDefined` throws → white screen; warm →
336
+ // the Mako Bauhaus PickList stale wedge).
337
+ //
338
+ // The invariant we restore in this layer (no effect-core change): an interrupt must never be a
339
+ // terminal state. We do NOT auto-retry the interrupted fetch — an interrupt is intentional (that
340
+ // caller lost interest), and the first observer recovers through its normal refresh/staleness rules.
341
+ // We only guarantee that a *genuine* new/parallel/future observer is never stuck on a departed
342
+ // observer's interrupt: per family atom we count live computes (`inFlight`), so "stuck" = a `waiting`
343
+ // result with `inFlight === 0` (nothing is actually fetching). On mount a stuck atom triggers one
344
+ // fresh fetch instead of adopting the dangling `waiting`; a genuinely in-flight fetch (`inFlight > 0`)
345
+ // is joined, not superseded (dedup / re-entrancy preserved). `recovering` dedupes concurrent mounts
346
+ // so exactly one recovery fetch is issued until a fetch is running again.
347
+ export interface QueryFetchState {
348
+ inFlight: number
349
+ recovering: boolean
350
+ }
351
+ export const queryFetchStates = new WeakMap<Atom.Atom<any>, QueryFetchState>()
352
+
353
+ const recoverStuckWaitingOnMount =
354
+ <A, E>(familyAtom: Atom.Atom<AsyncResult.AsyncResult<A, E>>) =>
355
+ (wrapped: Atom.Atom<AsyncResult.AsyncResult<A, E>>): Atom.Atom<AsyncResult.AsyncResult<A, E>> =>
356
+ Atom.transform(wrapped, (get) => {
357
+ const current = get.once(wrapped)
358
+ get.subscribe(wrapped, (value) => get.setSelf(value))
359
+ const state = queryFetchStates.get(familyAtom)
360
+ const waiting = current?.waiting === true
361
+ // Stuck: the result says `waiting` yet nothing is in-flight — an interrupt was hidden and left
362
+ // `waiting` dangling. Treat it as not-yet-fetched and refetch; a live fetch (`inFlight > 0`) is
363
+ // joined, not superseded. `recovering` prevents concurrent mounts from issuing more than one
364
+ // recovery fetch (cleared once a fetch is actually running again).
365
+ if (state && waiting && state.inFlight === 0 && !state.recovering) {
366
+ state.recovering = true
367
+ get.refresh(familyAtom)
368
+ }
369
+ return current
370
+ }, { initialValueTarget: wrapped })
371
+
372
+ export const withQueryOptions = <A, E>(
373
+ self: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
374
+ opts: AtomQueryOptions = {},
375
+ liveKey?: ReadonlyArray<unknown>
376
+ ): Atom.Atom<AsyncResult.AsyncResult<A, E>> => {
377
+ setAtomQueryMetadata(self, opts)
378
+ const staleTime: Duration.Input = opts.staleTime ?? defaults.staleTime
379
+ let atom = self
380
+ if (liveKey !== undefined) {
381
+ atom = Atom.transform(atom, (get) => {
382
+ const unregister = registerQueryInvalidationMode(liveKey, opts.invalidation ?? "await")
383
+ get.addFinalizer(unregister)
384
+ return get(self)
385
+ }, { initialValueTarget: self })
386
+ }
387
+ if (opts.live && liveKey !== undefined) {
388
+ const liveOptions = opts.live === true ? {} : opts.live
389
+ atom = Atom.transform(atom, (get) => {
390
+ const unregister = registerLiveQuery(
391
+ liveKey,
392
+ () => getQueryReadDependencies(liveKey),
393
+ liveOptions
394
+ )
395
+ get.addFinalizer(unregister)
396
+ return get(self)
397
+ }, { initialValueTarget: self })
398
+ }
399
+ const revalidateOnFocus = opts.revalidateOnFocus ?? true
400
+ atom = Atom.swr({
401
+ staleTime,
402
+ revalidateOnFocus,
403
+ focusSignal: revalidateOnFocus ? focusOrReconnectSignal : undefined
404
+ })(atom)
405
+ atom = recoverStuckWaitingOnMount(self)(atom)
406
+ if (opts.refetchInterval) atom = Atom.withRefresh(Duration.millis(opts.refetchInterval))(atom)
407
+ if (opts.structuralSharing ?? true) atom = structuralShare(atom)
408
+ return atom
409
+ }
410
+
411
+ /** Constant atom for disabled / `mode:"optional"`-None queries: stays Initial, never fetches. */
412
+ export const disabledQueryAtom: Atom.Atom<AsyncResult.AsyncResult<any, any>> = Atom.readable(() =>
413
+ AsyncResult.initial(false)
414
+ )
415
+
416
+ /**
417
+ * Bumps when the browser regains connectivity (the `online` event) — the tanstack
418
+ * `refetchOnReconnect` trigger. One shared listener (module-level). SSR-guarded.
419
+ */
420
+ const onlineSignal: Atom.Atom<number> = Atom.readable((get) => {
421
+ let count = 0
422
+ if (typeof window === "undefined") return count
423
+ const update = () => {
424
+ if (navigator.onLine) get.setSelf(++count)
425
+ }
426
+ window.addEventListener("online", update)
427
+ get.addFinalizer(() => window.removeEventListener("online", update))
428
+ return count
429
+ })
430
+
431
+ /**
432
+ * Focus OR reconnect, as a single signal for `swr` — both should stale-revalidate a query.
433
+ * swr takes one `focusSignal`, so we fold window-focus + reconnect into one derived atom;
434
+ * a bump from either triggers swr's stale check.
435
+ */
436
+ const focusOrReconnectSignal: Atom.Atom<number> = Atom.make((get) => get(Atom.windowFocusSignal) + get(onlineSignal))
437
+
438
+ /**
439
+ * Build the per-input atom family for a request handler — the query CACHE IDENTITY.
440
+ *
441
+ * This is the TanStack `queryKey = [handler, input]` equivalent and the piece that makes
442
+ * caching cross-component: `Atom.family` memoizes one atom per structurally-distinct input
443
+ * (v4 hashes the input via Hash/Equal), so every component querying the same handler+input
444
+ * reads the SAME atom instance => one fetch, one shared result in the global registry,
445
+ * ref-counted and GC'd on idle ttl. (The registry + ttl give lifetime; reactivity keys give
446
+ * invalidation; the family gives identity/sharing — all three are needed.)
447
+ *
448
+ * The family is created once per handler (see query.ts's per-handler cache), so it is shared
449
+ * process-wide via the registry.
450
+ *
451
+ * Invalidation is hierarchical: each atom registers under EVERY prefix of its full key
452
+ * `[...makeQueryKey(self), input]`. Since reactivity matches keys by exact hash, registering
453
+ * all prefixes means `invalidate(P)` refreshes every atom whose key starts with `P` — e.g.
454
+ * `["$X"]` refreshes all inputs, `["$X","$List",input]` only that input. (`makeQueryKey`'s
455
+ * collapsed form `getQueryKey` — what mutations invalidate by default — is one of the prefixes.)
456
+ */
457
+ export const buildQueryFamily = <I, A, E>(
458
+ rt: AtomClientRuntime,
459
+ self: {
460
+ readonly id: string
461
+ readonly handler: (i: I) => Effect.Effect<A, E, any>
462
+ readonly options?: ClientForOptions
463
+ readonly queryKeyProjectionHash?: string
464
+ }
465
+ ) => {
466
+ const baseKey = makeQueryKey(self) // hierarchical, input-independent
467
+
468
+ return Atom.family((input: I) => {
469
+ const fullKey = [...baseKey, input]
470
+ // Record the repository/server read-dependencies seen while fetching, keyed by `fullKey`, so a
471
+ // later mutation whose writes intersect them can derive this query as an invalidation target.
472
+ // The last recorded reads are retained on the (memoized) family atom so `trackReadDependencies`
473
+ // can re-assert them on a cache-hit remount that never re-runs the handler.
474
+ let lastReads: DataDependencies.DataDependencies = DataDependencies.empty()
475
+ // Fetch bookkeeping read by `recoverStuckWaitingOnMount`. `inFlight` counts live computes, so a
476
+ // `waiting` result with `inFlight === 0` is a stuck/hidden interrupt. `recovering` guards the
477
+ // recovery refresh against concurrent mounts; a running fetch clears it.
478
+ const fetchState: QueryFetchState = { inFlight: 0, recovering: false }
479
+ let atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
480
+ atom = rt.runtime.atom(
481
+ Effect.suspend(() => {
482
+ // A fetch is now running: the atom is not stuck, and any pending recovery is fulfilled.
483
+ fetchState.recovering = false
484
+ fetchState.inFlight++
485
+ const recordReads = Effect
486
+ .gen(function*() {
487
+ const readsRef = yield* Ref.make(DataDependencies.empty())
488
+ const writesRef = yield* Ref.make(DataDependencies.empty())
489
+ const recorder = DataDependencies.makeDataDependencyRecorder(readsRef, writesRef)
490
+ const result = yield* self
491
+ .handler(input)
492
+ .pipe(Effect.provideService(DataDependencies.DataDependencyRecorder, recorder))
493
+ lastReads = yield* Ref.get(readsRef)
494
+ setQueryReadDependencies(fullKey, lastReads)
495
+ return result
496
+ })
497
+ let liveFetch = false
498
+ const effect = Effect
499
+ .gen(function*() {
500
+ liveFetch = yield* Effect.promise(() => beginLiveQueryFetch(fullKey))
501
+ return yield* recordReads.pipe(Effect.retry({ times: 5, while: isRetryable }))
502
+ })
503
+ .pipe(
504
+ Effect.ensuring(Effect.sync(() => endLiveQueryFetch(liveFetch))),
505
+ Effect.tapCauseIf(Cause.hasDies, (cause) => reportRuntimeError(cause)),
506
+ // On exit, the compute is no longer in-flight. An interrupt (subscriber lost interest / a
507
+ // superseding refresh) may leave the result at `waiting`; with `inFlight` back at 0 that
508
+ // reads as "stuck", so the next mount recovers it (`recoverStuckWaitingOnMount`). We do not
509
+ // re-fire here — the interrupt was intentional; recovery is driven by a genuine (re)mount.
510
+ Effect.onExit(() =>
511
+ Effect.sync(() => {
512
+ fetchState.inFlight = Math.max(0, fetchState.inFlight - 1)
513
+ })
514
+ ),
515
+ Effect.withSpan(`query ${self.id}`, {}, { captureStackTrace: false })
516
+ )
517
+ const parentSpan = takeAtomQueryParentSpan(atom)
518
+ return parentSpan === undefined
519
+ ? effect
520
+ : effect.pipe(Effect.withParentSpan(parentSpan, { captureStackTrace: false }))
521
+ })
522
+ )
523
+ // Register under every prefix of the full key => hierarchical (prefix) invalidation. Two roles:
524
+ // - withReactivity: `Reactivity.invalidate(key)` refreshes this atom (the actual refetch).
525
+ // - trackByKeys: records the atom in `keyAtoms` so the mutation can AWAIT its settle.
526
+ const projectedFullKey = self.queryKeyProjectionHash === undefined
527
+ ? fullKey
528
+ : [...baseKey, self.queryKeyProjectionHash, input]
529
+ const reactivityKeys = uniqueKeys([...prefixesOf(fullKey), ...prefixesOf(projectedFullKey)])
530
+ atom = rt.factory.withReactivity(reactivityKeys)(atom)
531
+ atom = trackByKeys(reactivityKeys)(atom)
532
+ atom = trackReadDependencies(fullKey, () => lastReads)(atom)
533
+ // gcTime LAST so the whole chain (incl. the registration + tracking) stays alive through the
534
+ // idle window, letting invalidation reach a cached-but-unmounted query.
535
+ atom = Atom.setIdleTTL(atom, defaults.gcTime)
536
+ const registered = setAtomQueryMetadata(Atom.withLabel(`query:${self.id}`)(atom))
537
+ const writable = Atom.writable(
538
+ (get) => {
539
+ const current = get.once(registered)
540
+ get.subscribe(registered, (value) => get.setSelf(value))
541
+ return current
542
+ },
543
+ (ctx, value: AsyncResult.AsyncResult<A, E>) => ctx.setSelf(value),
544
+ (refresh) => refresh(registered)
545
+ )
546
+ const writableWithTarget = Object.assign(writable, { initialValueTarget: registered })
547
+ const result = setAtomQueryMetadata(Atom.withLabel(`query-cache:${self.id}`)(writableWithTarget))
548
+ // Key the fetch state by the atom `withQueryOptions` receives, so its mount hook can find it.
549
+ queryFetchStates.set(result, fetchState)
550
+ atomQueryKeys.set(result, fullKey)
551
+ return result
552
+ })
553
+ }
554
+
555
+ export const buildStreamQueryFamily = <I, A, E>(
556
+ rt: AtomClientRuntime,
557
+ self: {
558
+ readonly id: string
559
+ readonly handler: (i: I) => Stream.Stream<A, E, any>
560
+ readonly options?: ClientForOptions
561
+ readonly queryKeyProjectionHash?: string
562
+ }
563
+ ) => {
564
+ const baseKey = makeQueryKey(self)
565
+
566
+ return Atom.family((input: I) => {
567
+ let atom = rt.runtime.pull(
568
+ self.handler(input).pipe(
569
+ Stream.tapCause((cause) => Cause.hasDies(cause) ? reportRuntimeError(cause) : Effect.void)
570
+ )
571
+ )
572
+ const fullKey = [...baseKey, input]
573
+ const projectedFullKey = self.queryKeyProjectionHash === undefined
574
+ ? fullKey
575
+ : [...baseKey, self.queryKeyProjectionHash, input]
576
+ const reactivityKeys = uniqueKeys([...prefixesOf(fullKey), ...prefixesOf(projectedFullKey)])
577
+ atom = rt.factory.withReactivity(reactivityKeys)(atom)
578
+ atom = trackWritableByKeys(reactivityKeys)(atom)
579
+ atom = Atom.setIdleTTL(atom, defaults.gcTime)
580
+ return setAtomQueryMetadata(Atom.withLabel(`stream-query:${self.id}`)(atom))
581
+ })
582
+ }
583
+
584
+ /** Await the first resolved (non-Waiting) result of an atom. Failing query results fail the Effect. */
585
+ export const awaitAtomResult = <A, E>(
586
+ registry: AtomRegistry.AtomRegistry,
587
+ atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>
588
+ ) => AtomRegistry.getResult(registry, atom, { suspendOnWaiting: true })