import { AsyncIterableX } from '../asynciterablex' ; import { toArray } from '../toarray' ; import { OperatorAsyncFunction } from '../../interfaces' ; import { ScanOptions } from './scanoptions' ; import { throwIfAborted } from '../../aborterror' ; export class ScanRightAsyncIterable < T , R > extends AsyncIterableX < R > { private _source : AsyncIterable < T > ; private _fn : ( acc : R , x : T , index : number , signal ? : AbortSignal ) => R | Promise < R > ; private _seed ? : T | R ; private _hasSeed : boolean ; constructor ( source , options ) { super ( ) ; this . _source = source ; this . _fn = options [ 'callback' ] ; this . _hasSeed = options . hasOwnProperty ( 'seed' ) ; this . _seed = options [ 'seed' ] ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; let hasValue = false ; let acc = this . _seed ; const source = await toArray ( this . _source , signal ) ; for ( let offset = source . length - 1 ; offset >= 0 ; offset -- ) { const item = source [ offset ] ; if ( hasValue || ( hasValue = this . _hasSeed ) ) { acc = await this . _fn ( < R > acc , item , offset , signal ) ; yield acc ; } else { acc = item ; hasValue = true ; } } } } template T template R param { ScanOptions < T , R > } options returns { OperatorAsyncFunction < T , R > } export function scanRight < T , R = T > ( options ) { return function scanRightOperatorFunction ( source ) : AsyncIterableX < R > { return new ScanRightAsyncIterable ( source , options ) ; } ; }