import { MiddlewareOverload } from "../middleware" ; import { findOrCreateRemote , IS_SERVER } from "../internal" ; import MiddlewareEvent from "./MiddlewareEvent" ; import MiddlewareFunction from "./MiddlewareFunction" ; export default class ServerFunction < CallbackArgs extends ReadonlyArray < unknown > = Array < unknown > , Returns extends unknown = unknown > extends MiddlewareFunction { private instance : RemoteFunction ; public static readonly DefaultFunctionHook = ( ) => { return undefined ; } ; constructor ( name , middlewares = [ ] ) { super ( middlewares ) ; this . instance = findOrCreateRemote ( "RemoteFunction" , name , ( instance ) => { instance . OnServerInvoke = ServerFunction . DefaultFunctionHook ; } ) ; assert ( IS_SERVER , "Cannot create a Net.ServerFunction on the Client!" ) ; } internal public GetInstance ( ) { return this . instance ; } public SetCallback < R extends Returns > ( callback ) { this . instance . OnServerInvoke = ( player , ... args ) => { const result = this . _processMiddleware < CallbackArgs , R > ( callback ) ?. ( player , ... ( args as CallbackArgs ) ) ; if ( Promise . is ( result ) ) { warn ( "[rbx-net] WARNING: Promises should be used with an AsyncFunction!" ) ; const [ success , value ] = result . await ( ) ; if ( success ) { return value ; } else { throw value ; } } return result ; } ; } }