better typings in trigger functions
This commit is contained in:
parent
a9486db3f5
commit
c52cd8b4c8
@ -10,6 +10,8 @@ import {
|
||||
RageFW_ClientServerEventReturn,
|
||||
RageFW_ClientEventArguments,
|
||||
RageFW_ClientEventReturn,
|
||||
_ClientEventHasArgs,
|
||||
_ServerEventHasArgs,
|
||||
RageFW_CefEvent,
|
||||
RageFW_CefArgs,
|
||||
RageFW_CefReturn,
|
||||
@ -39,14 +41,18 @@ class Player {
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
|
||||
eventName: EventName,
|
||||
args: RageFW_ClientEventArguments<EventName>,
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ClientEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ClientEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ClientEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
|
||||
public triggerServer<EventName extends RageFW_ClientServerEvent>(
|
||||
eventName: EventName,
|
||||
args: RageFW_ClientServerEventArguments<EventName>,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ClientServerEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ClientServerEventReturn<EventName>> {
|
||||
return rpc.callServer(eventName, args)
|
||||
}
|
||||
|
@ -31,3 +31,11 @@ export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
|
||||
K extends keyof RageFW_ICustomClientEvent
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
|
||||
export type _ClientEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomClientEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomClientEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
|
@ -1,6 +1,9 @@
|
||||
/// <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
|
||||
@ -26,3 +29,11 @@ export type RageFW_ClientServerEventReturn<K extends RageFW_ClientServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
: never
|
||||
|
||||
export type _ServerEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomServerEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomServerEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
|
@ -3,12 +3,11 @@ import rpc from 'rage-rpc'
|
||||
import Logger from './logger'
|
||||
|
||||
import {
|
||||
RageFW_CefArgs,
|
||||
RageFW_CefEvent,
|
||||
RageFW_CefReturn,
|
||||
_ClientEventHasArgs,
|
||||
_ServerEventHasArgs,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ClientEventArguments,
|
||||
RageFW_ClientEventReturn,
|
||||
RageFW_ServerClientEventArguments,
|
||||
RageFW_ServerClientEventReturn,
|
||||
RageFW_ICustomServerEvent,
|
||||
RageFW_ServerEvent,
|
||||
RageFW_ServerEventArguments,
|
||||
@ -104,7 +103,9 @@ class Server {
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
args: RageFW_ServerEventArguments<EventName>,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
@ -114,18 +115,12 @@ class Player {
|
||||
public triggerClient<EventName extends RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
args: RageFW_ClientEventArguments<EventName>,
|
||||
): Promise<RageFW_ClientEventReturn<EventName>> {
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerClientEventArguments<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerClientEventReturn<EventName>> {
|
||||
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 = {
|
||||
@ -135,7 +130,3 @@ export const fw = {
|
||||
log: new Logger(),
|
||||
},
|
||||
}
|
||||
|
||||
fw.system.log.info(
|
||||
'Working on Rage Framework. RageFW © Powered by Entity Seven Group',
|
||||
)
|
||||
|
@ -12,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
||||
* Array of arguments of an event you pass as a generic
|
||||
* 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
|
||||
? Parameters<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
@ -21,7 +21,15 @@ export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
|
||||
* Return type of event you pass as a generic
|
||||
* 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
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
|
||||
export type _ClientEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomClientEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomClientEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
|
@ -1,6 +1,9 @@
|
||||
/// <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'
|
||||
|
||||
/**
|
||||
@ -62,3 +65,11 @@ export type RageFW_ServerEventCallbackCustom<
|
||||
export type RageFW_ServerEventCallbackNative<
|
||||
K extends keyof IServerEvents = keyof IServerEvents,
|
||||
> = 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
|
||||
|
12
shared-types/types/types/index.d.ts
vendored
12
shared-types/types/types/index.d.ts
vendored
@ -1,7 +1,13 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"lib": ["ESNext","ES2019"],
|
||||
"lib": ["ESNext","ES2019", "ES6"],
|
||||
"moduleResolution": "node",
|
||||
"module": "ESNext",
|
||||
"esModuleInterop": true,
|
||||
|
Loading…
Reference in New Issue
Block a user