resolved types

This commit is contained in:
Danya H 2024-06-11 14:45:47 +01:00
parent 4695f5dbd1
commit 5205a9e245
3 changed files with 66 additions and 28 deletions

View File

@ -5,21 +5,40 @@ import {
RageFW_ClientEventReturn, RageFW_ClientEventReturn,
RageFW_ServerEvent, RageFW_ServerEvent,
RageFW_ServerEventCallback, RageFW_ServerEventCallback,
RageFW_ServerEventCallbackCustom,
RageFW_ServerEventCallbackNative,
} from './types.js' } from './types.js'
import { nativeEvents } from './native.events' import { nativeEvents } from './native.events'
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
class Server { class Server {
private isNativeEvent(eventName: string): eventName is keyof IServerEvents { private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
return nativeEvents.includes(eventName) return nativeEvents.includes(eventName)
} }
public testRegister( private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
event: string, eventName: EventName,
callback: (player: PlayerMp, ...args: unknown[]) => void, callback: RageFW_ServerEventCallbackCustom<EventName>,
) { ): void {
rpc.register(event, async (data, info) => { rpc.register(
callback(info.player as PlayerMp, ...data) eventName,
}) async (
args: Parameters<RageFW_ICustomServerEvent[EventName]>,
info,
) => {
return Array.isArray(args)
? callback(info.player as PlayerMp, ...args)
: // @ts-ignore fixme spread tuple type no infer
callback(info.player as PlayerMp, args)
},
)
}
private registerNative<EventName extends keyof IServerEvents>(
eventName: EventName,
callback: RageFW_ServerEventCallbackNative<EventName>,
): void {
mp.events.add(eventName, callback)
} }
public register<EventName extends RageFW_ServerEvent>( public register<EventName extends RageFW_ServerEvent>(
@ -27,11 +46,15 @@ class Server {
callback: RageFW_ServerEventCallback<EventName>, callback: RageFW_ServerEventCallback<EventName>,
): void { ): void {
if (this.isNativeEvent(eventName)) { if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback) this.registerNative(
eventName,
callback as RageFW_ServerEventCallbackNative,
)
} else { } else {
rpc.register(eventName, async (data: any[], info) => { this.registerCustom(
return callback(info.player as PlayerMp, data) eventName,
}) callback as RageFW_ServerEventCallbackCustom,
)
} }
} }
@ -41,15 +64,15 @@ class Server {
Object.entries<RageFW_ServerEventCallback<EventName>>(events).map( Object.entries<RageFW_ServerEventCallback<EventName>>(events).map(
([eventName, callback]) => { ([eventName, callback]) => {
if (this.isNativeEvent(eventName)) { if (this.isNativeEvent(eventName)) {
mp.events.add(eventName, callback) this.registerNative(
eventName,
callback as RageFW_ServerEventCallbackNative,
)
} else { } else {
rpc.register(eventName, (args: unknown[]) => { this.registerCustom(
return Array.isArray(args) eventName as keyof RageFW_ICustomServerEvent,
? (callback as (...arg: typeof args) => void)( callback as RageFW_ServerEventCallbackCustom,
...args, )
)
: (callback as (arg: typeof args) => void)(args)
})
} }
}, },
) )
@ -71,8 +94,9 @@ export const fw = {
player: new Player(), player: new Player(),
} }
fw.event.register('ggfdgfd', player => {}) fw.event.register('customServerEvent', (player, args) => true)
fw.event.register('playerDeath', (player, args, killer) => {})
fw.event.registerMany({ fw.event.registerMany({
trailerAttached: player => {}, trailerAttached: (vehicle, trailer) => undefined,
}) })

View File

@ -5,20 +5,34 @@ import type {
RageFW_ICustomClientEvent, RageFW_ICustomClientEvent,
} from 'rage-fw-shared-types' } from 'rage-fw-shared-types'
// SERVER
export type RageFW_ServerEvent = export type RageFW_ServerEvent =
| keyof RageFW_ICustomServerEvent | keyof RageFW_ICustomServerEvent
| keyof IServerEvents | keyof IServerEvents
export type RageFW_ServerEventCallbackCustom<
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
> = (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
type x = Parameters<RageFW_ICustomServerEvent['customServerEvent']>[number]
export type RageFW_ServerEventCallbackNative<
K extends keyof IServerEvents = keyof IServerEvents,
> = IServerEvents[K]
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> = export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent K extends keyof RageFW_ICustomServerEvent
? ( ? RageFW_ServerEventCallbackCustom<K>
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents : K extends keyof IServerEvents
? IServerEvents[K] ? RageFW_ServerEventCallbackNative<K>
: never : never
// CLIENT
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> = export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =

View File

@ -1,7 +1,7 @@
declare module 'rage-fw-shared-types' { declare module 'rage-fw-shared-types' {
export interface RageFW_ICustomServerEvent { export interface RageFW_ICustomServerEvent {
customServerEvent(customArgs: string[]): boolean customServerEvent(customArgs: string): boolean
customServerEvent2(customArgs2: number[]): void customServerEvent2(customArgs2: number): void
} }
export interface RageFW_ICustomClientEvent {} export interface RageFW_ICustomClientEvent {}