import { IterableX } from '../iterablex' ; import { returnIterator } from '../../util/returniterator' ; import { OperatorFunction } from '../../interfaces' ; export class CatchWithIterable < TSource , TResult > extends IterableX < TSource | TResult > { private _source : Iterable < TSource > ; private _handler : ( error : any ) => Iterable < TResult > ; constructor ( source , handler ) { super ( ) ; this . _source = source ; this . _handler = handler ; } * [ Symbol . iterator ] ( ) { let err ; let hasError = false ; const it = this . _source [ Symbol . iterator ] ( ) ; while ( 1 ) { let done ; let value ; try { ( { done , value } = it . next ( ) ) ; if ( done ) { returnIterator ( it ) ; break ; } } catch ( e ) { err = this . _handler ( e ) ; hasError = true ; returnIterator ( it ) ; break ; } yield value ; } if ( hasError ) { for ( const item of err ! ) { yield item ; } } } } template TSource template TResult param { ( error : any ) => Iterable < TResult > } handler returns { ( OperatorFunction < TSource , TSource | TResult > ) } export function catchError < TSource , TResult > ( handler ) { return function catchWithOperatorFunction ( source ) : IterableX < TSource | TResult > { return new CatchWithIterable < TSource , TResult > ( source , handler ) ; } ; }