import { Middleware , MiddlewarePayload , MiddlewareNext } from '@amplitude/types' ; export class MiddlewareRunner { private readonly _middlewares : Middleware [ ] = [ ] ; param middleware add ( middleware ) { this . _middlewares . push ( middleware ) ; } param payload param next run ( payload , next ) { let curMiddlewareIndex = - 1 ; const middlewareCount = this . _middlewares . length ; const middlewareNext = curPayload => { curMiddlewareIndex += 1 ; if ( curMiddlewareIndex < middlewareCount ) { this . _middlewares [ curMiddlewareIndex ] ( curPayload , _next ) ; } else { next ( curPayload ) ; } } ; const _next = middlewareCount > 0 ? middlewareNext : next ; _next ( payload ) ; } }