From 05927df4b9a2c643014ae84110589920e484d5b7 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 21 Sep 2022 10:49:01 -0600 Subject: [PATCH] don't emit until bootstrapped with data --- client/lib/patch-db.ts | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index dd59c7f..636522a 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -1,5 +1,13 @@ import { Bootstrapper, DBCache, Dump, Revision, Update } from './types' -import { BehaviorSubject, Observable, Subscription, withLatestFrom } from 'rxjs' +import { + BehaviorSubject, + filter, + Observable, + Subscription, + switchMap, + take, + withLatestFrom, +} from 'rxjs' import { applyOperation, getValueByPointer, @@ -15,7 +23,10 @@ export class PatchDB { } } = {} - readonly cache$ = new BehaviorSubject({ sequence: 0, data: {} as T }) + readonly cache$ = new BehaviorSubject>({ + sequence: 0, + data: {} as T, + }) constructor(private readonly source$: Observable[]>) {} @@ -118,18 +129,21 @@ export class PatchDB { > > watch$(...args: (string | number)[]): Observable { - const path = args.length ? `/${args.join('/')}` : '' - - if (!this.watchedNodes[path]) { - const data = this.cache$.value.data - const value = getValueByPointer(data, path) - this.watchedNodes[path] = { - subject: new BehaviorSubject(value), - pathArr: jsonPathToKeyArray(path), - } - } - - return this.watchedNodes[path].subject + return this.cache$.pipe( + filter(({ sequence }) => !!sequence), + take(1), + switchMap(({ data }) => { + const path = args.length ? `/${args.join('/')}` : '' + if (!this.watchedNodes[path]) { + const value = getValueByPointer(data, path) + this.watchedNodes[path] = { + subject: new BehaviorSubject(value), + pathArr: jsonPathToKeyArray(path), + } + } + return this.watchedNodes[path].subject + }), + ) } proccessUpdates(updates: Update[], cache: DBCache) {