import { merge, Observable, of } from 'rxjs' import { concatMap, finalize, tap } from 'rxjs/operators' import { Source } from './source/source' import { Store } from './store' import { DBCache, HashMap, Http } from './types' export class PatchDB { store: Store constructor ( private readonly sources: Source[], private readonly http: Http, private readonly initialCache: DBCache, ) { this.store = new Store(this.http, this.initialCache) } sync$ (): Observable> { return merge(...this.sources.map(s => s.watch$(this.store))) .pipe( tap(update => this.store.update(update)), concatMap(() => of(this.store.cache)), finalize(() => { this.store.reset() }), ) } }