katanakit-js / MutationObserver
Class: MutationObserver<TData, TError, TVariables, TOnMutateResult>
Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:478
Observes a single mutation and derives a MutationObserverResult from it. A framework hook like useMutation creates one MutationObserver per hook call, keeps it stable across re-renders, calls setOptions when the options passed to the hook change, subscribes to it to re-render on updates, and reads getCurrentResult() for the value to return. Calling mutate() builds a new underlying Mutation in the MutationCache and executes it.
Example
const observer = new MutationObserver(queryClient, {
mutationFn: (variables: { title: string }) => addPost(variables),
})Extends
Subscribable<MutationObserverListener<TData,TError,TVariables,TOnMutateResult>>
Type Parameters
| Type Parameter | Default type |
|---|---|
TData | unknown |
TError | DefaultError |
TVariables | void |
TOnMutateResult | unknown |
Constructors
Constructor
new MutationObserver<TData, TError, TVariables, TOnMutateResult>(client, options): MutationObserver<TData, TError, TVariables, TOnMutateResult>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:481
Parameters
| Parameter | Type |
|---|---|
client | QueryClient |
options | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult> |
Returns
MutationObserver<TData, TError, TVariables, TOnMutateResult>
Overrides
Subscribable<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>>.constructorProperties
| Property | Modifier | Type | Inherited from | Defined in |
|---|---|---|---|---|
listeners | protected | Set<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>> | Subscribable.listeners | node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:8 |
options | public | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult> | - | node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:480 |
Methods
bindMethods()
protected bindMethods(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:482
Returns
void
getCurrentResult()
getCurrentResult(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:509
Returns the observer's current result, derived from the observed mutation's state (or the default, idle state if no mutation has been built yet, e.g. before the first mutate() call or after reset()).
Returns
MutationObserverResult<TData, TError, TVariables, TOnMutateResult>
hasListeners()
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
Subscribable.hasListenersmutate()
mutate(variables, options?): Promise<TData>;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:545
Builds a new Mutation in the MutationCache using the observer's current options, detaches this observer from any previously observed mutation, attaches it to the new one, and executes it with the given variables.
The optional per-call options (onSuccess/onError/onSettled) are invoked once the mutation settles, in addition to any callbacks defined on the observer's own options.
Parameters
| Parameter | Type |
|---|---|
variables | TVariables |
options? | MutateOptions<TData, TError, TVariables, TOnMutateResult> |
Returns
Promise<TData>
Example
await observer.mutate(
{ title: 'New post' },
{ onSuccess: (data) => console.log(data) },
)onSubscribe()
protected onSubscribe(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:500
Returns
void
Overrides
Subscribable.onSubscribeonUnsubscribe()
protected onUnsubscribe(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:501
Returns
void
Overrides
Subscribable.onUnsubscribereset()
reset(): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:526
Detaches the observer from the mutation it is currently observing (if any) and resets the observed result back to its default, idle state.
This does not cancel an in-flight mutation; the mutation itself keeps running to completion and its own callbacks still fire, but this observer stops reflecting its state and a subsequent mutate() call will build a brand new mutation.
Returns
void
Example
observer.reset()See
setOptions()
setOptions(options): void;Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:499
Updates the observer's options.
If the new mutationKey differs from the previous one (and both were defined), the observer is reset, detaching it from the mutation it was observing. Otherwise, if the currently observed mutation is still pending, its options are updated in place as well.
Parameters
| Parameter | Type |
|---|---|
options | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult> |
Returns
void
Example
observer.setOptions({
mutationFn: (variables: { title: string }) => addPost(variables),
onSuccess: (data) => console.log(data),
})subscribe()
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
| Parameter | Type | Description |
|---|---|---|
listener | MutationObserverListener | Called on each update, with whatever the subclass passes to its subscribers. |
Returns
() => void
Example
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()Inherited from
Subscribable.subscribe