Skip to content
0

katanakit-js / MutationCache

Class: MutationCache ​

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:616

The MutationCache is the storage for mutations.

Normally, you will not interact with the MutationCache directly and instead use a QueryClient. You can subscribe to it (inherited from Subscribable) to be informed of safe/known updates to the cache, such as mutations being added, removed, or updated.

Example ​

ts
const unsubscribe = mutationCache.subscribe((event) => {
  console.log(event.type, event.mutation)
})

Extends ​

  • Subscribable<MutationCacheListener>

Constructors ​

Constructor ​

ts
new MutationCache(config?): MutationCache;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:619

Parameters ​

ParameterType
config?MutationCacheConfig

Returns ​

MutationCache

Overrides ​

ts
Subscribable<MutationCacheListener>.constructor

Properties ​

PropertyModifierTypeInherited fromDefined in
configpublicMutationCacheConfig-node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:618
listenersprotectedSet<MutationCacheListener>Subscribable.listenersnode_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:8

Methods ​

clear() ​

ts
clear(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:640

Removes all mutations from the cache.

Returns ​

void

Example ​

ts
const mutationCache = queryClient.getMutationCache()

mutationCache.clear()

find() ​

ts
find<TData, TError, TVariables, TOnMutateResult>(filters): 
  | Mutation<TData, TError, TVariables, TOnMutateResult>
  | undefined;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:670

A slightly more advanced method that can be used to get an existing mutation instance from the cache. If the mutation does not exist, undefined is returned.

This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios.

Type Parameters ​

Type ParameterDefault type
TDataunknown
TErrorError
TVariablesany
TOnMutateResultunknown

Parameters ​

ParameterType
filtersMutationFilters

Returns ​

| Mutation<TData, TError, TVariables, TOnMutateResult> | undefined

See ​

MutationCache#findAll

Example ​

ts
const mutationCache = queryClient.getMutationCache()

const mutation = mutationCache.find({ mutationKey: ['addPost'] })

findAll() ​

ts
findAll(filters?): Mutation<unknown, Error, unknown, unknown>[];

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:686

An even more advanced method that can be used to get existing mutation instances from the cache that match the given filters. If no mutations match, an empty array is returned.

This is not typically needed for most applications, but can come in handy when needing more information about mutations in rare scenarios.

Parameters ​

ParameterType
filters?MutationFilters<unknown, Error, unknown, unknown>

Returns ​

Mutation<unknown, Error, unknown, unknown>[]

See ​

MutationCache#find

Example ​

ts
const mutationCache = queryClient.getMutationCache()

const mutations = mutationCache.findAll({ mutationKey: ['addPost'] })

getAll() ​

ts
getAll(): Mutation<unknown, Error, unknown, unknown>[];

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:654

Returns all mutations within the cache.

This is not typically needed for most applications, but can come in handy when needing more information about a mutation in rare scenarios.

Returns ​

Mutation<unknown, Error, unknown, unknown>[]

Example ​

ts
const mutationCache = queryClient.getMutationCache()

const mutations = mutationCache.getAll()

hasListeners() ​

ts
hasListeners(): boolean;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:28

Returns true while at least one listener is registered, false once they have all unsubscribed.

Returns ​

boolean

Inherited from ​

ts
Subscribable.hasListeners

onSubscribe() ​

ts
protected onSubscribe(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:29

Returns ​

void

Inherited from ​

ts
Subscribable.onSubscribe

onUnsubscribe() ​

ts
protected onUnsubscribe(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:30

Returns ​

void

Inherited from ​

ts
Subscribable.onUnsubscribe

subscribe() ​

ts
subscribe(listener): () => void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:24

Registers a listener to be called on every update this object notifies about. Returns a function that removes the listener again — call it to stop listening. The base class never drops a listener on its own, though some subclasses clear all of theirs in destroy().

Parameters ​

ParameterTypeDescription
listenerMutationCacheListenerCalled on each update, with whatever the subclass passes to its subscribers.

Returns ​

() => void

Example ​

ts
const unsubscribe = subscribable.subscribe(() => {
  // react to the update
})

unsubscribe()

Inherited from ​

ts
Subscribable.subscribe

Released under the MIT License.