import { LitElement, ReactiveController, ReactiveControllerHost } from "lit"; import { type QueryClient, type MutationState, type MutationFilters, type Mutation, type DefaultError, type MutationCache, } from "@tanstack/query-core"; import { ContextConsumer } from "@lit/context"; import { type WeavyType, WeavyContext } from "../contexts/weavy-context"; import { whenParentsDefined } from "../utils/dom"; import { eqObjects } from "../utils/objects"; import type { PlainObjectType } from "../types/generic.types"; type MutationStateOptions = { filters?: MutationFilters; select?: (mutation: Mutation) => TResult; }; function getResult( mutationCache: MutationCache, options: MutationStateOptions ): Array { return mutationCache .findAll(options.filters) .map((mutation): TResult => (options.select ? options.select(mutation) : mutation.state) as TResult); } export class MutationStateController implements ReactiveController { host: ReactiveControllerHost; context?: ContextConsumer<{ __context__: WeavyType }, LitElement>; whenContext?: Promise; resolveContext?: (value: void | PromiseLike) => void; result?: MutationState[]; mutationCache?: MutationCache; mutationCacheUnsubscribe?: () => void; alwaysUpdate: boolean = false; options?: MutationStateOptions>; constructor(host: ReactiveControllerHost) { host.addController(this); this.host = host; void this.setContext(); } async setContext() { this.whenContext = new Promise((r) => (this.resolveContext = r)); await whenParentsDefined(this.host as LitElement); this.context = new ContextConsumer(this.host as LitElement, { context: WeavyContext, subscribe: true }); } hostUpdate(): void { if (this.context?.value) { this.resolveContext?.(); } } async trackMutationState( options: MutationStateOptions>, queryClient?: QueryClient ) { if (!queryClient) { await this.whenContext; queryClient = this.context?.value?.queryClient; } if (!queryClient) { throw new Error("No QueryClient provided"); } this.mutationCacheUnsubscribe?.(); this.options = options; this.mutationCache = queryClient.getMutationCache(); this.mutationCacheSubscribe(); return this.result; } mutationCacheSubscribe() { if (this.mutationCache && this.options) { this.result = getResult(this.mutationCache, this.options); //console.log("track mutations", options.filters?.mutationKey, this.result) this.mutationCacheUnsubscribe = this.mutationCache.subscribe((event) => { if (this.mutationCache && this.options && /added|removed|updated/.test(event.type)) { //const nextResult = replaceEqualDeep(this.result, getResult(mutationCache, options)); const nextResult = getResult(this.mutationCache, this.options); if ( this.result !== nextResult && !eqObjects(this.result as unknown as PlainObjectType, nextResult as unknown as PlainObjectType) ) { //console.log("trackMutationState update", this.result, nextResult) this.result = nextResult; this.host.requestUpdate(); } } }); this.host.requestUpdate(); } } get isMutating(): number { if (!this.mutationCache || !this.options?.filters) { return 0; } return getResult(this.mutationCache, { filters: { ...this.options.filters, status: "pending" } }).length; } untrackMutationState() { this.mutationCacheUnsubscribe?.(); this.mutationCacheUnsubscribe = undefined; this.result = undefined; this.options = undefined; this.mutationCache = undefined; this.host.requestUpdate(); } hostConnected() { this.mutationCacheSubscribe(); } hostDisconnected() { // Clear the subscription when the host is disconnected this.mutationCacheUnsubscribe?.(); } }