Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createBaseQuery.ts

132 lines3.3 KB

1import {
2 batch,
3 createComputed,
4 createResource,
5 on,
6 onCleanup,
7 onMount,
8} from 'solid-js'
9import { createStore, unwrap } from 'solid-js/store'
10import { useQueryClient } from './QueryClientProvider'
11import { shouldThrowError } from './utils'
12import type { QueryObserver } from '@tanstack/query-core'
13import type { QueryKey, QueryObserverResult } from '@tanstack/query-core'
14import type { CreateBaseQueryOptions } from './types'
15​
16// Base Query Function that is used to create the query.
17export function createBaseQuery<
18 TQueryFnData,
19 TError,
20 TData,
21 TQueryData,
22 TQueryKey extends QueryKey,
23>(
24 options: CreateBaseQueryOptions<
25 TQueryFnData,
26 TError,
27 TData,
28 TQueryData,
29 TQueryKey
30 >,
31 Observer: typeof QueryObserver,
32): QueryObserverResult<TData, TError> {
33 const queryClient = useQueryClient({ context: options.context })
34 const emptyData = Symbol('empty')
35 const defaultedOptions = queryClient.defaultQueryOptions(options)
36 defaultedOptions._optimisticResults = 'optimistic'
37 const observer = new Observer(queryClient, defaultedOptions)
38​
39 const [state, setState] = createStore<QueryObserverResult<TData, TError>>(
40 // @ts-ignore
41 observer.getOptimisticResult(defaultedOptions),
42 )
43​
44 const [dataResource, { refetch, mutate }] = createResource<TData | undefined>(
45 () => {
46 return new Promise((resolve) => {
47 if (!(state.isFetching && state.isLoading)) {
48 if (unwrap(state.data) === emptyData) {
49 resolve(undefined)
50 }
51 resolve(unwrap(state.data))
52 }
53 })
54 },
55 )
56​
57 batch(() => {
58 mutate(() => unwrap(state.data))
59 refetch()
60 })
61​
62 let taskQueue: Array<() => void> = []
63​
64 const unsubscribe = observer.subscribe((result) => {
65 taskQueue.push(() => {
66 batch(() => {
67 const unwrappedResult = { ...unwrap(result) }
68 if (unwrappedResult.data === undefined) {
69 // This is a hack to prevent Solid
70 // from deleting the data property when it is `undefined`
71 // ref: https://www.solidjs.com/docs/latest/api#updating-stores
72 // @ts-ignore
73 unwrappedResult.data = emptyData
74 }
75 setState(unwrap(unwrappedResult))
76 mutate(() => unwrap(result.data))
77 refetch()
78 })
79 })
80​
81 queueMicrotask(() => {
82 const taskToRun = taskQueue.pop()
83 if (taskToRun) {
84 taskToRun()
85 }
86 taskQueue = []
87 })
88 })
89​
90 onCleanup(() => unsubscribe())
91​
92 onMount(() => {
93 observer.setOptions(defaultedOptions, { listeners: false })
94 })
95​
96 createComputed(() => {
97 const newDefaultedOptions = queryClient.defaultQueryOptions(options)
98 observer.setOptions(newDefaultedOptions)
99 })
100​
101 createComputed(
102 on(
103 () => state.status,
104 () => {
105 if (
106 state.isError &&
107 !state.isFetching &&
108 shouldThrowError(observer.options.useErrorBoundary, [
109 state.error,
110 observer.getCurrentQuery(),
111 ])
112 ) {
113 throw state.error
114 }
115 },
116 ),
117 )
118​
119 const handler = {
120 get(
121 target: QueryObserverResult<TData, TError>,
122 prop: keyof QueryObserverResult<TData, TError>,
123 ): any {
124 if (prop === 'data') {
125 return dataResource()
126 }
127 return Reflect.get(target, prop)
128 },
129 }
130​
131 return new Proxy(state, handler) as QueryObserverResult<TData, TError>