fixed type mismatches and callback handling in server register event

This commit is contained in:
Danya H 2024-06-13 15:02:41 +01:00
parent 045614379d
commit 2df12062d0
4 changed files with 24 additions and 11 deletions

View File

@ -4,16 +4,22 @@ import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
/** /**
* Union of all available client event names * Union of all available client event names
* These only include custom events * These include custom and system events
*/ */
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent export type RageFW_ClientEvent =
| keyof RageFW_ICustomClientEvent
| keyof IClientEvents
/** /**
* Array of arguments for an event, name of which you pass as a generic * Array of arguments for an event, name of which you pass as a generic
* These only include custom events * These include custom and system events
*/ */
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> = export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
Parameters<RageFW_ICustomClientEvent[K]> K extends keyof RageFW_ICustomClientEvent
? Parameters<RageFW_ICustomClientEvent[K]>
: K extends keyof IClientEvents
? Parameters<IClientEvents[K]>
: never
/** /**
* Callback (function) for an event, name of which you pass as a generic * Callback (function) for an event, name of which you pass as a generic

View File

@ -34,7 +34,7 @@ class Server {
rpc.register( rpc.register(
eventName, eventName,
async (args: RageFW_ServerEventArguments<EventName>, info) => { async (args: RageFW_ServerEventArguments<EventName>, info) => {
callback(info.player as PlayerMp, args) callback([info.player as PlayerMp, ...args])
}, },
) )
} }
@ -43,7 +43,11 @@ class Server {
eventName: EventName, eventName: EventName,
callback: RageFW_ServerEventCallbackNative<EventName>, callback: RageFW_ServerEventCallbackNative<EventName>,
): void { ): void {
mp.events.add(eventName, callback) mp.events.add(
eventName,
(...args: Parameters<IServerEvents[EventName]>) =>
callback([...args]),
)
} }
public register<EventName extends RageFW_ServerEvent>( public register<EventName extends RageFW_ServerEvent>(

View File

@ -54,8 +54,7 @@ export type RageFW_ServerEventReturn<K extends RageFW_ServerEvent> =
export type RageFW_ServerEventCallbackCustom< export type RageFW_ServerEventCallbackCustom<
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
> = ( > = (
player: PlayerMp, payload: [player: PlayerMp, ...args: RageFW_ServerEventArguments<K>],
args: RageFW_ServerEventArguments<K>,
) => RageFW_ServerEventReturn<K> ) => RageFW_ServerEventReturn<K>
/** /**
@ -64,7 +63,7 @@ export type RageFW_ServerEventCallbackCustom<
*/ */
export type RageFW_ServerEventCallbackNative< export type RageFW_ServerEventCallbackNative<
K extends keyof IServerEvents = keyof IServerEvents, K extends keyof IServerEvents = keyof IServerEvents,
> = IServerEvents[K] > = (payload: Parameters<IServerEvents[K]>) => ReturnType<IServerEvents[K]>
export type _ServerEventHasArgs< export type _ServerEventHasArgs<
EventName extends keyof RageFW_ICustomServerEvent, EventName extends keyof RageFW_ICustomServerEvent,

View File

@ -1,7 +1,11 @@
declare module 'rage-fw-shared-types' { declare module 'rage-fw-shared-types' {
export interface RageFW_ICustomServerEvent {} export interface RageFW_ICustomServerEvent {
test(test: string): void
}
export interface RageFW_ICustomClientEvent {} export interface RageFW_ICustomClientEvent {
test(test: string): void
}
export interface RageFW_ICustomCefEvent { export interface RageFW_ICustomCefEvent {
test(test: string): void test(test: string): void