import { MiddlewareOverload , NetGlobalMiddleware } from "../middleware" ; import { LegacyAsyncFunctionDeclaration , FunctionDeclaration , LegacyEventDeclaration , RemoteDeclarations , DefinitionsCreateResult , NamespaceDeclaration , ServerToClientEventDeclaration , ClientToServerEventDeclaration , BidirectionalEventDeclaration , AsyncServerFunctionDeclaration , AsyncClientFunctionDeclaration , DeclarationTypeCheck , } from "./Types" ; import { ServerDefinitionBuilder } from "./ServerDefinitionBuilder" ; import { ClientDefinitionBuilder } from "./ClientDefinitionBuilder" ; import { warnOnce } from "../internal" ; import { $nameof } from "rbxts-transform-debug" ; import { NamespaceBuilder } from "./NamespaceBuilder" ; namespace NetDefinitions { param declarations function validateDeclarations ( declarations ) { for ( const [ , declaration ] of pairs ( declarations ) ) { assert ( DeclarationTypeCheck . check ( declaration . Type ) , DeclarationTypeCheck . errorMessage ) ; } } description param declarations export function Create < T extends RemoteDeclarations > ( declarations , globalMiddleware ? ) { validateDeclarations ( declarations ) ; return identity < DefinitionsCreateResult < T > > ( { Server : new ServerDefinitionBuilder < T > ( declarations , globalMiddleware ) , Client : new ClientDefinitionBuilder < T > ( declarations ) , } ) ; } /**
     * Defines a namespace of remote definitions, which can be retrieved via `GetNamespace(namespaceId)`
     *
     * E.g.
     * ```ts
     * const Remotes = Net.Definitions.Create({
     * 		ExampleGroup: Net.Definitions.Namespace({
     * 			ExampleGroupRemote: Net.Definitions.ServerToClientEvent<[message: string]>(),
     * 		}),
     * });
     * const ExampleGroupRemote = Remotes.Server.GetNamespace("ExampleGroup").Create("ExampleGroupRemote");
     * ```
     *
     * This is useful for categorizing remotes by feature.
     */ export function Namespace < T extends RemoteDeclarations > ( declarations ) { return { Type : "Namespace" , Definitions : new NamespaceBuilder ( declarations ) , } as NamespaceDeclaration < T > ; } /**
     * Defines a function in which strictly the client can call the server asynchronously
     *
     * `Client` [`Calls`] -> `Server` [`Recieves Call`]
     * ... (asynchronously) ...
     * `Server` [`Responds to Call`] -> `Client` [`Recieves Response`]
     */ export function ServerAsyncFunction < ServerFunction extends ( ... args : any [ ] ) => defined = ( ... args : unknown [ ] ) => defined > ( mw ? ) { return { Type : "AsyncFunction" , ServerMiddleware : mw , } as AsyncServerFunctionDeclaration < Parameters < ServerFunction > , ReturnType < ServerFunction > > ; } /**
     * Defines a function in which strictly the server can call the client asynchronously
     *
     * `Server` [`Calls`] -> `Client` [`Recieves Call`]
     * ... (asynchronously) ...
     * `Client` [`Responds to Call`] -> `Server` [`Recieves Response`]
     */ export function ClientAsyncFunction < ClientFunction extends ( ... args : any [ ] ) => defined = ( ... args : unknown [ ] ) => defined > ( ) { return { Type : "AsyncFunction" , } as AsyncClientFunctionDeclaration < Parameters < ClientFunction > , ReturnType < ClientFunction > > ; } /**
     * Defines a regular function in which strictly the client can call the server synchronously
     *
     * (Synchronous) `Client` [`Calls`, `Recieves Response`] <- (yields for response) -> `Server` [`Recieves Call`, `Responds`]
     */ export function ServerFunction < ServerFunction extends ( ... args : any [ ] ) => any > ( mw ? ) { return { Type : "Function" , ServerMiddleware : mw , } as FunctionDeclaration < Parameters < ServerFunction > , ReturnType < ServerFunction > > ; } /**
     * Defines an event in which strictly the server fires an event that is recieved by clients
     *
     * `Server` [`Sends`] => `Client(s)` [`Recieves`]
     *
     * On the client, this will give an event that can use `Connect`.
     *
     * On the server, this will give an event that can use `SendToPlayer`, `SendToAllPlayers`, `SendToAllPlayersExcept`
     *
     */ export function ServerToClientEvent < ServerArgs extends readonly unknown [ ] = unknown [ ] > ( ) { return { ServerMiddleware : [ ] , Type : "Event" , } as ServerToClientEventDeclaration < ServerArgs > ; } param mw export function ClientToServerEvent < ClientArgs extends readonly unknown [ ] = unknown [ ] > ( ) ; export function ClientToServerEvent < ClientArgs extends readonly unknown [ ] > ( mw ? ) ; export function ClientToServerEvent < ClientArgs extends readonly unknown [ ] = unknown [ ] > ( mw ? ) { return { Type : "Event" , ServerMiddleware : mw , } as ClientToServerEventDeclaration < ClientArgs > ; } /**
     * Defines a remote event that can be fired both from the client and server
     *
     * This should only be required in rare use cases where `ClientToServerEvent` or `ServerToClientEvent` is not sufficient.
     */ export function BidirectionalEvent < ServerConnect extends readonly unknown [ ] = unknown [ ] , ClientConnect extends readonly unknown [ ] = unknown [ ] > ( ) ; export function BidirectionalEvent < ServerConnect extends readonly unknown [ ] = unknown [ ] , ClientConnect extends readonly unknown [ ] = unknown [ ] > ( mw ? ) ; export function BidirectionalEvent < ServerArgs extends readonly unknown [ ] = unknown [ ] , ClientArgs extends readonly unknown [ ] = unknown [ ] > ( ) { return { Type : "Event" , ServerMiddleware : [ ] , } as BidirectionalEventDeclaration < ServerArgs , ClientArgs > ; } deprecated export function Function < ServerFunction extends ( ... args : any [ ] ) => any > ( mw ? ) ; export function Function < ServerArgs extends ReadonlyArray < unknown > , ServerReturns extends unknown = undefined > ( mw ? ) ; export function Function < ServerArgs extends ReadonlyArray < unknown > , ServerReturns extends unknown = undefined > ( mw ? ) { warnOnce ( `Definition '${ $nameof ( Function ) }' is deprecated, use '${ $nameof ( ServerFunction ) }' in your declarations - https://github.com/roblox-aurora/rbx-net/issues/35` ) ; return { Type : "Function" , ServerMiddleware : mw , } as FunctionDeclaration < ServerArgs , ServerReturns > ; } deprecated export function Event < ServerArgs extends unknown [ ] = unknown [ ] , ClientArgs extends unknown [ ] = unknown [ ] > ( mw ? ) ; export function Event < ServerArgs extends unknown [ ] = unknown [ ] , ClientArgs extends unknown [ ] = unknown [ ] > ( mw ? ) { warnOnce ( `Definition '${ $nameof ( Event ) }' is deprecated, use '${ $nameof ( ServerToClientEvent ) }', '${ $nameof ( ClientToServerEvent ) }' or '${ $nameof ( BidirectionalEvent ) }' in your declarations - https://github.com/roblox-aurora/rbx-net/issues/35` ) ; return { Type : "Event" , ServerMiddleware : mw , } as LegacyEventDeclaration < ServerArgs , ClientArgs > ; } deprecated export function AsyncFunction < ServerFunction extends ( ... args : any [ ] ) => defined = ( ... args : unknown [ ] ) => defined , ClientFunction extends ( ... args : any [ ] ) => defined = ( ... args : unknown [ ] ) => defined > ( mw ? ) { warnOnce ( `Definition '${ $nameof ( AsyncFunction ) }' is deprecated, use '${ $nameof ( ServerAsyncFunction ) }' or '${ $nameof ( ClientAsyncFunction ) }' in your declarations - https://github.com/roblox-aurora/rbx-net/issues/35` ) ; return { Type : "AsyncFunction" , ServerMiddleware : mw , } as LegacyAsyncFunctionDeclaration < Parameters < ServerFunction > , ReturnType < ServerFunction > , Parameters < ClientFunction > , ReturnType < ClientFunction > > ; } } export default NetDefinitions ;