import type { GraphQLVariables, QueryResult, SubscriptionArtifact, GraphQLObject, } from 'houdini/runtime' import { CompiledSubscriptionKind } from 'houdini/runtime' import { derived, writable, type Subscriber, type Writable } from 'svelte/store' import { initClient } from '../client.js' import { getSession } from '../session.js' import { BaseStore } from './base.js' export class SubscriptionStore< _Data extends GraphQLObject, _Input extends GraphQLVariables | null | undefined, > extends BaseStore<_Data, _Input, SubscriptionArtifact> { kind = CompiledSubscriptionKind fetchingStore: Writable constructor({ artifact }: { artifact: SubscriptionArtifact }) { super({ artifact }) this.fetchingStore = writable(false) } async listen(variables?: _Input, args?: { metadata: App.Metadata }) { this.fetchingStore.set(true) await initClient() this.observer.send({ variables, session: await getSession(), metadata: args?.metadata, }) } async unlisten() { this.fetchingStore.set(false) await initClient() await this.observer.cleanup() } subscribe( run: Subscriber>, invalidate?: ((value?: QueryResult<_Data, _Input> | undefined) => void) | undefined ): () => void { // add the local fetching store to the default behavior return derived( [{ subscribe: super.subscribe.bind(this) }, this.fetchingStore], ([$parent, $fetching]) => ({ ...$parent, fetching: $fetching, }) ).subscribe(run, invalidate) } }