trigger client from server
This commit is contained in:
parent
39dfe4c71d
commit
3b20276486
@ -1,27 +1,56 @@
|
|||||||
import rpc from 'rage-rpc'
|
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 {
|
class Server {
|
||||||
public register<EventName extends RageFW_ServerEvent>(
|
public register<EventName extends RageFW_ServerEvent>(
|
||||||
eventName: EventName,
|
eventName: EventName,
|
||||||
callback: RageFW_ServerEventCallback<EventName>,
|
callback: RageFW_ServerEventCallback<EventName>,
|
||||||
): void {
|
): void {
|
||||||
|
if (nativeEvents.includes(eventName)) {
|
||||||
|
mp.events.add(eventName, callback)
|
||||||
|
} else {
|
||||||
rpc.register(eventName, callback as rpc.ProcedureListener)
|
rpc.register(eventName, callback as rpc.ProcedureListener)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||||
[event in EventName]: RageFW_ServerEventCallback<event>
|
[event in EventName]: RageFW_ServerEventCallback<event>
|
||||||
}): void {
|
}): 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[]) => {
|
rpc.register(eventName, (args: unknown[]) => {
|
||||||
return Array.isArray(args)
|
return Array.isArray(args)
|
||||||
? (callback as (...arg: typeof args) => void)(...args)
|
? (callback as (...arg: typeof args) => void)(...args)
|
||||||
: (callback as (arg: typeof args) => void)(args)
|
: (callback as (arg: typeof args) => void)(args)
|
||||||
}),
|
})
|
||||||
)
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const rage = {
|
class Player {
|
||||||
event: new Server(),
|
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" />
|
/// <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 =
|
export type RageFW_ServerEvent =
|
||||||
| keyof RageFW_ICustomServerEvent
|
| keyof RageFW_ICustomServerEvent
|
||||||
@ -16,3 +19,15 @@ export type RageFW_ServerEventCallback<
|
|||||||
: K extends keyof IServerEvents
|
: K extends keyof IServerEvents
|
||||||
? ThisifyServerEvents[K]
|
? ThisifyServerEvents[K]
|
||||||
: never
|
: 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