@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
169 lines (168 loc) • 6.21 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
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) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __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), value);
var __privateWrapper = (obj, member, setter, getter) => ({
set _(value) {
__privateSet(obj, member, value, setter);
},
get _() {
return __privateGet(obj, member, getter);
}
});
// src/mutationCache.ts
var mutationCache_exports = {};
__export(mutationCache_exports, {
MutationCache: () => MutationCache
});
module.exports = __toCommonJS(mutationCache_exports);
var import_notifyManager = require("./notifyManager.cjs");
var import_mutation = require("./mutation.cjs");
var import_utils = require("./utils.cjs");
var import_subscribable = require("./subscribable.cjs");
var _mutations, _scopes, _mutationId;
var MutationCache = class extends import_subscribable.Subscribable {
constructor(config = {}) {
super();
this.config = config;
__privateAdd(this, _mutations);
__privateAdd(this, _scopes);
__privateAdd(this, _mutationId);
__privateSet(this, _mutations, /* @__PURE__ */ new Set());
__privateSet(this, _scopes, /* @__PURE__ */ new Map());
__privateSet(this, _mutationId, 0);
}
build(client, options, state) {
const mutation = new import_mutation.Mutation({
mutationCache: this,
mutationId: ++__privateWrapper(this, _mutationId)._,
options: client.defaultMutationOptions(options),
state
});
this.add(mutation);
return mutation;
}
add(mutation) {
__privateGet(this, _mutations).add(mutation);
const scope = scopeFor(mutation);
if (typeof scope === "string") {
const scopedMutations = __privateGet(this, _scopes).get(scope);
if (scopedMutations) {
scopedMutations.push(mutation);
} else {
__privateGet(this, _scopes).set(scope, [mutation]);
}
}
this.notify({ type: "added", mutation });
}
remove(mutation) {
if (__privateGet(this, _mutations).delete(mutation)) {
const scope = scopeFor(mutation);
if (typeof scope === "string") {
const scopedMutations = __privateGet(this, _scopes).get(scope);
if (scopedMutations) {
if (scopedMutations.length > 1) {
const index = scopedMutations.indexOf(mutation);
if (index !== -1) {
scopedMutations.splice(index, 1);
}
} else if (scopedMutations[0] === mutation) {
__privateGet(this, _scopes).delete(scope);
}
}
}
}
this.notify({ type: "removed", mutation });
}
canRun(mutation) {
const scope = scopeFor(mutation);
if (typeof scope === "string") {
const mutationsWithSameScope = __privateGet(this, _scopes).get(scope);
const firstPendingMutation = mutationsWithSameScope == null ? void 0 : mutationsWithSameScope.find(
(m) => m.state.status === "pending"
);
return !firstPendingMutation || firstPendingMutation === mutation;
} else {
return true;
}
}
runNext(mutation) {
var _a;
const scope = scopeFor(mutation);
if (typeof scope === "string") {
const foundMutation = (_a = __privateGet(this, _scopes).get(scope)) == null ? void 0 : _a.find((m) => m !== mutation && m.state.isPaused);
return (foundMutation == null ? void 0 : foundMutation.continue()) ?? Promise.resolve();
} else {
return Promise.resolve();
}
}
clear() {
import_notifyManager.notifyManager.batch(() => {
__privateGet(this, _mutations).forEach((mutation) => {
this.notify({ type: "removed", mutation });
});
__privateGet(this, _mutations).clear();
__privateGet(this, _scopes).clear();
});
}
getAll() {
return Array.from(__privateGet(this, _mutations));
}
find(filters) {
const defaultedFilters = { exact: true, ...filters };
return this.getAll().find(
(mutation) => (0, import_utils.matchMutation)(defaultedFilters, mutation)
);
}
findAll(filters = {}) {
return this.getAll().filter((mutation) => (0, import_utils.matchMutation)(filters, mutation));
}
notify(event) {
import_notifyManager.notifyManager.batch(() => {
this.listeners.forEach((listener) => {
listener(event);
});
});
}
resumePausedMutations() {
const pausedMutations = this.getAll().filter((x) => x.state.isPaused);
return import_notifyManager.notifyManager.batch(
() => Promise.all(
pausedMutations.map((mutation) => mutation.continue().catch(import_utils.noop))
)
);
}
};
_mutations = new WeakMap();
_scopes = new WeakMap();
_mutationId = new WeakMap();
function scopeFor(mutation) {
var _a;
return (_a = mutation.options.scope) == null ? void 0 : _a.id;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MutationCache
});
//# sourceMappingURL=mutationCache.cjs.map
;