event register types + triggerClient
This commit is contained in:
parent
d67073d739
commit
dc9e713e6b
@ -1,36 +1,93 @@
|
|||||||
/// <reference types="@ragempcommunity/types-server" />
|
/// <reference types="@ragempcommunity/types-server" />
|
||||||
import rpc from 'rage-rpc'
|
import rpc from 'rage-rpc'
|
||||||
|
|
||||||
type ServerEvent<K> = K extends keyof IServerEvents ? K : never
|
type RageFW_ServerEvent<K> = K extends keyof RageFW_ICustomServerEvent
|
||||||
type ServerEventCallback<K> = K extends keyof IServerEvents
|
? K
|
||||||
|
: K extends keyof IServerEvents
|
||||||
|
? K
|
||||||
|
: never
|
||||||
|
|
||||||
|
type RageFW_ServerEventCallback<K> = K extends keyof RageFW_ICustomServerEvent
|
||||||
|
? (
|
||||||
|
player: PlayerMp,
|
||||||
|
...args: Parameters<RageFW_ICustomServerEvent[K]>
|
||||||
|
) => ReturnType<RageFW_ICustomServerEvent[K]>
|
||||||
|
: K extends keyof IServerEvents
|
||||||
? ThisifyServerEvents[K]
|
? ThisifyServerEvents[K]
|
||||||
: never
|
: never
|
||||||
|
|
||||||
declare class Server {
|
type RageFW_ClientEvent<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
public register<K = ServerEvent<K>>(
|
? K
|
||||||
eventName: K,
|
: K extends keyof IServerEvents
|
||||||
callback: ServerEventCallback,
|
? K
|
||||||
): void
|
: never
|
||||||
|
|
||||||
public registerMultiple(events: {
|
type RageFW_ClientEventArguments<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
[name: string]: (player: PlayerMp, ...args: any[]) => any
|
? Parameters<RageFW_ICustomClientEvent[K]>
|
||||||
}): void
|
: (K extends keyof IServerEvents ? ThisifyServerEvents[K] : never)[]
|
||||||
|
|
||||||
registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
|
type RageFW_ClientEventReturn<K> = K extends keyof RageFW_ICustomClientEvent
|
||||||
Object.entries(events).forEach(([name, callback]) =>
|
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||||
rpc.register(name, (data: any[]) => {
|
: never
|
||||||
return Array.isArray(data) ? callback(...data) : callback(data)
|
|
||||||
}),
|
interface RageFW_ICustomServerEvent {
|
||||||
)
|
customEvent(arg1: number, arg2: boolean): void
|
||||||
}
|
}
|
||||||
|
|
||||||
register() {}
|
interface RageFW_ICustomClientEvent {
|
||||||
|
customClientEvent(arg1: number, arg2: boolean): boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class Server {
|
||||||
|
register<EventName extends string>(
|
||||||
|
eventName: RageFW_ServerEvent<EventName>,
|
||||||
|
callback: RageFW_ServerEventCallback<EventName>,
|
||||||
|
): void
|
||||||
|
|
||||||
|
public register(
|
||||||
|
eventName: string,
|
||||||
|
callback: (...args: unknown[]) => unknown,
|
||||||
|
) {
|
||||||
|
rpc.register(eventName, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
// public registerMultiple(events: {
|
||||||
|
// [name: string]: (player: PlayerMp, ...args: any[]) => any
|
||||||
|
// }): void
|
||||||
|
//
|
||||||
|
// registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
|
||||||
|
// Object.entries(events).forEach(([name, callback]) =>
|
||||||
|
// rpc.register(name, (data: any[]) => {
|
||||||
|
// return Array.isArray(data) ? callback(...data) : callback(data)
|
||||||
|
// }),
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// register() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Player {
|
||||||
|
triggerClient<EventName extends string>(
|
||||||
|
player: PlayerMp,
|
||||||
|
eventName: RageFW_ClientEvent<EventName>,
|
||||||
|
...args: RageFW_ClientEventArguments<EventName>
|
||||||
|
): RageFW_ClientEventReturn<EventName>
|
||||||
|
public triggerClient(
|
||||||
|
player: PlayerMp,
|
||||||
|
eventName: string,
|
||||||
|
...args: unknown[]
|
||||||
|
) {
|
||||||
|
return rpc.callClient(player, eventName, ...args)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const rage = {
|
export const rage = {
|
||||||
event: new Server(),
|
event: new Server(),
|
||||||
|
player: new Player(),
|
||||||
}
|
}
|
||||||
|
|
||||||
rage.event.register('customevent', vehicle => {})
|
rage.event.register('customEvent', (player, arg1, arg2) => {
|
||||||
|
rage.player.triggerClient(player, 'customClientEvent', arg1, arg2)
|
||||||
|
})
|
||||||
|
|
||||||
function test() {}
|
function test() {}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"target": "ESNext",
|
"target": "ESNext",
|
||||||
"lib": ["ESNext", "ES5"],
|
"lib": ["ESNext", "ES5"],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"forceConsistentCasingInFileNames": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
Loading…
Reference in New Issue
Block a user