trigger client from server
This commit is contained in:
parent
39dfe4c71d
commit
3b20276486
@ -1,27 +1,56 @@
|
||||
import rpc from 'rage-rpc'
|
||||
import type { RageFW_ServerEvent, RageFW_ServerEventCallback } from './types.js'
|
||||
import {
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ClientEventArguments,
|
||||
RageFW_ClientEventReturn,
|
||||
RageFW_ServerEvent,
|
||||
RageFW_ServerEventCallback,
|
||||
} from './types.js'
|
||||
import { nativeEvents } from './native.events'
|
||||
|
||||
class Server {
|
||||
public register<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerEventCallback<EventName>,
|
||||
): void {
|
||||
if (nativeEvents.includes(eventName)) {
|
||||
mp.events.add(eventName, callback)
|
||||
} else {
|
||||
rpc.register(eventName, callback as rpc.ProcedureListener)
|
||||
}
|
||||
}
|
||||
|
||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||
[event in EventName]: RageFW_ServerEventCallback<event>
|
||||
}): void {
|
||||
Object.entries(events).map(([eventName, callback]) =>
|
||||
Object.entries(events).map(([eventName, callback]) => {
|
||||
if (nativeEvents.includes(eventName)) {
|
||||
mp.events.add(
|
||||
eventName as keyof IServerEvents,
|
||||
callback as (...arg: unknown[]) => void,
|
||||
)
|
||||
} else {
|
||||
rpc.register(eventName, (args: unknown[]) => {
|
||||
return Array.isArray(args)
|
||||
? (callback as (...arg: typeof args) => void)(...args)
|
||||
: (callback as (arg: typeof args) => void)(args)
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const rage = {
|
||||
event: new Server(),
|
||||
class Player {
|
||||
public triggerClient<EventName extends RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: RageFW_ClientEventArguments<EventName>
|
||||
): Promise<RageFW_ClientEventReturn<EventName>> {
|
||||
return rpc.callClient(player, eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
export const fw = {
|
||||
event: new Server(),
|
||||
player: new Player(),
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
/// <reference types="@ragempcommunity/types-server" />
|
||||
|
||||
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
|
||||
import type {
|
||||
RageFW_ICustomServerEvent,
|
||||
RageFW_ICustomClientEvent,
|
||||
} from 'rage-fw-shared-types'
|
||||
|
||||
export type RageFW_ServerEvent =
|
||||
| keyof RageFW_ICustomServerEvent
|
||||
@ -16,3 +19,15 @@ export type RageFW_ServerEventCallback<
|
||||
: K extends keyof IServerEvents
|
||||
? ThisifyServerEvents[K]
|
||||
: never
|
||||
|
||||
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
||||
|
||||
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
|
||||
K extends RageFW_ClientEvent
|
||||
? Parameters<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
|
||||
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
|
||||
K extends RageFW_ClientEvent
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
|
Loading…
Reference in New Issue
Block a user