resolved types

This commit is contained in:
Danya H 2024-06-11 15:14:20 +01:00
parent 5205a9e245
commit 3f48215eb1
5 changed files with 40 additions and 29 deletions

View File

@ -1,29 +1,30 @@
import rpc from 'rage-rpc'
import {
RageFW_ClientEventReturn,
RageFW_ClientCallback,
RageFW_ClientEvent,
RageFW_ClientEventArguments,
RageFW_ClientServerCallback,
RageFW_ClientEventReturn,
RageFW_ClientServerEvent,
RageFW_ClientServerEventArguments,
RageFW_ClientServerEventReturn,
} from './types'
class Client {
public register<EventName extends RageFW_ClientServerEvent>(
public register<EventName extends RageFW_ClientEvent>(
eventName: EventName,
callback: RageFW_ClientServerCallback<EventName>,
callback: RageFW_ClientCallback<EventName>,
): void {
rpc.register(eventName, data => {
return callback(...data)
return callback(data)
})
}
}
class Player {
public triggerServer<EventName extends RageFW_ClientEvent>(
public triggerServer<EventName extends RageFW_ClientServerEvent>(
eventName: EventName,
...args: RageFW_ClientEventArguments<EventName>
): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.callServer(eventName, ...args)
args: RageFW_ClientServerEventArguments<EventName>,
): Promise<RageFW_ClientServerEventReturn<EventName>> {
return rpc.callServer(eventName, args)
}
}
@ -31,3 +32,6 @@ export const fw = {
event: new Client(),
player: new Player(),
}
fw.player.triggerServer('customServerEvent', ['wer'])
fw.event.register('customClientEvent', ([arg1]) => true)

View File

@ -5,20 +5,30 @@ import type {
RageFW_ICustomServerEvent,
} from 'rage-fw-shared-types'
export type RageFW_ClientEvent = keyof RageFW_ICustomServerEvent
// CLIENT
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomServerEvent
? Parameters<RageFW_ICustomServerEvent[K]>
: never
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
args: Parameters<RageFW_ICustomClientEvent[K]>,
) => RageFW_ClientEventReturn<K>
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]>
: never
export type RageFW_ClientServerEvent = keyof RageFW_ICustomClientEvent
// SERVER
export type RageFW_ClientServerCallback<K extends RageFW_ClientServerEvent> = (
...args: Parameters<RageFW_ICustomClientEvent[K]>
) => ReturnType<RageFW_ICustomClientEvent[K]>
export type RageFW_ClientServerEvent = keyof RageFW_ICustomServerEvent
export type RageFW_ClientServerEventArguments<
K extends RageFW_ClientServerEvent,
> = K extends keyof RageFW_ICustomServerEvent
? Parameters<RageFW_ICustomServerEvent[K]>
: never
export type RageFW_ClientServerEventReturn<K extends RageFW_ClientServerEvent> =
K extends keyof RageFW_ICustomServerEvent
? ReturnType<RageFW_ICustomServerEvent[K]>
: never

View File

@ -26,10 +26,7 @@ class Server {
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)
callback(info.player as PlayerMp, args)
},
)
}
@ -83,7 +80,7 @@ class Player {
public triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
...args: RageFW_ClientEventArguments<EventName>
args: RageFW_ClientEventArguments<EventName>,
): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.callClient(player, eventName, args)
}

View File

@ -15,11 +15,9 @@ export type RageFW_ServerEventCallbackCustom<
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
> = (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
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]

View File

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