mirror of
https://github.com/Start9Labs/patch-db.git
synced 2026-03-26 18:31:53 +00:00
30 lines
730 B
TypeScript
30 lines
730 B
TypeScript
import { merge, Observable } from 'rxjs'
|
|
import { concatMap, finalize, tap } from 'rxjs/operators'
|
|
import { Source } from './source/source'
|
|
import { Store } from './store'
|
|
import { DBCache } from './types'
|
|
|
|
export { Operation } from 'fast-json-patch'
|
|
|
|
export class PatchDB<T extends object> {
|
|
store: Store<T>
|
|
|
|
constructor (
|
|
private readonly sources: Source<T>[],
|
|
readonly cache: DBCache<T>,
|
|
) {
|
|
this.store = new Store(cache)
|
|
}
|
|
|
|
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$()),
|
|
finalize(() => {
|
|
this.store.reset()
|
|
}),
|
|
)
|
|
}
|
|
}
|