import { $ifEnv } from "rbxts-transform-env" ; import { NetGlobalMiddleware , NetMiddleware , NextCaller } from "../middleware" ; internal export type MutableMiddlewareList = Array < NetMiddleware < Array < unknown > > > ; export type MiddlewareList = ReadonlyArray < NetMiddleware < ReadonlyArray < unknown > > > ; abstract class MiddlewareEvent { protected constructor ( private readonly middlewares = [ ] ) { } internal abstract GetInstance ( ) ; internal public _use ( middleware ) { ( this . middlewares as MutableMiddlewareList ) . push ( middleware ) ; } protected _processMiddleware < A extends ReadonlyArray < unknown > , R = void > ( callback ) { const { middlewares } = this ; try { assert ( typeIs ( middlewares , "table" ) , "The middleware argument should be an array of middlewares not a " + typeOf ( middlewares ) ) ; if ( middlewares . size ( ) > 0 ) { let callbackFn = callback as NextCaller < R > ; for ( const middleware of middlewares ) { callbackFn = middleware ( callbackFn , this ) as NextCaller < R > ; } return callbackFn ; } else { return callback ; } } catch ( e ) { warn ( "[rbx-net] " + tostring ( e ) ) ; } } } export default MiddlewareEvent ;