Skip to content
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

perf(query-core): Improve performance of mutationCache #8450

Open
wants to merge 2 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
66 changes: 41 additions & 25 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ type MutationCacheListener = (event: MutationCacheNotifyEvent) => void
// CLASS

export class MutationCache extends Subscribable<MutationCacheListener> {
#mutations: Map<string, Array<Mutation<any, any, any, any>>>
#unscopedMutations: Set<Mutation<any, any, any, any>>
#scopedMutations: Map<string, Array<Mutation<any, any, any, any>>>
#mutationId: number

constructor(public config: MutationCacheConfig = {}) {
super()
this.#mutations = new Map()
this.#mutationId = Date.now()
this.#unscopedMutations = new Set()
this.#scopedMutations = new Map()
this.#mutationId = 0
}

build<TData, TError, TVariables, TContext>(
Expand All @@ -110,46 +112,60 @@ export class MutationCache extends Subscribable<MutationCacheListener> {

add(mutation: Mutation<any, any, any, any>): void {
const scope = scopeFor(mutation)
const mutations = this.#mutations.get(scope) ?? []
mutations.push(mutation)
this.#mutations.set(scope, mutations)
if (typeof scope === 'string') {
const mutations = this.#scopedMutations.get(scope) ?? []
mutations.push(mutation)
this.#scopedMutations.set(scope, mutations)
} else {
this.#unscopedMutations.add(mutation)
}
this.notify({ type: 'added', mutation })
}

remove(mutation: Mutation<any, any, any, any>): void {
const scope = scopeFor(mutation)
if (this.#mutations.has(scope)) {
const mutations = this.#mutations
.get(scope)
?.filter((x) => x !== mutation)
if (typeof scope === 'string') {
const mutations = this.#scopedMutations.get(scope);
if (mutations) {
if (mutations.length === 0) {
this.#mutations.delete(scope)
if (mutations.length > 1) {
this.#scopedMutations.set(scope, mutations.filter((x) => x !== mutation))
} else {
this.#mutations.set(scope, mutations)
this.#scopedMutations.delete(scope)
}
}
} else {
this.#unscopedMutations.delete(mutation)
}

this.notify({ type: 'removed', mutation })
}

canRun(mutation: Mutation<any, any, any, any>): boolean {
const firstPendingMutation = this.#mutations
.get(scopeFor(mutation))
?.find((m) => m.state.status === 'pending')

// we can run if there is no current pending mutation (start use-case)
// or if WE are the first pending mutation (continue use-case)
return !firstPendingMutation || firstPendingMutation === mutation
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const mutations = this.#scopedMutations.get(scope)
const firstPendingMutation = mutations?.find(m => m.state.status === 'pending')
// we can run if there is no current pending mutation (start use-case)
// or if WE are the first pending mutation (continue use-case)
return !firstPendingMutation || firstPendingMutation === mutation
} else {
// For unscoped mutations there are never any pending mutations in front of the
// current mutation
return true
}
}

runNext(mutation: Mutation<any, any, any, any>): Promise<unknown> {
const foundMutation = this.#mutations
.get(scopeFor(mutation))
const scope = scopeFor(mutation)
if (typeof scope === 'string') {
const foundMutation = this.#scopedMutations
.get(scope)
?.find((m) => m !== mutation && m.state.isPaused)

return foundMutation?.continue() ?? Promise.resolve()
return foundMutation?.continue() ?? Promise.resolve()
} else {
return Promise.resolve()
}
}

clear(): void {
Expand All @@ -161,7 +177,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
}

getAll(): Array<Mutation> {
return [...this.#mutations.values()].flat()
return [...this.#unscopedMutations, ...this.#scopedMutations.values()].flat()
}

find<
Expand Down Expand Up @@ -203,5 +219,5 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
}

function scopeFor(mutation: Mutation<any, any, any, any>) {
return mutation.options.scope?.id ?? String(mutation.mutationId)
return mutation.options.scope?.id
}
Loading