This commit is contained in:
Matt Hill
2021-07-06 16:56:15 -06:00
parent 11818232eb
commit 0e2fa3c2c0
4 changed files with 25 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
import { merge, Observable } from 'rxjs'
import { merge, Observable, of } from 'rxjs'
import { concatMap, finalize, tap } from 'rxjs/operators'
import { Source } from './source/source'
import { Store } from './store'
@@ -6,21 +6,21 @@ import { DBCache } from './types'
export { Operation } from 'fast-json-patch'
export class PatchDB<T extends object> {
export class PatchDB<T> {
store: Store<T>
constructor (
private readonly sources: Source<T>[],
readonly cache: DBCache<T>,
private readonly initialCache: DBCache<T>,
) {
this.store = new Store(cache)
this.store = new Store(this.initialCache)
}
sync$ (): Observable<DBCache<T>> {
return merge(...this.sources.map(s => s.watch$(this.store)))
.pipe(
tap(update => this.store.update(update)),
concatMap(() => this.store.watchCache$()),
concatMap(() => of(this.store.cache)),
finalize(() => {
this.store.reset()
}),