better typization for cef

This commit is contained in:
Oleksandr Honcharov 2024-06-13 05:06:02 +03:00
parent 95352908f8
commit 1766f047a4
4 changed files with 37 additions and 5 deletions

View File

@ -1,16 +1,19 @@
import rpc from 'rage-rpc'
import {
_CefEventHasArgs,
_ClientEventHasArgs,
_ServerEventHasArgs,
RageFW_CefArguments,
RageFW_CefCallback,
RageFW_CefReturn,
RageFW_ClientArguments,
RageFW_ClientReturn,
RageFW_ServerArguments,
RageFW_ServerReturn,
RageFW_ICustomCefEvent,
RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent,
RageFW_ServerArguments,
RageFW_ServerReturn,
} from './types'
class Cef {
@ -23,21 +26,27 @@ class Cef {
public trigger<EventName extends keyof RageFW_ICustomCefEvent>(
eventName: EventName,
args: RageFW_CefArguments<EventName>,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArguments<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
return rpc.call(eventName, args)
}
public triggerServer<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
args: RageFW_ServerArguments<EventName>,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerArguments<EventName>]
: []
): Promise<RageFW_ServerReturn<EventName>> {
return rpc.callServer(eventName, args)
}
public triggerClient<EventName extends keyof RageFW_ICustomClientEvent>(
eventName: EventName,
args: RageFW_ClientArguments<EventName>,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArguments<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
return rpc.callClient(eventName, args)
}

View File

@ -30,3 +30,10 @@ export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
export type RageFW_CefCallback<K extends keyof RageFW_ICustomCefEvent> = (
args: RageFW_CefArguments<K>,
) => RageFW_CefReturn<K>
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
keyof RageFW_ICustomCefEvent extends never
? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -22,3 +22,11 @@ export type RageFW_ClientArguments<K extends RageFW_ClientEvent> = Parameters<
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> = ReturnType<
RageFW_ICustomClientEvent[K]
>
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

@ -22,3 +22,11 @@ export type RageFW_ServerArguments<K extends RageFW_ServerEvent> = Parameters<
export type RageFW_ServerReturn<K extends RageFW_ServerEvent> = ReturnType<
RageFW_ICustomServerEvent[K]
>
export type _ServerEventHasArgs<
EventName extends keyof RageFW_ICustomServerEvent,
> = keyof RageFW_ICustomServerEvent extends never
? false
: Parameters<RageFW_ICustomServerEvent[EventName]>[0] extends undefined
? false
: true