import { LitElement, ReactiveController, ReactiveControllerHost } from "lit"; import { type QueryClient, type MutationObserverResult, type MutationObserverOptions, MutationObserver, replaceEqualDeep, MutateOptions, } from "@tanstack/query-core"; import { ContextConsumer } from "@lit/context"; import { type WeavyType, WeavyContext } from "../contexts/weavy-context"; import { whenParentsDefined } from "../utils/dom"; import { MutationAbortProps } from "../types/query.types"; export function hasAbort(obj: unknown): obj is MutationAbortProps { return (obj as MutationAbortProps).signal instanceof AbortSignal || typeof (obj as MutationAbortProps).abort === "function" ; } export class MutationController implements ReactiveController { host: ReactiveControllerHost; context?: ContextConsumer<{ __context__: WeavyType }, LitElement>; whenContext: Promise; resolveContext?: (value: void | PromiseLike) => void; whenObserver: Promise>; resolveObserver?: ( value: | MutationObserver | PromiseLike> ) => void; observer?: MutationObserver; result?: MutationObserverResult; abortController?: AbortController; private observerUnsubscribe?: () => void; constructor(host: ReactiveControllerHost) { host.addController(this); this.host = host; this.whenContext = new Promise((r) => (this.resolveContext = r)); this.whenObserver = new Promise((r) => (this.resolveObserver = r)); void this.setContext(); } async setContext() { 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 trackMutation( options: MutationObserverOptions, queryClient?: QueryClient ) { if (!queryClient) { await this.whenContext; queryClient = this.context?.value?.queryClient; } if (!queryClient) { throw new Error("No QueryClient provided"); } this.observerUnsubscribe?.(); if (this.observer) { // Reset promise if observer is set before this.whenObserver = new Promise((r) => (this.resolveObserver = r)); } this.observer = new MutationObserver(queryClient, { ...options }); this.observerSubscribe(); this.resolveObserver?.(this.observer); return this.observer; } observerSubscribe() { if (this.observer) { this.result = this.observer.getCurrentResult(); this.observerUnsubscribe ??= this.observer.subscribe(() => { if (this.observer) { const nextResult = replaceEqualDeep(this.result, this.observer.getCurrentResult()); if (this.result !== nextResult) { //console.log("update", this.result, nextResult) this.result = nextResult; this.host.requestUpdate(); } } }); this.host.requestUpdate(); } } untrackMutation() { this.observerUnsubscribe?.(); this.observerUnsubscribe = undefined; this.result = undefined; this.observer = undefined; this.host.requestUpdate(); } async mutate(variables: TVariables, options?: MutateOptions) { const observer = await this.whenObserver; const abortController = new AbortController(); return observer.mutate( { ...variables, signal: abortController.signal, abort: abortController.abort.bind(abortController), }, options ); } hostConnected() { this.observerSubscribe(); } hostDisconnected() { // Clear the subscription when the host is disconnected this.observerUnsubscribe?.(); } }