set connection error null if poll returns 2xx

This commit is contained in:
Matt Hill
2022-08-16 09:56:05 -06:00
parent 9cccf57991
commit caf53a9141
4 changed files with 16 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
import { EMPTY, map, merge, Observable, shareReplay, Subject } from 'rxjs'
import { catchError, switchMap } from 'rxjs/operators'
import { catchError, filter, switchMap, tap } from 'rxjs/operators'
import { Store } from './store'
import { DBCache, Http } from './types'
import { Source } from './source/source'
@@ -17,9 +17,10 @@ export class PatchDB<T> {
}),
),
),
tap(_ => this.connectionError$.next(null)),
filter(Boolean),
map(res => {
this.store.update(res)
return this.store.cache
}),
shareReplay(1),

View File

@@ -14,12 +14,20 @@ export class PollSource<T> implements Source<T> {
private readonly http: Http<T>,
) {}
watch$({ sequence$ }: Store<T>): Observable<Update<T>> {
watch$({ sequence$ }: Store<T>): Observable<Update<T> | null> {
return sequence$.pipe(
concatMap(seq => this.http.getRevisions(seq)),
take(1),
// convert Revision[] it into Observable<Revision>. Convert Dump<T> into Observable<Dump<T>>
concatMap(res => (Array.isArray(res) ? from(res) : of(res))),
concatMap(res => {
// If Revision[]
if (Array.isArray(res)) {
// Convert Revision[] it into Observable<Revision> OR return null
return res.length ? from(res) : of(null)
// If Dump<T>
}
// Convert Dump<T> into Observable<Dump<T>>
return of(res)
}),
repeat({ delay: this.pollConfig.cooldown }),
)
}

View File

@@ -3,5 +3,5 @@ import { Store } from '../store'
import { Update } from '../types'
export interface Source<T> {
watch$(store?: Store<T>): Observable<Update<T>>
watch$(store?: Store<T>): Observable<Update<T> | null>
}

View File

@@ -15,7 +15,7 @@
"uuid": "8.3.2"
},
"peerDependencies": {
"rxjs": ">=6.0.0"
"rxjs": ">=7.0.0"
},
"devDependencies": {
"@types/node": "16.4.13",