Skip to content

refactor: rename cache methods for better clarity and consistency #9216

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

Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/query-broadcast-client-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
return
}

queryCache.build(
queryCache.ensure(

Check warning on line 81 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L81

Added line #L81 was not covered by tests
queryClient,
{
queryKey,
Expand All @@ -95,7 +95,7 @@
query.setState(state)
return
}
queryCache.build(
queryCache.ensure(

Check warning on line 98 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L98

Added line #L98 was not covered by tests
queryClient,
{
queryKey,
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/__tests__/mutations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('mutations', () => {

const mutation = queryClient
.getMutationCache()
.build<string, unknown, string, string>(
.create<string, unknown, string, string>(
queryClient,
{
mutationKey: key,
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('mutations', () => {
test('addObserver should not add an existing observer', () => {
const mutationCache = queryClient.getMutationCache()
const observer = new MutationObserver(queryClient, {})
const currentMutation = mutationCache.build(queryClient, {})
const currentMutation = mutationCache.create(queryClient, {})

const fn = vi.fn()

Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function executeMutation<TVariables>(
) {
return queryClient
.getMutationCache()
.build(queryClient, options)
.create(queryClient, options)
.execute(variables)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function hydrate(
const queries = (dehydratedState as DehydratedState).queries || []

mutations.forEach(({ state, ...mutationOptions }) => {
mutationCache.build(
mutationCache.create(
client,
{
...client.getDefaultOptions().hydrate?.mutations,
Expand Down Expand Up @@ -227,7 +227,7 @@ export function hydrate(
}
} else {
// Restore query
query = queryCache.build(
query = queryCache.ensure(
client,
{
...client.getDefaultOptions().hydrate?.queries,
Expand Down
12 changes: 11 additions & 1 deletion packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
this.#mutationId = 0
}

build<TData, TError, TVariables, TContext>(
/**
* Creates a new mutation instance and adds it to the cache.
* Unlike QueryCache.ensure(), this always creates a new mutation since
* each mutation execution should be unique.
*
* @param client - The QueryClient instance
* @param options - Mutation options
* @param state - Optional initial state for the new mutation
* @returns A new Mutation instance
*/
create<TData, TError, TVariables, TContext>(
client: QueryClient,
options: MutationOptions<TData, TError, TVariables, TContext>,
state?: MutationState<TData, TError, TVariables, TContext>,
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class MutationObserver<

this.#currentMutation = this.#client
.getMutationCache()
.build(this.#client, this.options)
.create(this.#client, this.options)

this.#currentMutation.addObserver(this)

Expand Down
17 changes: 16 additions & 1 deletion packages/query-core/src/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
this.#queries = new Map<string, Query>()
}

build<
/**
* Ensures a query exists in the cache, creating it if it doesn't exist.
* This is the primary method for ensuring a query exists in the cache.
*
* @param client - The QueryClient instance
* @param options - Query options including the queryKey
* @param state - Optional initial state for newly created queries
* @returns Always returns a Query instance (creates one if needed)
*/
ensure<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
Expand Down Expand Up @@ -163,6 +172,12 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
})
}

/**
* Retrieves an existing query from the cache by its hash.
*
* @param queryHash - The hash string identifying the query
* @returns The existing Query instance, or undefined if not found
*/
get<
TQueryFnData = unknown,
TError = DefaultError,
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class QueryClient {
options: EnsureQueryDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): Promise<TData> {
const defaultedOptions = this.defaultQueryOptions(options)
const query = this.#queryCache.build(this, defaultedOptions)
const query = this.#queryCache.ensure(this, defaultedOptions)
const cachedData = query.state.data

if (cachedData === undefined) {
Expand Down Expand Up @@ -205,7 +205,7 @@ export class QueryClient {
}

return this.#queryCache
.build(this, defaultedOptions)
.ensure(this, defaultedOptions)
.setData(data, { ...options, manual: true })
}

Expand Down Expand Up @@ -361,7 +361,7 @@ export class QueryClient {
defaultedOptions.retry = false
}

const query = this.#queryCache.build(this, defaultedOptions)
const query = this.#queryCache.ensure(this, defaultedOptions)

return query.isStaleByTime(
resolveStaleTime(defaultedOptions.staleTime, query),
Expand Down
8 changes: 5 additions & 3 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class QueryObserver<
TQueryKey
>,
): QueryObserverResult<TData, TError> {
const query = this.#client.getQueryCache().build(this.#client, options)
const query = this.#client.getQueryCache().ensure(this.#client, options)

const result = this.createResult(query, options)

Expand Down Expand Up @@ -306,7 +306,7 @@ export class QueryObserver<

const query = this.#client
.getQueryCache()
.build(this.#client, defaultedOptions)
.ensure(this.#client, defaultedOptions)

return query.fetch().then(() => this.createResult(query, defaultedOptions))
}
Expand Down Expand Up @@ -691,7 +691,9 @@ export class QueryObserver<
}

#updateQuery(): void {
const query = this.#client.getQueryCache().build(this.#client, this.options)
const query = this.#client
.getQueryCache()
.ensure(this.#client, this.options)

if (query === this.#currentQuery) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('persistQueryClientSubscribe', () => {
dehydrateOptions: { shouldDehydrateMutation: () => true },
})

queryClient.getMutationCache().build(queryClient, {
queryClient.getMutationCache().create(queryClient, {
mutationFn: (text: string) => Promise.resolve(text),
})

Expand Down
Loading