refactor: remove nested subscription (#36)

This commit is contained in:
Alex Inkin
2022-06-23 00:23:35 +03:00
committed by GitHub
parent e138b88e75
commit 9a28793f27

View File

@@ -1,4 +1,5 @@
import { merge, Observable, ReplaySubject, Subject, Subscription } from 'rxjs'
import { merge, Observable, ReplaySubject, Subject } from 'rxjs'
import { switchMap } from 'rxjs/operators'
import { Store } from './store'
import { DBCache, Http } from './types'
import { RPCError } from './source/ws-source'
@@ -10,15 +11,14 @@ export class PatchDB<T> {
public rpcError$ = new Subject<RPCError>()
public cache$ = new ReplaySubject<DBCache<T>>(1)
private updatesSub?: Subscription
private sourcesSub = this.sources$.subscribe(sources => {
this.updatesSub = merge(...sources.map(s => s.watch$(this.store))).subscribe({
private sub = this.sources$.pipe(
switchMap(sources => merge(...sources.map(s => s.watch$(this.store)))),
).subscribe({
next: (res) => {
if ('result' in res) {
this.store.update(res.result)
this.cache$.next(this.store.cache)
}
else {
} else {
this.rpcError$.next(res)
}
},
@@ -26,7 +26,6 @@ export class PatchDB<T> {
this.connectionError$.next(e)
},
})
})
constructor (
private readonly sources$: Observable<Source<T>[]>,
@@ -35,7 +34,6 @@ export class PatchDB<T> {
) { }
clean () {
this.sourcesSub.unsubscribe()
this.updatesSub?.unsubscribe()
this.sub.unsubscribe()
}
}