@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
134 lines (133 loc) • 4.32 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/mutationObserver.ts
var mutationObserver_exports = {};
__export(mutationObserver_exports, {
MutationObserver: () => MutationObserver
});
module.exports = __toCommonJS(mutationObserver_exports);
var import_mutation = require("./mutation.cjs");
var import_notifyManager = require("./notifyManager.cjs");
var import_subscribable = require("./subscribable.cjs");
var import_utils = require("./utils.cjs");
var MutationObserver = class extends import_subscribable.Subscribable {
constructor(client, options) {
super();
this.#currentResult = void 0;
this.#client = client;
this.setOptions(options);
this.bindMethods();
this.#updateResult();
}
#client;
#currentResult;
#currentMutation;
#mutateOptions;
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 (!(0, import_utils.shallowEqualObjects)(prevOptions, this.options)) {
this.#client.getMutationCache().notify({
type: "observerOptionsUpdated",
mutation: this.#currentMutation,
observer: this
});
}
this.#currentMutation?.setOptions(this.options);
}
onUnsubscribe() {
if (!this.hasListeners()) {
this.#currentMutation?.removeObserver(this);
}
}
onMutationUpdate(action) {
this.#updateResult();
this.#notify(action);
}
getCurrentResult() {
return this.#currentResult;
}
reset() {
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 ?? (0, import_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) {
import_notifyManager.notifyManager.batch(() => {
if (this.#mutateOptions && this.hasListeners()) {
if (action?.type === "success") {
this.#mutateOptions.onSuccess?.(
action.data,
this.#currentResult.variables,
this.#currentResult.context
);
this.#mutateOptions.onSettled?.(
action.data,
null,
this.#currentResult.variables,
this.#currentResult.context
);
} else if (action?.type === "error") {
this.#mutateOptions.onError?.(
action.error,
this.#currentResult.variables,
this.#currentResult.context
);
this.#mutateOptions.onSettled?.(
void 0,
action.error,
this.#currentResult.variables,
this.#currentResult.context
);
}
}
this.listeners.forEach((listener) => {
listener(this.#currentResult);
});
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MutationObserver
});
//# sourceMappingURL=mutationObserver.cjs.map