-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
braden-w
wants to merge
5
commits into
TanStack:main
Choose a base branch
from
EpicenterHQ:refactor/resolveValueOrFunction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−140
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolveStaleTime
and resolveEnabled
View your CI Pipeline Execution ↗ for commit 44416bf.
☁️ Nx Cloud last updated this comment at |
df0249f
to
2421a38
Compare
… patterns, replace specialized `resolveStaleTime` and `resolveEnabled` This commit introduces `isFunctionVariant()` and `resolveValueOrFunction()` utilities to replace repetitive `typeof === 'function'` checks throughout the codebase, consolidating the common "value or function that computes value" pattern.
409a222
to
b28e950
Compare
… NonFunctionGuard This simplification is possible due to the introduction of generic runtime utilities that handle value-or-function resolution. The new `resolveValueOrFunction()` utility handles the distinction between direct values and functions at runtime with proper type safety, eliminating the need for complex type-level guards.
b28e950
to
cd59cc4
Compare
9d15984
to
427c977
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces
isFunctionVariant()
andresolveValueOrFunction()
utilities to replace repetitivetypeof === 'function'
checks throughout the codebase, consolidating the common "value or function that computes value" pattern.Problem and Solution
The codebase had scattered implementations of the same pattern across multiple files:
This led to code duplication, inconsistency, and maintenance overhead. We also had specialized versions like
resolveStaleTime()
andresolveEnabled()
that could be generalized.The new utilities provide a clean, generic solution:
While we could inline
typeof value === 'function'
checks, TypeScript's type narrowing doesn't work properly with generic types in complex expressions. TheisFunctionVariant()
type guard provides proper type narrowing that allowsresolveValueOrFunction()
to safely call the function variant. Without it, TypeScript throws errors because it can't guarantee the type safety across the generic boundary.Both utilities support zero-argument functions (
() => T
) and functions with parameters ((...args) => T
), making them applicable to all value-or-function patterns in the codebase.Updated implementations in
query.ts
,queryObserver.ts
, andretryer.ts
for handlinginitialData
,retryDelay
,refetchInterval
, andnotifyOnChangeProps
. These utilities can replace existingresolveStaleTime()
andresolveEnabled()
functions in future iterations.