diff --git a/client/lib/json-patch-lib.ts b/client/lib/json-patch-lib.ts index 7330260..361a5af 100644 --- a/client/lib/json-patch-lib.ts +++ b/client/lib/json-patch-lib.ts @@ -1,4 +1,4 @@ -import { DBCache, PatchOp } from './types' +import { Dump, PatchOp } from './types' export interface BaseOperation { path: string @@ -37,10 +37,10 @@ export function getValueByPointer>( } export function applyOperation( - doc: DBCache>, + doc: Dump>, { path, op, value }: Operation & { value?: T }, ) { - doc.data = recursiveApply(doc.data, arrayFromPath(path), op, value) + doc.value = recursiveApply(doc.value, arrayFromPath(path), op, value) } export function arrayFromPath(path: string): string[] { @@ -61,7 +61,7 @@ export function pathFromArray(args: Array): string { args .map(a => String(a) - // do not change order, "~" needs to be replaced first + // do not change order, "~" needs to be replaced first .replace(new RegExp('~', 'g'), '~0') .replace(new RegExp('/', 'g'), '~1'), ) diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index 4051150..0587c84 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -1,4 +1,4 @@ -import { Bootstrapper, DBCache, Dump, Revision, Update } from './types' +import { Dump, Revision, Update } from './types' import { BehaviorSubject, filter, @@ -24,24 +24,21 @@ export class PatchDB { } } = {} - readonly cache$ = new BehaviorSubject>({ - sequence: 0, - data: {} as T, - }) + constructor( + private readonly source$: Observable[]>, + private readonly cache$ = new BehaviorSubject>({ + id: 0, + value: {} as T, + }), + ) {} - constructor(private readonly source$: Observable[]>) {} - - start(bootstrapper: Bootstrapper) { + start() { if (this.sub) return - const initialCache = bootstrapper.init() - this.cache$.next(initialCache) - this.sub = this.source$ .pipe(withLatestFrom(this.cache$)) .subscribe(([updates, cache]) => { this.proccessUpdates(updates, cache) - bootstrapper.update(cache) }) } @@ -52,7 +49,7 @@ export class PatchDB { this.watchedNodes = {} this.sub.unsubscribe() this.sub = null - this.cache$.next({ sequence: 0, data: {} as T }) + this.cache$.next({ id: 0, value: {} as T }) } watch$(): Observable @@ -131,14 +128,13 @@ export class PatchDB { > watch$(...args: (string | number)[]): Observable { return this.cache$.pipe( - filter(({ sequence }) => !!sequence), + filter(({ id }) => !!id), take(1), - switchMap(({ data }) => { + switchMap(({ value }) => { const path = pathFromArray(args) if (!this.watchedNodes[path]) { - const value = getValueByPointer(data, path) this.watchedNodes[path] = { - subject: new BehaviorSubject(value), + subject: new BehaviorSubject(getValueByPointer(value, path)), pathArr: arrayFromPath(path), } } @@ -147,21 +143,21 @@ export class PatchDB { ) } - proccessUpdates(updates: Update[], cache: DBCache) { + proccessUpdates(updates: Update[], cache: Dump) { updates.forEach(update => { if (this.isRevision(update)) { - const expected = cache.sequence + 1 + const expected = cache.id + 1 if (update.id < expected) return this.handleRevision(update, cache) } else { this.handleDump(update, cache) } - cache.sequence = update.id + cache.id = update.id }) this.cache$.next(cache) } - private handleRevision(revision: Revision, cache: DBCache): void { + private handleRevision(revision: Revision, cache: Dump): void { // apply opperations revision.patch.forEach(op => { applyOperation(cache, op) @@ -172,14 +168,14 @@ export class PatchDB { const arr = arrayFromPath(path) return startsWith(pathArr, arr) || startsWith(arr, pathArr) }) - if (match) this.updateWatchedNode(watchedPath, cache.data) + if (match) this.updateWatchedNode(watchedPath, cache.value) }) } - private handleDump(dump: Dump, cache: DBCache): void { - cache.data = { ...dump.value } + private handleDump(dump: Dump, cache: Dump): void { + cache.value = { ...dump.value } Object.keys(this.watchedNodes).forEach(watchedPath => { - this.updateWatchedNode(watchedPath, cache.data) + this.updateWatchedNode(watchedPath, cache.value) }) } diff --git a/client/lib/types.ts b/client/lib/types.ts index 8545ffa..a5bb75b 100644 --- a/client/lib/types.ts +++ b/client/lib/types.ts @@ -1,12 +1,10 @@ import { Operation } from './json-patch-lib' -// revise a collection of nodes. export type Revision = { id: number patch: Operation[] } -// dump/replace the entire store with T export type Dump = { id: number; value: T } export type Update = Revision | Dump @@ -16,13 +14,3 @@ export enum PatchOp { REMOVE = 'remove', REPLACE = 'replace', } - -export interface Bootstrapper> { - init(): DBCache - update(cache: DBCache): void -} - -export interface DBCache> { - sequence: number - data: T -}