import { AsyncIterableX } from '../asynciterablex' ; import { OperatorAsyncFunction } from '../../interfaces' ; import { wrapWithAbort } from './withabort' ; import { throwIfAborted } from '../../aborterror' ; export interface TimeInterval < T > { value : T ; elapsed : number ; } export class TimeIntervalAsyncIterable < TSource > extends AsyncIterableX < TimeInterval < TSource > > { private _source : AsyncIterable < TSource > ; constructor ( source ) { super ( ) ; this . _source = source ; } async * [ Symbol . asyncIterator ] ( signal ? ) { throwIfAborted ( signal ) ; let last = Date . now ( ) ; for await ( const item of wrapWithAbort ( this . _source , signal ) ) { const now = Date . now ( ) ; const span = now - last ; last = now ; yield { value : item , elapsed : span } ; } } } template TSource returns { OperatorAsyncFunction < TSource , TimeInterval < TSource > > } export function timeInterval < TSource > ( ) { return function timeIntervalOperatorFunction ( source ) : AsyncIterableX < TimeInterval < TSource > > { return new TimeIntervalAsyncIterable < TSource > ( source ) ; } ; }