diff --git a/client/src/index.ts b/client/src/index.ts index a7013e3..fd3dd5c 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -2,13 +2,16 @@ import rpc from 'rage-rpc' import Logger from './logger' -import type { +import { RageFW_ClientEventCallback, RageFW_ClientEvent, RageFW_ClientServerEvent, RageFW_ClientServerEventArguments, RageFW_ClientServerEventReturn, + RageFW_ClientEventArguments, + RageFW_ClientEventReturn, } from './types' +import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' class Client { public register( @@ -19,6 +22,19 @@ class Client { return callback(data) }) } + + public unregister( + eventName: EventName, + ): void { + rpc.unregister(eventName) + } + + public trigger( + eventName: EventName, + args: RageFW_ClientEventArguments, + ): Promise> { + return rpc.call>(eventName, args) + } } class Player { diff --git a/client/src/types/client.ts b/client/src/types/client.ts index e334159..afaea2c 100644 --- a/client/src/types/client.ts +++ b/client/src/types/client.ts @@ -4,8 +4,11 @@ import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent +export type RageFW_ClientEventArguments = + Parameters + export type RageFW_ClientEventCallback = ( - args: Parameters, + args: RageFW_ClientEventArguments, ) => RageFW_ClientEventReturn export type RageFW_ClientEventReturn = diff --git a/server/src/index.ts b/server/src/index.ts index 18734e5..7ee181b 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,15 +2,17 @@ import rpc from 'rage-rpc' import Logger from './logger' -import type { +import { RageFW_ClientEvent, RageFW_ClientEventArguments, RageFW_ClientEventReturn, RageFW_ICustomServerEvent, RageFW_ServerEvent, + RageFW_ServerEventArguments, RageFW_ServerEventCallback, RageFW_ServerEventCallbackCustom, RageFW_ServerEventCallbackNative, + RageFW_ServerEventReturn, } from './types' import { nativeEvents } from './native.events' @@ -25,10 +27,7 @@ class Server { ): void { rpc.register( eventName, - async ( - args: Parameters, - info, - ) => { + async (args: RageFW_ServerEventArguments, info) => { callback(info.player as PlayerMp, args) }, ) @@ -77,6 +76,35 @@ class Server { }, ) } + + private unregisterCustom( + eventName: EventName, + ): void { + rpc.unregister(eventName) + } + + private unregisterNative( + eventName: EventName, + ): void { + mp.events.remove(eventName) + } + + public unregister( + eventName: EventName, + ): void { + if (this.isNativeEvent(eventName)) { + this.unregisterNative(eventName) + } else { + this.unregisterCustom(eventName) + } + } + + private trigger( + eventName: EventName, + args: RageFW_ServerEventArguments, + ): Promise> { + return rpc.call>(eventName, args) + } } class Player { diff --git a/server/src/types/server.ts b/server/src/types/server.ts index 9b12f02..0e93d8a 100644 --- a/server/src/types/server.ts +++ b/server/src/types/server.ts @@ -7,16 +7,12 @@ export type RageFW_ServerEvent = | keyof RageFW_ICustomServerEvent | keyof IServerEvents -export type RageFW_ServerEventCallbackCustom< - K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, -> = ( - player: PlayerMp, - args: Parameters, -) => ReturnType - -export type RageFW_ServerEventCallbackNative< - K extends keyof IServerEvents = keyof IServerEvents, -> = IServerEvents[K] +export type RageFW_ServerEventArguments = + K extends keyof RageFW_ICustomServerEvent + ? Parameters> + : K extends keyof IServerEvents + ? Parameters> + : never export type RageFW_ServerEventCallback = K extends keyof RageFW_ICustomServerEvent @@ -24,3 +20,21 @@ export type RageFW_ServerEventCallback = : K extends keyof IServerEvents ? RageFW_ServerEventCallbackNative : never + +export type RageFW_ServerEventReturn = + K extends keyof RageFW_ICustomServerEvent + ? ReturnType + : K extends keyof IServerEvents + ? ReturnType + : never + +export type RageFW_ServerEventCallbackCustom< + K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, +> = ( + player: PlayerMp, + args: RageFW_ServerEventArguments, +) => RageFW_ServerEventReturn + +export type RageFW_ServerEventCallbackNative< + K extends keyof IServerEvents = keyof IServerEvents, +> = IServerEvents[K]