mirror of
https://github.com/hanzoai/agency.git
synced 2026-11-02 19:13:28 +00:00
137 lines
4.2 KiB
Plaintext
137 lines
4.2 KiB
Plaintext
import {
|
|
__privateAdd,
|
|
__privateGet,
|
|
__privateSet,
|
|
__privateWrapper
|
|
} from "./chunk-PXG64RU4.js";
|
|
|
|
// src/mutationCache.ts
|
|
import { notifyManager } from "./notifyManager.js";
|
|
import { Mutation } from "./mutation.js";
|
|
import { matchMutation, noop } from "./utils.js";
|
|
import { Subscribable } from "./subscribable.js";
|
|
var _mutations, _scopes, _mutationId;
|
|
var MutationCache = class extends 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 Mutation({
|
|
client,
|
|
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() {
|
|
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) => matchMutation(defaultedFilters, mutation)
|
|
);
|
|
}
|
|
findAll(filters = {}) {
|
|
return this.getAll().filter((mutation) => matchMutation(filters, mutation));
|
|
}
|
|
notify(event) {
|
|
notifyManager.batch(() => {
|
|
this.listeners.forEach((listener) => {
|
|
listener(event);
|
|
});
|
|
});
|
|
}
|
|
resumePausedMutations() {
|
|
const pausedMutations = this.getAll().filter((x) => x.state.isPaused);
|
|
return notifyManager.batch(
|
|
() => Promise.all(
|
|
pausedMutations.map((mutation) => mutation.continue().catch(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;
|
|
}
|
|
export {
|
|
MutationCache
|
|
};
|
|
//# sourceMappingURL=mutationCache.js.map |