Skip to content

Commit

Permalink
don't emit until bootstrapped with data (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattDHill authored Sep 21, 2022
1 parent 20beb61 commit 4d987b1
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions client/lib/patch-db.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,7 +23,10 @@ export class PatchDB<T extends { [key: string]: any }> {
}
} = {}

readonly cache$ = new BehaviorSubject({ sequence: 0, data: {} as T })
readonly cache$ = new BehaviorSubject<DBCache<T>>({
sequence: 0,
data: {} as T,
})

constructor(private readonly source$: Observable<Update<T>[]>) {}

Expand Down Expand Up @@ -118,18 +129,21 @@ export class PatchDB<T extends { [key: string]: any }> {
>
>
watch$(...args: (string | number)[]): Observable<any> {
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<T>[], cache: DBCache<T>) {
Expand Down

0 comments on commit 4d987b1

Please sign in to comment.