fix: properly handle source errors without stopping the stream

This commit is contained in:
waterplea
2022-06-29 11:51:28 +03:00
parent 9a28793f27
commit 915433f877

View File

@@ -1,5 +1,5 @@
import { merge, Observable, ReplaySubject, Subject } from 'rxjs' import { EMPTY, merge, Observable, ReplaySubject, Subject } from 'rxjs'
import { switchMap } from 'rxjs/operators' import { catchError, switchMap } from 'rxjs/operators'
import { Store } from './store' import { Store } from './store'
import { DBCache, Http } from './types' import { DBCache, Http } from './types'
import { RPCError } from './source/ws-source' import { RPCError } from './source/ws-source'
@@ -12,19 +12,20 @@ export class PatchDB<T> {
public cache$ = new ReplaySubject<DBCache<T>>(1) public cache$ = new ReplaySubject<DBCache<T>>(1)
private sub = this.sources$.pipe( private sub = this.sources$.pipe(
switchMap(sources => merge(...sources.map(s => s.watch$(this.store)))), switchMap(sources => merge(...sources.map(s => s.watch$(this.store))).pipe(
).subscribe({ catchError(e => {
next: (res) => { this.connectionError$.next(e)
if ('result' in res) {
this.store.update(res.result) return EMPTY
this.cache$.next(this.store.cache) }),
} else { )),
this.rpcError$.next(res) ).subscribe((res) => {
} if ('result' in res) {
}, this.store.update(res.result)
error: (e) => { this.cache$.next(this.store.cache)
this.connectionError$.next(e) } else {
}, this.rpcError$.next(res)
}
}) })
constructor ( constructor (