resolved types
This commit is contained in:
parent
5205a9e245
commit
3f48215eb1
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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]
|
||||
|
6
shared-types/types/types/index.d.ts
vendored
6
shared-types/types/types/index.d.ts
vendored
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user