import { queryClient } from '$shared/api/queryClient'; import { QueryObserver, type QueryObserverOptions, type QueryObserverResult, } from '@tanstack/query-core'; /** * Abstract base class for reactive Svelte 5 stores backed by TanStack Query. * * Provides a unified way to use TanStack Query observers within Svelte 5 classes * using runes for reactivity. Handles subscription lifecycle automatically. * * @template TData - The type of data returned by the query. * @template TError - The type of error that can be thrown. */ export abstract class BaseQueryStore { #result = $state>({} as QueryObserverResult); #observer: QueryObserver; #unsubscribe: () => void; constructor(options: QueryObserverOptions) { this.#observer = new QueryObserver(queryClient, options); this.#unsubscribe = this.#observer.subscribe(result => { this.#result = result; }); } /** * Current query result (reactive) */ protected get result(): QueryObserverResult { return this.#result; } /** * Updates observer options dynamically. * Use this when query parameters or dependencies change. */ protected updateOptions(options: QueryObserverOptions): void { this.#observer.setOptions(options); } /** * Cleans up the observer subscription. * Should be called when the store is no longer needed. */ destroy(): void { this.#unsubscribe(); } }