import { AsyncIterableX } from '../asynciterablex' ; import { MonoTypeOperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export class ThrottleAsyncIterable < TSource > extends AsyncIterableX < TSource > { private _source : AsyncIterable < TSource > ; private _time : number ; constructor ( source , time ) { super ( ) ; this . _source = source ; this . _time = time ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; let currentTime ; let previousTime ; for await ( const item of wrapWithAbort ( this . _source , signal ) ) { currentTime = Date . now ( ) ; if ( ! previousTime || currentTime - previousTime > this . _time ) { previousTime = currentTime ; yield item ; } } } } template TSource param { number } time returns { MonoTypeOperatorAsyncFunction < TSource > } export function throttle < TSource > ( time ) { return function throttleOperatorFunction ( source ) : AsyncIterableX < TSource > { return new ThrottleAsyncIterable < TSource > ( source , time ) ; } ; }