import { Observer , Observable } from "../../utils/observable" ; import { sleep } from "../../sleep" ; export function trackReadiness < T > ( interval , f ) { let refreshCount = 0 ; const observers = [ ] ; const observable = { subscribe ( observer ) { observers . push ( observer ) ; } , async notify ( value ) { for ( const o of observers ) { await o . onNext ( value ) ; } } , async run ( ) { while ( true ) { await sleep ( interval ) ; const [ value , retry ] = await f ( refreshCount ) ; await this . notify ( value ) ; if ( ! retry ) { while ( observers . length > 0 ) { observers . unshift ( ) ; } return ; } ++ refreshCount ; } } } ; observable . run ( ) ; return observable ; }