From 88084f60a2298af1feb2c0cca69ab050e69003fd Mon Sep 17 00:00:00 2001 From: Drew Ansbacher Date: Wed, 21 Jul 2021 14:44:08 -0600 Subject: [PATCH] hashmap --- client/lib/patch-db.ts | 4 ++-- client/lib/store.ts | 6 ++++-- client/lib/types.ts | 13 ++++++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index 58a1a13..9f79056 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -2,7 +2,7 @@ import { merge, Observable, of } from 'rxjs' import { concatMap, finalize, tap } from 'rxjs/operators' import { Source } from './source/source' import { Store } from './store' -import { DBCache, Http } from './types' +import { DBCache, HashMap, Http } from './types' export class PatchDB { store: Store @@ -15,7 +15,7 @@ export class PatchDB { this.store = new Store(this.http, this.initialCache) } - sync$ (): Observable> { + sync$ (): Observable> { return merge(...this.sources.map(s => s.watch$(this.store))) .pipe( tap(update => this.store.update(update)), diff --git a/client/lib/store.ts b/client/lib/store.ts index 180a651..4580766 100644 --- a/client/lib/store.ts +++ b/client/lib/store.ts @@ -1,4 +1,4 @@ -import { DBCache, Dump, Http, Revision, Update } from './types' +import { DBCache, Dump, HashMap, Http, Revision, Update } from './types' import { BehaviorSubject, Observable } from 'rxjs' import { applyOperation, getValueByPointer, Operation } from './json-patch-lib' import BTree from 'sorted-btree' @@ -9,7 +9,7 @@ export interface StashEntry { } export class Store { - cache: DBCache + cache: DBCache sequence$: BehaviorSubject private watchedNodes: { [path: string]: BehaviorSubject } = { } private stash = new BTree() @@ -66,6 +66,8 @@ 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 this.stash.deleteRange(this.cache.sequence, dump.id, false) this.updateWatchedNodes('') diff --git a/client/lib/types.ts b/client/lib/types.ts index c441f5f..ae683a0 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,17 +13,20 @@ 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 } +