@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
118 lines (117 loc) • 3.77 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils = require("./utils.cjs");
const require_subscribable = require("./subscribable.cjs");
const require_notifyManager = require("./notifyManager.cjs");
const require_mutation = require("./mutation.cjs");
//#region src/mutationObserver.ts
var MutationObserver = class extends require_subscribable.Subscribable {
#client;
#currentResult = void 0;
#currentMutation;
#mutateOptions;
constructor(client, options) {
super();
this.#client = client;
this.setOptions(options);
this.bindMethods();
this.#updateResult();
}
bindMethods() {
this.mutate = this.mutate.bind(this);
this.reset = this.reset.bind(this);
}
setOptions(options) {
const prevOptions = this.options;
this.options = this.#client.defaultMutationOptions(options);
if (!require_utils.shallowEqualObjects(this.options, prevOptions)) this.#client.getMutationCache().notify({
type: "observerOptionsUpdated",
mutation: this.#currentMutation,
observer: this
});
if (prevOptions?.mutationKey && this.options.mutationKey && require_utils.hashKey(prevOptions.mutationKey) !== require_utils.hashKey(this.options.mutationKey)) this.reset();
else if (this.#currentMutation?.state.status === "pending") this.#currentMutation.setOptions(this.options);
}
onSubscribe() {
if (this.listeners.size === 1 && this.#currentMutation) {
this.#currentMutation.addObserver(this);
this.#updateResult();
}
}
onUnsubscribe() {
if (!this.hasListeners()) this.#currentMutation?.removeObserver(this);
}
onMutationUpdate(action) {
this.#updateResult();
this.#notify(action);
}
getCurrentResult() {
return this.#currentResult;
}
reset() {
this.#currentMutation?.removeObserver(this);
this.#currentMutation = void 0;
this.#updateResult();
this.#notify();
}
mutate(variables, options) {
this.#mutateOptions = options;
this.#currentMutation?.removeObserver(this);
this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
this.#currentMutation.addObserver(this);
return this.#currentMutation.execute(variables);
}
#updateResult() {
const state = this.#currentMutation?.state ?? require_mutation.getDefaultState();
this.#currentResult = {
...state,
isPending: state.status === "pending",
isSuccess: state.status === "success",
isError: state.status === "error",
isIdle: state.status === "idle",
mutate: this.mutate,
reset: this.reset
};
}
#notify(action) {
require_notifyManager.notifyManager.batch(() => {
if (this.#mutateOptions && this.hasListeners()) {
const variables = this.#currentResult.variables;
const onMutateResult = this.#currentResult.context;
const context = {
client: this.#client,
meta: this.options.meta,
mutationKey: this.options.mutationKey
};
if (action?.type === "success") {
try {
this.#mutateOptions.onSuccess?.(action.data, variables, onMutateResult, context);
} catch (e) {
Promise.reject(e);
}
try {
this.#mutateOptions.onSettled?.(action.data, null, variables, onMutateResult, context);
} catch (e) {
Promise.reject(e);
}
} else if (action?.type === "error") {
try {
this.#mutateOptions.onError?.(action.error, variables, onMutateResult, context);
} catch (e) {
Promise.reject(e);
}
try {
this.#mutateOptions.onSettled?.(void 0, action.error, variables, onMutateResult, context);
} catch (e) {
Promise.reject(e);
}
}
}
this.listeners.forEach((listener) => {
listener(this.#currentResult);
});
});
}
};
//#endregion
exports.MutationObserver = MutationObserver;
//# sourceMappingURL=mutationObserver.cjs.map