@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
173 lines (172 loc) • 6.64 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);
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var __privateMethod = (obj, member, method) => {
__accessCheck(obj, member, "access private method");
return method;
};
// 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 _client, _currentResult, _currentMutation, _mutateOptions, _updateResult, updateResult_fn, _notify, notify_fn;
var MutationObserver = class extends import_subscribable.Subscribable {
constructor(client, options) {
super();
__privateAdd(this, _updateResult);
__privateAdd(this, _notify);
__privateAdd(this, _client, void 0);
__privateAdd(this, _currentResult, void 0);
__privateAdd(this, _currentMutation, void 0);
__privateAdd(this, _mutateOptions, void 0);
__privateSet(this, _client, client);
this.setOptions(options);
this.bindMethods();
__privateMethod(this, _updateResult, updateResult_fn).call(this);
}
bindMethods() {
this.mutate = this.mutate.bind(this);
this.reset = this.reset.bind(this);
}
setOptions(options) {
var _a;
const prevOptions = this.options;
this.options = __privateGet(this, _client).defaultMutationOptions(options);
if (!(0, import_utils.shallowEqualObjects)(prevOptions, this.options)) {
__privateGet(this, _client).getMutationCache().notify({
type: "observerOptionsUpdated",
mutation: __privateGet(this, _currentMutation),
observer: this
});
}
(_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.setOptions(this.options);
}
onUnsubscribe() {
var _a;
if (!this.hasListeners()) {
(_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this);
}
}
onMutationUpdate(action) {
__privateMethod(this, _updateResult, updateResult_fn).call(this);
__privateMethod(this, _notify, notify_fn).call(this, action);
}
getCurrentResult() {
return __privateGet(this, _currentResult);
}
reset() {
__privateSet(this, _currentMutation, void 0);
__privateMethod(this, _updateResult, updateResult_fn).call(this);
__privateMethod(this, _notify, notify_fn).call(this);
}
mutate(variables, options) {
var _a;
__privateSet(this, _mutateOptions, options);
(_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this);
__privateSet(this, _currentMutation, __privateGet(this, _client).getMutationCache().build(__privateGet(this, _client), this.options));
__privateGet(this, _currentMutation).addObserver(this);
return __privateGet(this, _currentMutation).execute(variables);
}
};
_client = new WeakMap();
_currentResult = new WeakMap();
_currentMutation = new WeakMap();
_mutateOptions = new WeakMap();
_updateResult = new WeakSet();
updateResult_fn = function() {
var _a;
const state = ((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state) ?? (0, import_mutation.getDefaultState)();
__privateSet(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 = new WeakSet();
notify_fn = function(action) {
import_notifyManager.notifyManager.batch(() => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (__privateGet(this, _mutateOptions) && this.hasListeners()) {
if ((action == null ? void 0 : action.type) === "success") {
(_b = (_a = __privateGet(this, _mutateOptions)).onSuccess) == null ? void 0 : _b.call(
_a,
action.data,
__privateGet(this, _currentResult).variables,
__privateGet(this, _currentResult).context
);
(_d = (_c = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _d.call(
_c,
action.data,
null,
__privateGet(this, _currentResult).variables,
__privateGet(this, _currentResult).context
);
} else if ((action == null ? void 0 : action.type) === "error") {
(_f = (_e = __privateGet(this, _mutateOptions)).onError) == null ? void 0 : _f.call(
_e,
action.error,
__privateGet(this, _currentResult).variables,
__privateGet(this, _currentResult).context
);
(_h = (_g = __privateGet(this, _mutateOptions)).onSettled) == null ? void 0 : _h.call(
_g,
void 0,
action.error,
__privateGet(this, _currentResult).variables,
__privateGet(this, _currentResult).context
);
}
}
this.listeners.forEach((listener) => {
listener(__privateGet(this, _currentResult));
});
});
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MutationObserver
});
//# sourceMappingURL=mutationObserver.cjs.map