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

View File

@ -5,20 +5,30 @@ import type {
RageFW_ICustomServerEvent, RageFW_ICustomServerEvent,
} from 'rage-fw-shared-types' } from 'rage-fw-shared-types'
export type RageFW_ClientEvent = keyof RageFW_ICustomServerEvent // CLIENT
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> = export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
K extends keyof RageFW_ICustomServerEvent
? Parameters<RageFW_ICustomServerEvent[K]> export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
: never args: Parameters<RageFW_ICustomClientEvent[K]>,
) => RageFW_ClientEventReturn<K>
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> = export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent K extends keyof RageFW_ICustomClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]> ? ReturnType<RageFW_ICustomClientEvent[K]>
: never : never
export type RageFW_ClientServerEvent = keyof RageFW_ICustomClientEvent // SERVER
export type RageFW_ClientServerCallback<K extends RageFW_ClientServerEvent> = ( export type RageFW_ClientServerEvent = keyof RageFW_ICustomServerEvent
...args: Parameters<RageFW_ICustomClientEvent[K]>
) => ReturnType<RageFW_ICustomClientEvent[K]> 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,9 +26,6 @@ class Server {
args: Parameters<RageFW_ICustomServerEvent[EventName]>, args: Parameters<RageFW_ICustomServerEvent[EventName]>,
info, 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>( public triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp, player: PlayerMp,
eventName: EventName, eventName: EventName,
...args: RageFW_ClientEventArguments<EventName> args: RageFW_ClientEventArguments<EventName>,
): Promise<RageFW_ClientEventReturn<EventName>> { ): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.callClient(player, eventName, args) return rpc.callClient(player, eventName, args)
} }

View File

@ -15,11 +15,9 @@ export type RageFW_ServerEventCallbackCustom<
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
> = ( > = (
player: PlayerMp, player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]> args: Parameters<RageFW_ICustomServerEvent[K]>,
) => ReturnType<RageFW_ICustomServerEvent[K]> ) => ReturnType<RageFW_ICustomServerEvent[K]>
type x = Parameters<RageFW_ICustomServerEvent['customServerEvent']>[number]
export type RageFW_ServerEventCallbackNative< export type RageFW_ServerEventCallbackNative<
K extends keyof IServerEvents = keyof IServerEvents, K extends keyof IServerEvents = keyof IServerEvents,
> = IServerEvents[K] > = IServerEvents[K]

View File

@ -1,8 +1,10 @@
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 {
customClientEvent(clientArgs: string): boolean
}
} }