diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index de96130..58a1a13 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -2,9 +2,9 @@ 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' +import { DBCache, Http } from './types' -export class PatchDB { +export class PatchDB { store: Store constructor ( diff --git a/client/lib/source/source.ts b/client/lib/source/source.ts index 300ad0a..45cf1b8 100644 --- a/client/lib/source/source.ts +++ b/client/lib/source/source.ts @@ -1,7 +1,7 @@ import { Observable } from 'rxjs' import { Store } from '../store' -import { HashMap, Update } from '../types' +import { Update } from '../types' -export interface Source { +export interface Source { watch$ (store?: Store): Observable> } diff --git a/client/lib/store.ts b/client/lib/store.ts index 7035244..abdf608 100644 --- a/client/lib/store.ts +++ b/client/lib/store.ts @@ -1,4 +1,4 @@ -import { DBCache, Dump, HashMap, Http, Revision, Update } from './types' +import { DBCache, Dump, Http, Revision, Update } from './types' import { BehaviorSubject, Observable } from 'rxjs' import { applyOperation, getValueByPointer, Operation } from './json-patch-lib' import BTree from 'sorted-btree' @@ -8,7 +8,7 @@ export interface StashEntry { undo: Operation[] } -export class Store { +export class Store { cache: DBCache sequence$: BehaviorSubject private watchedNodes: { [path: string]: BehaviorSubject } = { } @@ -66,9 +66,13 @@ export class Store { } private handleDump (dump: Dump): void { - Object.keys(this.cache.data).forEach(key => delete this.cache.data[key]) - Object.assign(this.cache.data, dump) - this.cache.data = dump.value + Object.keys(this.cache.data).forEach(key => { + if (dump.value[key] !== undefined) { + (this.cache.data as any)[key] = dump.value[key] + } else { + delete this.cache.data[key] + } + }) this.stash.deleteRange(this.cache.sequence, dump.id, false) this.updateWatchedNodes('') this.updateSequence(dump.id) diff --git a/client/lib/types.ts b/client/lib/types.ts index ae683a0..278aaa9 100644 --- a/client/lib/types.ts +++ b/client/lib/types.ts @@ -3,9 +3,9 @@ import { Operation } from './json-patch-lib' // revise a collection of nodes. export type Revision = { id: number, patch: Operation[], expireId: string | null } // dump/replace the entire store with T -export type Dump = { id: number, value: T, expireId: string | null } +export type Dump = { id: number, value: T, expireId: string | null } -export type Update = Revision | Dump +export type Update = Revision | Dump export enum PatchOp { ADD = 'add', @@ -13,20 +13,17 @@ export enum PatchOp { REPLACE = 'replace', } -export interface Http { +export interface Http { getRevisions (since: number): Promise> getDump (): Promise> } -export interface Bootstrapper { +export interface Bootstrapper { init (): Promise> update (cache: DBCache): Promise } -export interface DBCache{ +export interface DBCache{ sequence: number, data: T } - -export type HashMap = { [type: string]: any } -