import { DBCache, Dump, Revision, Update } from './types' import { applyPatch, getValueByPointer } from 'fast-json-patch' import { BehaviorSubject, Observable } from 'rxjs' import { finalize } from 'rxjs/operators' export class Store { cache: DBCache sequence$: BehaviorSubject private nodes: { [path: string]: BehaviorSubject } = { } constructor ( readonly initialCache: DBCache, ) { this.cache = initialCache this.sequence$ = new BehaviorSubject(initialCache.sequence) } watch$ (): Observable watch$ (p1: P1): Observable watch$ (p1: P1, p2: P2): Observable watch$ (p1: P1, p2: P2, p3: P3): Observable watch$ (p1: P1, p2: P2, p3: P3, p4: P4): Observable watch$ (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5): Observable watch$ (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6): Observable watch$ (...args: (string | number)[]): Observable { const path = `/${args.join('/')}` if (!this.nodes[path]) { this.nodes[path] = new BehaviorSubject(getValueByPointer(this.cache.data, path)) this.nodes[path].pipe( finalize(() => delete this.nodes[path]), ) } return this.nodes[path].asObservable() } update (update: Update): void { if ((update as Revision).patch) { if (this.cache.sequence + 1 !== update.id) throw new Error(`Outdated sequence: current: ${this.cache.sequence}, new: ${update.id}`) applyPatch(this.cache.data, (update as Revision).patch, true, true); (update as Revision).patch.forEach(op => { this.updateNodesByPath(op.path) }) } else { this.cache.data = (update as Dump).value this.updateNodesByPath('') } this.cache.sequence = update.id this.sequence$.next(this.cache.sequence) } updateNodesByPath (revisionPath: string) { Object.keys(this.nodes).forEach(nodePath => { if (!this.nodes[nodePath]) return if (nodePath.includes(revisionPath) || revisionPath.includes(nodePath)) { try { this.nodes[nodePath].next(getValueByPointer(this.cache.data, nodePath)) } catch (e) { this.nodes[nodePath].complete() delete this.nodes[nodePath] } } }) } reset (): void { Object.values(this.nodes).forEach(node => node.complete()) } }