From 416d622df4d19addfe35ed1ac5f470b8df1e4f1f Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 16 Jun 2021 18:01:06 -0600 Subject: [PATCH] updates to bootstrapper --- client/lib/patch-db.ts | 37 ++++++++++++------------- client/package.json | 2 +- client/tests/mocks/bootstrapper.mock.ts | 5 ---- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index cff4772..68cb98d 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -1,5 +1,5 @@ import { EMPTY, from, merge, Observable, of, Subject, timer } from 'rxjs' -import { catchError, concatMap, delay, finalize, map, skip, take, takeUntil, tap, throttleTime } from 'rxjs/operators' +import { catchError, concatMap, debounce, debounceTime, delay, finalize, map, skip, take, takeUntil, tap, throttleTime } from 'rxjs/operators' import { Source } from './source/source' import { Dump, SequenceStore, Result, Revision } from './sequence-store' import { Store } from './store' @@ -23,15 +23,14 @@ export class PatchDB { let sequence: number = 0 let data: T = { } as T - if (bootstrapper) { - try { - const cache = await bootstrapper.init() - console.log('FROM CACHE', cache) - sequence = cache.sequence - data = cache.data - } catch (e) { - console.error('bootstrapper failed: ', e) - } + try { + const cache = await bootstrapper.init() + console.log('bootstrapped: ', cache) + sequence = cache.sequence + data = cache.data + } catch (e) { + // @TODO what to do if bootstrapper fails? + console.error('bootstrapper failed: ', e) } const store = new Store(data) @@ -39,25 +38,24 @@ export class PatchDB { const sequenceStore = new SequenceStore(store, sequence) // update cache when sequenceStore emits, throttled - if (bootstrapper) { - sequenceStore.watch$().pipe(throttleTime(500), delay(500), skip(1)).subscribe(({ data, sequence }) => { - console.log('PATCHDB - update cache(): ', sequence, data) - bootstrapper.update({ sequence, data }).catch(e => { - console.error('Exception in updateCache: ', e) - }) + sequenceStore.watch$().pipe(debounceTime(500), skip(1)).subscribe(({ data, sequence }) => { + console.log('PATCHDB - update cache(): ', sequence, data) + bootstrapper.update({ sequence, data }).catch(e => { + console.error('Exception in updateCache: ', e) }) - } + }) return new PatchDB(sources, http, sequenceStore, timeoutForMissingRevision) } sync$ (): Observable { console.log('PATCHDB - sync$()') + const sequence$ = this.sequenceStore.watch$().pipe(map(cache => cache.sequence)) // nested concatMaps, as it is written, ensure sync is not run for update2 until handleSyncResult is complete for update1. // flat concatMaps would allow many syncs to run while handleSyncResult was hanging. We can consider such an idea if performance requires it. return merge(...this.sources.map(s => s.watch$(sequence$))).pipe( - tap(update => console.log('PATCHDB - sources updated:', update)), + tap(update => console.log('PATCHDB - source updated:', update)), concatMap(update => this.sequenceStore.update$(update).pipe( concatMap(res => this.handleSyncResult$(res)), @@ -109,7 +107,7 @@ export class PatchDB { export type PatchDbConfig = { http: Http sources: Source[] - bootstrapper?: Bootstrapper + bootstrapper: Bootstrapper timeoutForMissingRevision?: number } @@ -127,7 +125,6 @@ export interface Http { export interface Bootstrapper { init (): Promise> update (cache: DBCache): Promise - clear (): Promise } export interface DBCache{ diff --git a/client/package.json b/client/package.json index 0a18c77..2eebd2a 100644 --- a/client/package.json +++ b/client/package.json @@ -28,8 +28,8 @@ "chai": "^4.2.0", "chai-string": "^1.5.0", "mocha": "^8.2.1", - "tslint": "^6.1.0", "ts-node": "^9.1.1", + "tslint": "^6.1.0", "typescript": "4.1.5" } } diff --git a/client/tests/mocks/bootstrapper.mock.ts b/client/tests/mocks/bootstrapper.mock.ts index 0c74499..d93f4e0 100644 --- a/client/tests/mocks/bootstrapper.mock.ts +++ b/client/tests/mocks/bootstrapper.mock.ts @@ -18,9 +18,4 @@ export class MockBootstrapper implements Bootstrapper { this.sequence = cache.sequence this.data = cache.data } - - async clear (): Promise { - this.sequence = 0 - this.data = { } as T - } } \ No newline at end of file