UNPKG

sanity

Version:

Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches

24 lines (19 loc) 716 B
import {type Observable, type OperatorFunction} from 'rxjs' import {catchError, tap} from 'rxjs/operators' type Selector<T, K> = (error: any, attemptNo: number, caught: Observable<T>) => Observable<K> export function catchWithCount<T, K = T>(selector: Selector<T, K>): OperatorFunction<T, K> export function catchWithCount<T, K = T>(selector: Selector<T, K>) { return (input$: Observable<T>) => { let errors: any[] = [] const errorOp: OperatorFunction<any, any> = catchError((err, caught: Observable<T>) => { errors.push(err) return selector(err, errors.length, caught).pipe(errorOp) }) return input$.pipe( tap(() => { errors = [] }), errorOp, ) } }