Skip to content

refactor(core): add generic utilities for resolving value-or-function patterns, replace specialized resolveStaleTime and resolveEnabled #9212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/angular-query-devtools-experimental/package.json
Original file line number Diff line number Diff line change
@@ -58,7 +58,8 @@
"@angular/platform-browser-dynamic": "^20.0.0",
"@tanstack/angular-query-experimental": "workspace:*",
"eslint-plugin-jsdoc": "^50.5.0",
"npm-run-all2": "^5.0.0"
"npm-run-all2": "^5.0.0",
"tsup": "^8.4.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was adding tsup necessary here? I don’t think angular uses tsup for bundling 🤔

},
"peerDependencies": {
"@angular/common": ">=16.0.0",
3 changes: 2 additions & 1 deletion packages/angular-query-experimental/package.json
Original file line number Diff line number Diff line change
@@ -77,7 +77,8 @@
"@angular/platform-browser-dynamic": "^20.0.0",
"@tanstack/query-test-utils": "workspace:*",
"eslint-plugin-jsdoc": "^50.5.0",
"npm-run-all2": "^5.0.0"
"npm-run-all2": "^5.0.0",
"tsup": "^8.4.0"
},
"peerDependencies": {
"@angular/common": ">=16.0.0",
18 changes: 6 additions & 12 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,7 @@ import {
ensureQueryFn,
noop,
replaceData,
resolveEnabled,
resolveStaleTime,
resolveValueOrFunction,
skipToken,
timeUntilStale,
} from './utils'
@@ -16,7 +15,6 @@ import type {
CancelOptions,
DefaultError,
FetchStatus,
InitialDataFunction,
OmitKeyof,
QueryFunctionContext,
QueryKey,
@@ -256,7 +254,8 @@ export class Query<

isActive(): boolean {
return this.observers.some(
(observer) => resolveEnabled(observer.options.enabled, this) !== false,
(observer) =>
resolveValueOrFunction(observer.options.enabled, this) !== false,
)
}

@@ -275,7 +274,7 @@ export class Query<
if (this.getObserversCount() > 0) {
return this.observers.some(
(observer) =>
resolveStaleTime(observer.options.staleTime, this) === 'static',
resolveValueOrFunction(observer.options.staleTime, this) === 'static',
)
}

@@ -689,17 +688,12 @@ function getDefaultState<
>(
options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): QueryState<TData, TError> {
const data =
typeof options.initialData === 'function'
? (options.initialData as InitialDataFunction<TData>)()
: options.initialData
const data = resolveValueOrFunction(options.initialData)

const hasData = data !== undefined

const initialDataUpdatedAt = hasData
? typeof options.initialDataUpdatedAt === 'function'
? (options.initialDataUpdatedAt as () => number | undefined)()
: options.initialDataUpdatedAt
? resolveValueOrFunction(options.initialDataUpdatedAt)
: 0

return {
8 changes: 5 additions & 3 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import {
hashQueryKeyByOptions,
noop,
partialMatchKey,
resolveStaleTime,
resolveValueOrFunction,
skipToken,
} from './utils'
import { QueryCache } from './queryCache'
@@ -156,7 +156,9 @@ export class QueryClient {

if (
options.revalidateIfStale &&
query.isStaleByTime(resolveStaleTime(defaultedOptions.staleTime, query))
query.isStaleByTime(
resolveValueOrFunction(defaultedOptions.staleTime, query),
)
) {
void this.prefetchQuery(defaultedOptions)
}
@@ -364,7 +366,7 @@ export class QueryClient {
const query = this.#queryCache.build(this, defaultedOptions)

return query.isStaleByTime(
resolveStaleTime(defaultedOptions.staleTime, query),
resolveValueOrFunction(defaultedOptions.staleTime, query),
)
? query.fetch(defaultedOptions)
: Promise.resolve(query.state.data as TData)
66 changes: 31 additions & 35 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
@@ -8,8 +8,7 @@ import {
isValidTimeout,
noop,
replaceData,
resolveEnabled,
resolveStaleTime,
resolveValueOrFunction,
shallowEqualObjects,
timeUntilStale,
} from './utils'
@@ -19,7 +18,6 @@ import type { PendingThenable, Thenable } from './thenable'
import type {
DefaultError,
DefaultedQueryObserverOptions,
PlaceholderDataFunction,
QueryKey,
QueryObserverBaseResult,
QueryObserverOptions,
@@ -157,8 +155,10 @@ export class QueryObserver<
this.options.enabled !== undefined &&
typeof this.options.enabled !== 'boolean' &&
typeof this.options.enabled !== 'function' &&
typeof resolveEnabled(this.options.enabled, this.#currentQuery) !==
'boolean'
typeof resolveValueOrFunction(
this.options.enabled,
this.#currentQuery,
) !== 'boolean'
) {
throw new Error(
'Expected enabled to be a boolean or a callback that returns a boolean',
@@ -201,10 +201,10 @@ export class QueryObserver<
if (
mounted &&
(this.#currentQuery !== prevQuery ||
resolveEnabled(this.options.enabled, this.#currentQuery) !==
resolveEnabled(prevOptions.enabled, this.#currentQuery) ||
resolveStaleTime(this.options.staleTime, this.#currentQuery) !==
resolveStaleTime(prevOptions.staleTime, this.#currentQuery))
resolveValueOrFunction(this.options.enabled, this.#currentQuery) !==
resolveValueOrFunction(prevOptions.enabled, this.#currentQuery) ||
resolveValueOrFunction(this.options.staleTime, this.#currentQuery) !==
resolveValueOrFunction(prevOptions.staleTime, this.#currentQuery))
) {
this.#updateStaleTimeout()
}
@@ -215,8 +215,8 @@ export class QueryObserver<
if (
mounted &&
(this.#currentQuery !== prevQuery ||
resolveEnabled(this.options.enabled, this.#currentQuery) !==
resolveEnabled(prevOptions.enabled, this.#currentQuery) ||
resolveValueOrFunction(this.options.enabled, this.#currentQuery) !==
resolveValueOrFunction(prevOptions.enabled, this.#currentQuery) ||
nextRefetchInterval !== this.#currentRefetchInterval)
) {
this.#updateRefetchInterval(nextRefetchInterval)
@@ -344,7 +344,7 @@ export class QueryObserver<

#updateStaleTimeout(): void {
this.#clearStaleTimeout()
const staleTime = resolveStaleTime(
const staleTime = resolveValueOrFunction(
this.options.staleTime,
this.#currentQuery,
)
@@ -368,9 +368,10 @@ export class QueryObserver<

#computeRefetchInterval() {
return (
(typeof this.options.refetchInterval === 'function'
? this.options.refetchInterval(this.#currentQuery)
: this.options.refetchInterval) ?? false
resolveValueOrFunction(
this.options.refetchInterval,
this.#currentQuery,
) ?? false
)
}

@@ -381,7 +382,8 @@ export class QueryObserver<

if (
isServer ||
resolveEnabled(this.options.enabled, this.#currentQuery) === false ||
resolveValueOrFunction(this.options.enabled, this.#currentQuery) ===
false ||
!isValidTimeout(this.#currentRefetchInterval) ||
this.#currentRefetchInterval === 0
) {
@@ -489,15 +491,11 @@ export class QueryObserver<
skipSelect = true
} else {
// compute placeholderData
placeholderData =
typeof options.placeholderData === 'function'
? (
options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
)(
this.#lastQueryWithDefinedData?.state.data,
this.#lastQueryWithDefinedData as any,
)
: options.placeholderData
placeholderData = resolveValueOrFunction(
options.placeholderData,
this.#lastQueryWithDefinedData?.state.data,
this.#lastQueryWithDefinedData as any,
)
}

if (placeholderData !== undefined) {
@@ -660,9 +658,7 @@ export class QueryObserver<

const { notifyOnChangeProps } = this.options
const notifyOnChangePropsValue =
typeof notifyOnChangeProps === 'function'
? notifyOnChangeProps()
: notifyOnChangeProps
resolveValueOrFunction(notifyOnChangeProps)

if (
notifyOnChangePropsValue === 'all' ||
@@ -740,7 +736,7 @@ function shouldLoadOnMount(
options: QueryObserverOptions<any, any, any, any>,
): boolean {
return (
resolveEnabled(options.enabled, query) !== false &&
resolveValueOrFunction(options.enabled, query) !== false &&
query.state.data === undefined &&
!(query.state.status === 'error' && options.retryOnMount === false)
)
@@ -765,10 +761,10 @@ function shouldFetchOn(
(typeof options)['refetchOnReconnect'],
) {
if (
resolveEnabled(options.enabled, query) !== false &&
resolveStaleTime(options.staleTime, query) !== 'static'
resolveValueOrFunction(options.enabled, query) !== false &&
resolveValueOrFunction(options.staleTime, query) !== 'static'
) {
const value = typeof field === 'function' ? field(query) : field
const value = resolveValueOrFunction(field, query)

return value === 'always' || (value !== false && isStale(query, options))
}
@@ -783,7 +779,7 @@ function shouldFetchOptionally(
): boolean {
return (
(query !== prevQuery ||
resolveEnabled(prevOptions.enabled, query) === false) &&
resolveValueOrFunction(prevOptions.enabled, query) === false) &&
(!options.suspense || query.state.status !== 'error') &&
isStale(query, options)
)
@@ -794,8 +790,8 @@ function isStale(
options: QueryObserverOptions<any, any, any, any, any>,
): boolean {
return (
resolveEnabled(options.enabled, query) !== false &&
query.isStaleByTime(resolveStaleTime(options.staleTime, query))
resolveValueOrFunction(options.enabled, query) !== false &&
query.isStaleByTime(resolveValueOrFunction(options.staleTime, query))
)
}

7 changes: 2 additions & 5 deletions packages/query-core/src/retryer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { focusManager } from './focusManager'
import { onlineManager } from './onlineManager'
import { pendingThenable } from './thenable'
import { isServer, sleep } from './utils'
import { isServer, resolveValueOrFunction, sleep } from './utils'
import type { CancelOptions, DefaultError, NetworkMode } from './types'

// TYPES
@@ -166,10 +166,7 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
// Do we need to retry the request?
const retry = config.retry ?? (isServer ? 0 : 3)
const retryDelay = config.retryDelay ?? defaultRetryDelay
const delay =
typeof retryDelay === 'function'
? retryDelay(failureCount, error)
: retryDelay
const delay = resolveValueOrFunction(retryDelay, failureCount, error)
const shouldRetry =
retry === true ||
(typeof retry === 'number' && failureCount < retry) ||
13 changes: 3 additions & 10 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -166,9 +166,7 @@ export type QueryFunctionContext<

export type InitialDataFunction<T> = () => T | undefined

type NonFunctionGuard<T> = T extends Function ? never : T

export type PlaceholderDataFunction<
type PlaceholderDataFunction<
TQueryFnData = unknown,
TError = DefaultError,
TQueryData = TQueryFnData,
@@ -424,13 +422,8 @@ export interface QueryObserverOptions<
* If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `loading` data and no initialData has been provided.
*/
placeholderData?:
| NonFunctionGuard<TQueryData>
| PlaceholderDataFunction<
NonFunctionGuard<TQueryData>,
TError,
NonFunctionGuard<TQueryData>,
TQueryKey
>
| TQueryData
| PlaceholderDataFunction<TQueryData, TError, TQueryData, TQueryKey>

_optimisticResults?: 'optimistic' | 'isRestoring'

Loading
Oops, something went wrong.
Loading
Oops, something went wrong.