import { notifyManager } from './notifyManager' import { defaultQueryOptions } from './queryClient' import { QueryObserver } from './queryObserver' import { Subscribable } from './subscribable' import { replaceEqualDeep, shallowEqualObjects, shouldThrowError, } from './utils' import type { QueryClient } from './queryClient' import type { ObserverOptionsInput, QueryObserverResultReader, } from './queryObserver' import type { DefaultedQueryObserverOptions, QueryObserverResult, } from './types' type CombineContext = { result?: Array combine?: CombineFn value?: TCombinedResult queryHashes?: Array } type CombineFn = ( result: Array, ) => TCombinedResult type QueriesObserverListener = (result: Array) => void type QueryObserverMatch = { defaultedQueryOptions: DefaultedQueryObserverOptions observer: QueryObserver reader?: QueryObserverResultReader } /** * Returns the items of `array1` that are not in `array2`. * @param array1 - The items to filter. * @param array2 - The items to exclude. * @returns A new array with the items of `array1` that are not in `array2`. */ function difference(array1: Array, array2: Array): Array { const excludeSet = new Set(array2) return array1.filter((x) => !excludeSet.has(x)) } /** * Compares two arrays of results by position and field value. * @param result - The new results. * @param previous - The previous results, if available. * @returns Whether the results match. */ function equalResults( result: Array, previous: Array | undefined, ): boolean { return ( !!previous && result.length === previous.length && result.every((value, index) => shallowEqualObjects(value, previous[index])) ) } /** * A `QueriesObserver` watches an array of queries at once, exposing them as * a single array of `QueryObserverResult`s (or, when a `combine` option is * given, as a combined value derived from that array). It manages one * internal `QueryObserver` per query, and is the primitive that framework * adapters (e.g. `useQueries`) build their hooks on top of. * @example * ```ts * const observer = new QueriesObserver(queryClient, [ * { queryKey: ['post', 1], queryFn: fetchPost }, * { queryKey: ['post', 2], queryFn: fetchPost }, * ]) * * const unsubscribe = observer.subscribe((result) => { * console.log(result) * }) * ``` */ export class QueriesObserver< TCombinedResult = Array, > extends Subscribable { #client: QueryClient #result: Array #options?: QueriesObserverOptions #observers: Array #combinedResult?: TCombinedResult #combineContext: CombineContext = {} #observerMatches: Array = [] #updatingQueries = false constructor( client: QueryClient, queries: Array>, options?: QueriesObserverOptions, ) { super() this.#client = client this.#options = options this.#observers = [] this.#result = [] this.setQueries(queries) } protected override onSubscribe(): void { if (this.listeners.size === 1) { this.#observers.forEach((observer) => this.#subscribeTo(observer)) } } protected override onUnsubscribe(): void { if (!this.listeners.size) { this.destroy() } } /** * Stops observing all queries: clears all listeners and destroys every * underlying `QueryObserver` this observer manages. */ destroy(): void { this.listeners = new Set() this.#observers.forEach((observer) => { observer.destroy() }) } /** * Replaces the set of queries being observed. Existing `QueryObserver`s * are reused for queries that match an already-observed query hash; * observers for queries that are no longer present are destroyed, and new * observers are created and subscribed to for newly added queries. * @param queries - The options of the queries to observe. * @param options - Replaces the observer's options, e.g. its `combine` function. * @example * ```ts * observer.setQueries([ * { queryKey: ['post', 1], queryFn: fetchPost }, * { queryKey: ['post', 3], queryFn: fetchPost }, * ]) * ``` */ setQueries( queries: Array, options?: QueriesObserverOptions, ): void { this.#setQueries(this.#findMatchingObservers(queries), options) } #setQueries( newObserverMatches: Array, options?: QueriesObserverOptions, ): void { this.#options = options if (process.env.NODE_ENV !== 'production') { const queryHashes = newObserverMatches.map( (match) => match.defaultedQueryOptions.queryHash, ) if (new Set(queryHashes).size !== queryHashes.length) { console.warn( '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.', ) } } notifyManager.batch(() => { const prevObservers = this.#observers // Apply all options before combining or notifying with the new results. this.#updatingQueries = true try { newObserverMatches.forEach((match) => { if (match.reader) { match.reader.commit() } else { match.observer.setOptions(match.defaultedQueryOptions) } }) } finally { this.#updatingQueries = false } const newObservers = newObserverMatches.map((match) => match.observer) const newResult = newObservers.map((observer) => observer.getCurrentResult(), ) const hasStructuralChange = prevObservers.length !== newObservers.length || newObservers.some( (observer, index) => observer !== prevObservers[index], ) if (!hasStructuralChange && equalResults(newResult, this.#result)) return if (hasStructuralChange) { this.#observerMatches = newObserverMatches.map( ({ defaultedQueryOptions, observer }) => ({ defaultedQueryOptions, observer, }), ) this.#observers = newObservers } this.#result = newResult if (!this.hasListeners()) return if (hasStructuralChange) { difference(prevObservers, newObservers).forEach((observer) => { observer.destroy() }) difference(newObservers, prevObservers).forEach((observer) => this.#subscribeTo(observer), ) } this.#notify() }) } /** * Returns the most recently computed array of `QueryObserverResult`s, one * per observed query, in the same order as the queries passed to the * constructor or `setQueries`. * @returns The current results. * @example * ```ts * const results = observer.getCurrentResult() * const data = results.map((result) => result.data) * ``` */ getCurrentResult(): Array { return this.#result } /** * Returns the underlying `QueryObserver` instances this observer manages, * in the same order as the queries passed to the constructor or * `setQueries`. * @returns The managed query observers. */ getObservers() { return this.#observers } /** * Creates a reader for the supplied queries and combine function. Reads keep * result and combine caches local until commit applies the queries and reuses * their computation state. Abandoned readers do not change active observers. * @param queries - The options of the queries to read and commit. * @param options - The combine function used for this reader. * @returns A reader with local snapshot and combine caches. */ createResultReader( queries: Array>, options?: QueriesObserverOptions, ): QueriesObserverResultReader { const matches = this.#findMatchingObservers(queries).map((match) => ({ ...match, reader: match.observer.createResultReader(match.defaultedQueryOptions), })) const queryHashes = matches.map( (match) => match.defaultedQueryOptions.queryHash, ) const combine = options?.combine const context = { ...this.#combineContext } let result = this.#result const getSnapshot = () => { const nextResult = matches.map((match) => match.reader.getSnapshot()) if (!shallowEqualObjects(nextResult, result)) { result = nextResult } return result } return { getSnapshot, combineResult: (input) => { // Notifications may have already combined these results. const committed = this.#combineContext if ( committed.combine === combine && equalResults(input, committed.result) && shallowEqualObjects(queryHashes, committed.queryHashes) ) { Object.assign(context, committed) } return this.#combineResult( this.#trackResult(input, matches), combine, queryHashes, context, input, ) }, commit: () => { this.#combineContext = { ...context } this.#setQueries(matches, { combine }) if ( this.#options?.combine === combine && equalResults(this.#result, context.result) ) { this.#combinedResult = context.value } }, } } #trackResult( result: Array, matches: Array, ) { const trackedProps = new Set() return matches.map((match, index) => { const observerResult = result[index]! return !match.defaultedQueryOptions.notifyOnChangeProps ? match.observer.trackResult(observerResult, (accessedProp) => { // track property on all observers to ensure proper (synchronized) tracking (#7000) if (!trackedProps.has(accessedProp)) { trackedProps.add(accessedProp) matches.forEach((m) => { m.observer.trackProp(accessedProp) }) } }) : observerResult }) } #combineResult( input: Array, combine: CombineFn | undefined, queryHashes?: Array, context = this.#combineContext, result = this.#result, ): TCombinedResult { if (combine) { const queryHashesChanged = queryHashes !== undefined && context.queryHashes !== undefined && !shallowEqualObjects(queryHashes, context.queryHashes) if ( !equalResults(result, context.result) || queryHashesChanged || combine !== context.combine ) { context.value = replaceEqualDeep(context.value, combine(input)) context.combine = combine context.result = result if (queryHashes !== undefined) { context.queryHashes = queryHashes } } return context.value as TCombinedResult } return input as any } #shouldSkipCombine(): boolean { return ( !this.#options?.combine || this.#observers.some((observer, index) => { const result = this.#result[index] const rule = observer.options.throwOnError return ( (observer.options.suspense && result?.data === undefined) || (result?.status === 'error' && result.fetchStatus !== 'fetching' && shouldThrowError(typeof rule === 'object' ? rule[1] : rule, [ result.error, observer.getCurrentQuery(), ])) ) }) ) } #findMatchingObservers( queries: Array, ): Array { const prevObserversMap = new Map>() this.#observers.forEach((observer) => { const key = observer.options.queryHash if (!key) return const previousObservers = prevObserversMap.get(key) if (previousObservers) { previousObservers.push(observer) } else { prevObserversMap.set(key, [observer]) } }) const observers: Array = [] queries.forEach((options) => { const defaultedOptions = defaultQueryOptions(this.#client, options) const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift() const observer = match ?? new QueryObserver(this.#client, defaultedOptions) observers.push({ defaultedQueryOptions: defaultedOptions, observer, }) }) return observers } #subscribeTo(observer: QueryObserver): void { // Create the listener here, not in `setQueries`: a listener created there // keeps the previous observers, and their queries, alive (#12656) observer.subscribe((result) => { this.#onUpdate(observer, result) }) } #onUpdate(observer: QueryObserver, result: QueryObserverResult): void { if (this.#updatingQueries) return const index = this.#observers.indexOf(observer) if (index !== -1) { this.#result = this.#result.slice() this.#result[index] = result this.#notify() } } #notify(): void { if (this.hasListeners()) { const shouldSkipCombine = this.#shouldSkipCombine() const previousResult = this.#combinedResult const newResult = shouldSkipCombine ? previousResult : this.#combineResult( this.#trackResult(this.#result, this.#observerMatches), this.#options?.combine, this.#observerMatches.map( (match) => match.defaultedQueryOptions.queryHash, ), ) this.#combinedResult = newResult if (shouldSkipCombine || previousResult !== newResult) { notifyManager.batch(() => { this.listeners.forEach((listener) => { listener(this.#result) }) }) } } } } /** * Options for a `QueriesObserver` that apply to all of its queries at once. */ export interface QueriesObserverOptions< TCombinedResult = Array, > { /** * A function that combines the array of `QueryObserverResult`s (one per * observed query) into a single value. The combined value is memoized and * only recomputed when one of the underlying results, the query hashes, or * the `combine` function itself changes. * * Defaults to returning the array of `QueryObserverResult`s unchanged. */ combine?: CombineFn } /** A cached reader for multiple query results and their combined value. */ export interface QueriesObserverResultReader< TCombinedResult, > extends QueryObserverResultReader> { /** Track result properties and combine the results with this reader's options. */ combineResult: (result: Array) => TCombinedResult }