(
q: RequestHandlerWithInput<I, A, E, R, Request, Name>
)
| 147 | } |
| 148 | queryClient.setQueryData(queryKey, (data: A | undefined) => data === undefined ? data : updater(data)) |
| 149 | } |
| 150 | }) |
| 151 | |
| 152 | export const makeTanstackQuery = <R>( |
| 153 | getRuntime: () => Context.Context<R>, |
| 154 | queryClient: QueryClient |
| 155 | ): MakeQuery2<R> => { |
| 156 | // Drop a query's recorded read-dependencies when tanstack evicts it from the cache, so the |
| 157 | // registry mirrors the live queries (mirrors the atom engine's `trackReadDependencies` finalizer). |
| 158 | queryClient.getQueryCache().subscribe((event) => { |
| 159 | if (event.type === "removed") clearQueryReadDependencies(event.query.queryKey) |
| 160 | }) |
| 161 | |
| 162 | const useQuery: MakeQuery2<R> = <I, A, E, Request extends Req, Name extends string>( |
| 163 | q: RequestHandlerWithInput<I, A, E, R, Request, Name> |
| 164 | ) => |
| 165 | <TData = A>( |
| 166 | arg: I | WatchSource<I> | undefined | WatchSource<Option.Option<I>>, |
| 167 | options?: LegacyTanstackOptions<A, CauseException<E>, TData> |
| 168 | ) => { |
| 169 | const runPromise = makeRunPromise(getRuntime()) |
| 170 | const queryKey = makeQueryKey(q) |
| 171 | const enabled = resolveEnabled(arg, options) |
| 172 | const structuralSharing = options?.structuralSharing === false ? false : replaceEqualDeep |
| 173 | const tanstackOptions = { |
| 174 | ...(options?.staleTime !== undefined ? { staleTime: options.staleTime } : {}), |
| 175 | ...(typeof options?.gcTime === "number" ? { gcTime: options.gcTime } : {}), |
| 176 | ...(options?.refetchOnWindowFocus !== undefined ? { refetchOnWindowFocus: options.refetchOnWindowFocus } : {}), |
| 177 | structuralSharing, |
| 178 | ...(options?.refetchInterval !== undefined ? { refetchInterval: options.refetchInterval } : {}), |
| 179 | ...(options?.select !== undefined ? { select: options.select } : {}) |
| 180 | } |
| 181 | const resolvedQueryKey = computed(() => { |
| 182 | const input = resolveInput(arg, options?.mode) |
| 183 | return fullQueryKey(q, queryKey, input) |
| 184 | }) |
| 185 | watch( |
| 186 | resolvedQueryKey, |
| 187 | (key, _, onCleanup) => onCleanup(registerQueryInvalidationMode(key, options?.invalidation ?? "await")), |
| 188 | { immediate: true } |
| 189 | ) |
| 190 | const tanstack = useTanstackQuery<A, CauseException<E>, TData>({ |
| 191 | ...tanstackOptions, |
| 192 | enabled, |
| 193 | throwOnError: false, |
| 194 | retry: (retryCount: number, error: unknown) => isRetryable(error) && retryCount < 5, |
| 195 | queryKey: resolvedQueryKey, |
| 196 | queryFn: ( |
| 197 | { meta, signal }: { |
| 198 | readonly meta?: { readonly span?: Tracer.AnySpan | undefined } | undefined |
| 199 | readonly signal: AbortSignal |
| 200 | } |
| 201 | ) => |
| 202 | runPromise( |
| 203 | Effect.gen(function*() { |
| 204 | const input = resolveInput(arg, options?.mode)! |
| 205 | // Record the repository/server read-dependencies seen while fetching, keyed by the |
| 206 | // tanstack queryKey, so a later mutation whose writes intersect them derives this query |
searching dependent graphs…