better typings in trigger functions

This commit is contained in:
Danya H 2024-06-13 02:18:33 +01:00
parent a9486db3f5
commit c52cd8b4c8
8 changed files with 71 additions and 30 deletions

View File

@ -10,6 +10,8 @@ import {
RageFW_ClientServerEventReturn, RageFW_ClientServerEventReturn,
RageFW_ClientEventArguments, RageFW_ClientEventArguments,
RageFW_ClientEventReturn, RageFW_ClientEventReturn,
_ClientEventHasArgs,
_ServerEventHasArgs,
RageFW_CefEvent, RageFW_CefEvent,
RageFW_CefArgs, RageFW_CefArgs,
RageFW_CefReturn, RageFW_CefReturn,
@ -39,14 +41,18 @@ class Player {
public trigger<EventName extends keyof RageFW_ICustomClientEvent>( public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
eventName: EventName, eventName: EventName,
args: RageFW_ClientEventArguments<EventName>, ...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientEventArguments<EventName>]
: []
): Promise<RageFW_ClientEventReturn<EventName>> { ): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.call<RageFW_ClientEventReturn<EventName>>(eventName, args) return rpc.call<RageFW_ClientEventReturn<EventName>>(eventName, args)
} }
public triggerServer<EventName extends RageFW_ClientServerEvent>( public triggerServer<EventName extends RageFW_ClientServerEvent>(
eventName: EventName, eventName: EventName,
args: RageFW_ClientServerEventArguments<EventName>, ...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ClientServerEventArguments<EventName>]
: []
): Promise<RageFW_ClientServerEventReturn<EventName>> { ): Promise<RageFW_ClientServerEventReturn<EventName>> {
return rpc.callServer(eventName, args) return rpc.callServer(eventName, args)
} }

View File

@ -31,3 +31,11 @@ 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 _ClientEventHasArgs<
EventName extends keyof RageFW_ICustomClientEvent,
> = keyof RageFW_ICustomClientEvent extends never
? false
: Parameters<RageFW_ICustomClientEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,6 +1,9 @@
/// <reference types="@ragempcommunity/types-client" /> /// <reference types="@ragempcommunity/types-client" />
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' import type {
RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent,
} from 'rage-fw-shared-types'
/** /**
* Union of all available server event names callable from client * Union of all available server event names callable from client
@ -26,3 +29,11 @@ export type RageFW_ClientServerEventReturn<K extends RageFW_ClientServerEvent> =
K extends keyof RageFW_ICustomServerEvent K extends keyof RageFW_ICustomServerEvent
? ReturnType<RageFW_ICustomServerEvent[K]> ? ReturnType<RageFW_ICustomServerEvent[K]>
: never : never
export type _ServerEventHasArgs<
EventName extends keyof RageFW_ICustomServerEvent,
> = keyof RageFW_ICustomClientEvent extends never
? false
: Parameters<RageFW_ICustomServerEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -3,12 +3,11 @@ import rpc from 'rage-rpc'
import Logger from './logger' import Logger from './logger'
import { import {
RageFW_CefArgs, _ClientEventHasArgs,
RageFW_CefEvent, _ServerEventHasArgs,
RageFW_CefReturn,
RageFW_ClientEvent, RageFW_ClientEvent,
RageFW_ClientEventArguments, RageFW_ServerClientEventArguments,
RageFW_ClientEventReturn, RageFW_ServerClientEventReturn,
RageFW_ICustomServerEvent, RageFW_ICustomServerEvent,
RageFW_ServerEvent, RageFW_ServerEvent,
RageFW_ServerEventArguments, RageFW_ServerEventArguments,
@ -104,7 +103,9 @@ class Server {
public trigger<EventName extends keyof RageFW_ICustomServerEvent>( public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName, eventName: EventName,
args: RageFW_ServerEventArguments<EventName>, ...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerEventArguments<EventName>]
: []
): Promise<RageFW_ServerEventReturn<EventName>> { ): Promise<RageFW_ServerEventReturn<EventName>> {
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args) return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
} }
@ -114,18 +115,12 @@ 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: _ClientEventHasArgs<EventName> extends true
): Promise<RageFW_ClientEventReturn<EventName>> { ? [RageFW_ServerClientEventArguments<EventName>]
: []
): Promise<RageFW_ServerClientEventReturn<EventName>> {
return rpc.callClient(player, eventName, args) return rpc.callClient(player, eventName, args)
} }
public triggerBrowser<EventName extends RageFW_CefEvent>(
player: PlayerMp,
eventName: EventName,
args: RageFW_CefArgs<EventName>,
): Promise<RageFW_CefReturn<EventName>> {
return rpc.callBrowsers(player, eventName, args)
}
} }
export const fw = { export const fw = {
@ -135,7 +130,3 @@ export const fw = {
log: new Logger(), log: new Logger(),
}, },
} }
fw.system.log.info(
'Working on Rage Framework. RageFW © Powered by Entity Seven Group',
)

View File

@ -12,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
* Array of arguments of an event you pass as a generic * Array of arguments of an event you pass as a generic
* These only include custom events * These only include custom events
*/ */
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> = export type RageFW_ServerClientEventArguments<K extends RageFW_ClientEvent> =
K extends RageFW_ClientEvent K extends RageFW_ClientEvent
? Parameters<RageFW_ICustomClientEvent[K]> ? Parameters<RageFW_ICustomClientEvent[K]>
: never : never
@ -21,7 +21,15 @@ export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
* Return type of event you pass as a generic * Return type of event you pass as a generic
* These only include custom events * These only include custom events
*/ */
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> = export type RageFW_ServerClientEventReturn<K extends RageFW_ClientEvent> =
K extends RageFW_ClientEvent K extends RageFW_ClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]> ? ReturnType<RageFW_ICustomClientEvent[K]>
: never : never
export type _ClientEventHasArgs<
EventName extends keyof RageFW_ICustomClientEvent,
> = keyof RageFW_ICustomClientEvent extends never
? false
: Parameters<RageFW_ICustomClientEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,6 +1,9 @@
/// <reference types="@ragempcommunity/types-server" /> /// <reference types="@ragempcommunity/types-server" />
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' import type {
RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent,
} from 'rage-fw-shared-types'
export type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' export type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
/** /**
@ -62,3 +65,11 @@ 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] > = IServerEvents[K]
export type _ServerEventHasArgs<
EventName extends keyof RageFW_ICustomServerEvent,
> = keyof RageFW_ICustomClientEvent extends never
? false
: Parameters<RageFW_ICustomServerEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,7 +1,13 @@
declare module 'rage-fw-shared-types' { declare module 'rage-fw-shared-types' {
export interface RageFW_ICustomServerEvent {} export interface RageFW_ICustomServerEvent {
customServerEvent(test: string, test2: number): void
}
export interface RageFW_ICustomClientEvent {} export interface RageFW_ICustomClientEvent {
customClientEvent(test: string, test2: number): void
}
export interface RageFW_ICustomCefEvent {} export interface RageFW_ICustomCefEvent {
customCefEvent(test: string, test2: number): void
}
} }

View File

@ -1,7 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ESNext", "target": "ESNext",
"lib": ["ESNext","ES2019"], "lib": ["ESNext","ES2019", "ES6"],
"moduleResolution": "node", "moduleResolution": "node",
"module": "ESNext", "module": "ESNext",
"esModuleInterop": true, "esModuleInterop": true,