unregister + trigger
This commit is contained in:
parent
22945357fd
commit
1880f28eb7
@ -2,13 +2,16 @@ import rpc from 'rage-rpc'
|
||||
|
||||
import Logger from './logger'
|
||||
|
||||
import type {
|
||||
import {
|
||||
RageFW_ClientEventCallback,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ClientServerEvent,
|
||||
RageFW_ClientServerEventArguments,
|
||||
RageFW_ClientServerEventReturn,
|
||||
RageFW_ClientEventArguments,
|
||||
RageFW_ClientEventReturn,
|
||||
} from './types'
|
||||
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
|
||||
|
||||
class Client {
|
||||
public register<EventName extends RageFW_ClientEvent>(
|
||||
@ -19,6 +22,19 @@ class Client {
|
||||
return callback(data)
|
||||
})
|
||||
}
|
||||
|
||||
public unregister<EventName extends RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
rpc.unregister(eventName)
|
||||
}
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
|
||||
eventName: EventName,
|
||||
args: RageFW_ClientEventArguments<EventName>,
|
||||
): Promise<RageFW_ClientEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ClientEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
class Player {
|
||||
|
@ -4,8 +4,11 @@ import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
|
||||
|
||||
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
|
||||
|
||||
export type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
|
||||
Parameters<RageFW_ICustomClientEvent[K]>
|
||||
|
||||
export type RageFW_ClientEventCallback<K extends RageFW_ClientEvent> = (
|
||||
args: Parameters<RageFW_ICustomClientEvent[K]>,
|
||||
args: RageFW_ClientEventArguments<K>,
|
||||
) => RageFW_ClientEventReturn<K>
|
||||
|
||||
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
|
||||
|
@ -2,15 +2,17 @@ import rpc from 'rage-rpc'
|
||||
|
||||
import Logger from './logger'
|
||||
|
||||
import type {
|
||||
import {
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ClientEventArguments,
|
||||
RageFW_ClientEventReturn,
|
||||
RageFW_ICustomServerEvent,
|
||||
RageFW_ServerEvent,
|
||||
RageFW_ServerEventArguments,
|
||||
RageFW_ServerEventCallback,
|
||||
RageFW_ServerEventCallbackCustom,
|
||||
RageFW_ServerEventCallbackNative,
|
||||
RageFW_ServerEventReturn,
|
||||
} from './types'
|
||||
import { nativeEvents } from './native.events'
|
||||
|
||||
@ -25,10 +27,7 @@ class Server {
|
||||
): void {
|
||||
rpc.register(
|
||||
eventName,
|
||||
async (
|
||||
args: Parameters<RageFW_ICustomServerEvent[EventName]>,
|
||||
info,
|
||||
) => {
|
||||
async (args: RageFW_ServerEventArguments<EventName>, info) => {
|
||||
callback(info.player as PlayerMp, args)
|
||||
},
|
||||
)
|
||||
@ -77,6 +76,35 @@ class Server {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private unregisterCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
rpc.unregister(eventName)
|
||||
}
|
||||
|
||||
private unregisterNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
mp.events.remove(eventName)
|
||||
}
|
||||
|
||||
public unregister<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.unregisterNative(eventName)
|
||||
} else {
|
||||
this.unregisterCustom(eventName)
|
||||
}
|
||||
}
|
||||
|
||||
private trigger<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
args: RageFW_ServerEventArguments<EventName>,
|
||||
): Promise<RageFW_ServerEventReturn<EventName>> {
|
||||
return rpc.call<RageFW_ServerEventReturn<EventName>>(eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
class Player {
|
||||
|
@ -7,16 +7,12 @@ export type RageFW_ServerEvent =
|
||||
| keyof RageFW_ICustomServerEvent
|
||||
| keyof IServerEvents
|
||||
|
||||
export type RageFW_ServerEventCallbackCustom<
|
||||
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
|
||||
> = (
|
||||
player: PlayerMp,
|
||||
args: Parameters<RageFW_ICustomServerEvent[K]>,
|
||||
) => ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
|
||||
export type RageFW_ServerEventCallbackNative<
|
||||
K extends keyof IServerEvents = keyof IServerEvents,
|
||||
> = IServerEvents[K]
|
||||
export type RageFW_ServerEventArguments<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? Parameters<RageFW_ServerEventCallbackCustom<K>>
|
||||
: K extends keyof IServerEvents
|
||||
? Parameters<RageFW_ServerEventCallbackNative<K>>
|
||||
: never
|
||||
|
||||
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
@ -24,3 +20,21 @@ export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
|
||||
: K extends keyof IServerEvents
|
||||
? RageFW_ServerEventCallbackNative<K>
|
||||
: never
|
||||
|
||||
export type RageFW_ServerEventReturn<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
: K extends keyof IServerEvents
|
||||
? ReturnType<IServerEvents[K]>
|
||||
: never
|
||||
|
||||
export type RageFW_ServerEventCallbackCustom<
|
||||
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
|
||||
> = (
|
||||
player: PlayerMp,
|
||||
args: RageFW_ServerEventArguments<K>,
|
||||
) => RageFW_ServerEventReturn<K>
|
||||
|
||||
export type RageFW_ServerEventCallbackNative<
|
||||
K extends keyof IServerEvents = keyof IServerEvents,
|
||||
> = IServerEvents[K]
|
||||
|
Loading…
Reference in New Issue
Block a user