import { AsyncIterableX } from '../asynciterablex' ; import { MonoTypeOperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export class DefaultIfEmptyAsyncIterable < TSource > extends AsyncIterableX < TSource > { private _source : AsyncIterable < TSource > ; private _defaultValue : TSource ; constructor ( source , defaultValue ) { super ( ) ; this . _source = source ; this . _defaultValue = defaultValue ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; let state = 1 ; for await ( const item of wrapWithAbort ( this . _source , signal ) ) { state = 2 ; yield item ; } if ( state === 1 ) { yield this . _defaultValue ; } } } template T param { T } defaultValue returns { MonoTypeOperatorAsyncFunction < T > } export function defaultIfEmpty < T > ( defaultValue ) { return function defaultIfEmptyOperatorFunction ( source ) : AsyncIterableX < T > { return new DefaultIfEmptyAsyncIterable < T > ( source , defaultValue ) ; } ; }