Files
agency/.pnpm-store/v3/files/13/4c58a1a76950d06161198d8b105b4b89839d41614e9af1b6240587c708075cb59e3cec3c41d3b8cbe45a47721c131db49d463a4d66c1f4a1e93e7e4875aaf9
hanzo-agent 5af6960b87 run project
Run: sess_f37640604ae254cb4d11dc6217f7b721
2026-09-30 00:04:56 +00:00

140 lines
4.0 KiB
Plaintext

// src/mutationObserver.ts
import { getDefaultState } from "./mutation.js";
import { notifyManager } from "./notifyManager.js";
import { Subscribable } from "./subscribable.js";
import { hashKey, shallowEqualObjects } from "./utils.js";
var MutationObserver = class extends 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 (!shallowEqualObjects(this.options, prevOptions)) {
this.#client.getMutationCache().notify({
type: "observerOptionsUpdated",
mutation: this.#currentMutation,
observer: this
});
}
if (prevOptions?.mutationKey && this.options.mutationKey && hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)) {
this.reset();
} else if (this.#currentMutation?.state.status === "pending") {
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?.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 ?? 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) {
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) {
void Promise.reject(e);
}
try {
this.#mutateOptions.onSettled?.(
action.data,
null,
variables,
onMutateResult,
context
);
} catch (e) {
void Promise.reject(e);
}
} else if (action?.type === "error") {
try {
this.#mutateOptions.onError?.(
action.error,
variables,
onMutateResult,
context
);
} catch (e) {
void Promise.reject(e);
}
try {
this.#mutateOptions.onSettled?.(
void 0,
action.error,
variables,
onMutateResult,
context
);
} catch (e) {
void Promise.reject(e);
}
}
}
this.listeners.forEach((listener) => {
listener(this.#currentResult);
});
});
}
};
export {
MutationObserver
};
//# sourceMappingURL=mutationObserver.js.map