| 121 | * ``` |
| 122 | */ |
| 123 | export class QueryCache extends Subscribable<QueryCacheListener> { |
| 124 | #queries: QueryStore |
| 125 | |
| 126 | constructor(public config: QueryCacheConfig = {}) { |
| 127 | super() |
| 128 | this.#queries = new Map<string, Query>() |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or |
| 133 | * builds and adds a new one to the cache if none exists yet. Used by framework adapters and |
| 134 | * plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing |
| 135 | * the reactive `QueryObserver` machinery. |
| 136 | * |
| 137 | * @example |
| 138 | * ```ts |
| 139 | * const queryCache = queryClient.getQueryCache() |
| 140 | * |
| 141 | * const query = queryCache.build(queryClient, { |
| 142 | * queryKey: ['posts'], |
| 143 | * queryFn: fetchPosts, |
| 144 | * }) |
| 145 | * ``` |
| 146 | */ |
| 147 | build< |
| 148 | TQueryFnData = unknown, |
| 149 | TError = DefaultError, |
| 150 | TData = TQueryFnData, |
| 151 | TQueryKey extends QueryKey = QueryKey, |
| 152 | >( |
| 153 | client: QueryClient, |
| 154 | options: WithRequired< |
| 155 | QueryOptions<TQueryFnData, TError, TData, TQueryKey>, |
| 156 | 'queryKey' |
| 157 | >, |
| 158 | state?: QueryState<TData, TError>, |
| 159 | ): Query<TQueryFnData, TError, TData, TQueryKey> { |
| 160 | const queryKey = options.queryKey |
| 161 | const queryHash = |
| 162 | options.queryHash ?? hashQueryKeyByOptions(queryKey, options) |
| 163 | let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash) |
| 164 | |
| 165 | if (!query) { |
| 166 | query = new Query({ |
| 167 | client, |
| 168 | queryKey, |
| 169 | queryHash, |
| 170 | options: client.defaultQueryOptions(options), |
| 171 | state, |
| 172 | defaultOptions: client.getQueryDefaults(queryKey), |
| 173 | }) |
| 174 | this.add(query) |
| 175 | } |
| 176 | |
| 177 | return query |
| 178 | } |
| 179 | |
| 180 | /** @internal */ |
nothing calls this directly
no outgoing calls
no test coverage detected