import type { ExecutionResult } from 'graphql' ; import type { CacheType } from '../Cache' ; import type { GQtyError } from '../Error' ; import type { Selection } from '../Selection' ; export interface FetchEventData { label ? : string ; executionResult ? : Pick < ExecutionResult , 'data' | 'extensions' > ; error ? : GQtyError ; query : string ; variables : Record < string , unknown > | undefined ; cacheSnapshot : CacheType ; selections : Selection [ ] ; type : 'query' | 'mutation' | 'subscription' ; } export interface CacheChangeEventData { selection : Selection ; data : unknown ; } interface OnFetchEventFn { ( data , selections ) : void ; } interface OnCacheChangeEventFn { ( data ) : void ; } export class EventHandler { public hasFetchSubscribers = false ; private onFetchListeners = new Set < OnFetchEventFn > ( ) ; private onCacheChangeListeners = new Set < OnCacheChangeEventFn > ( ) ; public sendCacheChange ( data ) { for ( const listener of this . onCacheChangeListeners ) listener ( data ) ; } public sendFetchPromise ( data , selections ) { for ( const listener of this . onFetchListeners ) listener ( data , selections ) ; } public onCacheChangeSubscribe ( fn ) { const self = this ; self . onCacheChangeListeners . add ( fn ) ; return function unsubscribe ( ) { self . onCacheChangeListeners . delete ( fn ) ; } ; } public onFetchSubscribe ( fn ) { const self = this ; self . hasFetchSubscribers = Boolean ( self . onFetchListeners . add ( fn ) . size ) ; return function unsubscribe ( ) { self . onFetchListeners . delete ( fn ) ; self . hasFetchSubscribers = Boolean ( self . onFetchListeners . size ) ; } ; } }