| 90 | // CLASS |
| 91 | |
| 92 | export class QueryCache extends Subscribable<QueryCacheListener> { |
| 93 | #queries: QueryStore |
| 94 | |
| 95 | constructor(public config: QueryCacheConfig = {}) { |
| 96 | super() |
| 97 | this.#queries = new Map<string, Query>() |
| 98 | } |
| 99 | |
| 100 | build< |
| 101 | TQueryFnData = unknown, |
| 102 | TError = DefaultError, |
| 103 | TData = TQueryFnData, |
| 104 | TQueryKey extends QueryKey = QueryKey, |
| 105 | >( |
| 106 | client: QueryClient, |
| 107 | options: WithRequired< |
| 108 | QueryOptions<TQueryFnData, TError, TData, TQueryKey>, |
| 109 | 'queryKey' |
| 110 | >, |
| 111 | state?: QueryState<TData, TError>, |
| 112 | ): Query<TQueryFnData, TError, TData, TQueryKey> { |
| 113 | const queryKey = options.queryKey |
| 114 | const queryHash = |
| 115 | options.queryHash ?? hashQueryKeyByOptions(queryKey, options) |
| 116 | let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash) |
| 117 | |
| 118 | if (!query) { |
| 119 | query = new Query({ |
| 120 | client, |
| 121 | queryKey, |
| 122 | queryHash, |
| 123 | options: client.defaultQueryOptions(options), |
| 124 | state, |
| 125 | defaultOptions: client.getQueryDefaults(queryKey), |
| 126 | }) |
| 127 | this.add(query) |
| 128 | } |
| 129 | |
| 130 | return query |
| 131 | } |
| 132 | |
| 133 | add(query: Query<any, any, any, any>): void { |
| 134 | if (!this.#queries.has(query.queryHash)) { |
| 135 | this.#queries.set(query.queryHash, query) |
| 136 | |
| 137 | this.notify({ |
| 138 | type: 'added', |
| 139 | query, |
| 140 | }) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | remove(query: Query<any, any, any, any>): void { |
| 145 | const queryInMap = this.#queries.get(query.queryHash) |
| 146 | |
| 147 | if (queryInMap) { |
| 148 | query.destroy() |
| 149 |
nothing calls this directly
no outgoing calls
no test coverage detected