import { readable , derived , Readable } from 'svelte/store' ; import { QueryResult } from '../getQuery' ; export function getRefetchContainer ( ) { let hasResolved = false ; let previousValue ; let setState = ( state ) => { } ; const dataStore = readable ( { isInFlight : false } , ( set ) => { setState = set ; } ) ; function refetchContainer < T extends QueryResult < any > > ( query ) { const derivedStore = derived ( query , ( $query , set ) => { setState ( { isInFlight : true } ) ; if ( ! hasResolved ) { $query . then ( ( ) => { previousValue = $query ; hasResolved = true ; setState ( { isInFlight : false } ) ; } ) ; set ( $query ) ; } else { $query . then ( ( ) => { previousValue = $query ; set ( $query ) ; setState ( { isInFlight : false } ) ; } ) ; set ( previousValue ) ; } } ) ; return derivedStore as any ; } refetchContainer . subscribe = ( ... args ) => dataStore . subscribe ( ... args ) ; return refetchContainer ; }