import { from, Observable, of, repeat } from 'rxjs' import { concatMap, take } from 'rxjs/operators' import { Store } from '../store' import { Http, Update } from '../types' import { Source } from './source' export type PollConfig = { cooldown: number } export class PollSource implements Source { constructor( private readonly pollConfig: PollConfig, private readonly http: Http, ) {} watch$({ sequence$ }: Store): Observable | null> { return sequence$.pipe( concatMap(seq => this.http.getRevisions(seq)), take(1), concatMap(res => { // If Revision[] if (Array.isArray(res)) { // Convert Revision[] it into Observable OR return null return res.length ? from(res) : of(null) // If Dump } // Convert Dump into Observable> return of(res) }), repeat({ delay: this.pollConfig.cooldown }), ) } }