Compare commits

..

2 Commits

Author SHA1 Message Date
fa3b1b1b0b Merge remote-tracking branch 'origin/master' 2024-06-11 19:52:07 +01:00
1880f28eb7 unregister + trigger 2024-06-11 19:52:00 +01:00
4 changed files with 78 additions and 17 deletions

View File

@ -2,13 +2,16 @@ import rpc from 'rage-rpc'
import Logger from './logger' import Logger from './logger'
import type { import {
RageFW_ClientEventCallback, RageFW_ClientEventCallback,
RageFW_ClientEvent, RageFW_ClientEvent,
RageFW_ClientServerEvent, RageFW_ClientServerEvent,
RageFW_ClientServerEventArguments, RageFW_ClientServerEventArguments,
RageFW_ClientServerEventReturn, RageFW_ClientServerEventReturn,
RageFW_ClientEventArguments,
RageFW_ClientEventReturn,
} from './types' } from './types'
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
class Client { class Client {
public register<EventName extends RageFW_ClientEvent>( public register<EventName extends RageFW_ClientEvent>(
@ -19,6 +22,19 @@ class Client {
return callback(data) 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 { class Player {

View File

@ -4,8 +4,11 @@ import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent 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> = ( export type RageFW_ClientEventCallback<K extends RageFW_ClientEvent> = (
args: Parameters<RageFW_ICustomClientEvent[K]>, args: RageFW_ClientEventArguments<K>,
) => RageFW_ClientEventReturn<K> ) => RageFW_ClientEventReturn<K>
export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> = export type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =

View File

@ -2,15 +2,17 @@ import rpc from 'rage-rpc'
import Logger from './logger' import Logger from './logger'
import type { import {
RageFW_ClientEvent, RageFW_ClientEvent,
RageFW_ClientEventArguments, RageFW_ClientEventArguments,
RageFW_ClientEventReturn, RageFW_ClientEventReturn,
RageFW_ICustomServerEvent, RageFW_ICustomServerEvent,
RageFW_ServerEvent, RageFW_ServerEvent,
RageFW_ServerEventArguments,
RageFW_ServerEventCallback, RageFW_ServerEventCallback,
RageFW_ServerEventCallbackCustom, RageFW_ServerEventCallbackCustom,
RageFW_ServerEventCallbackNative, RageFW_ServerEventCallbackNative,
RageFW_ServerEventReturn,
} from './types' } from './types'
import { nativeEvents } from './native.events' import { nativeEvents } from './native.events'
@ -25,10 +27,7 @@ class Server {
): void { ): void {
rpc.register( rpc.register(
eventName, eventName,
async ( async (args: RageFW_ServerEventArguments<EventName>, info) => {
args: Parameters<RageFW_ICustomServerEvent[EventName]>,
info,
) => {
callback(info.player as PlayerMp, args) 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 { class Player {

View File

@ -7,16 +7,12 @@ export type RageFW_ServerEvent =
| keyof RageFW_ICustomServerEvent | keyof RageFW_ICustomServerEvent
| keyof IServerEvents | keyof IServerEvents
export type RageFW_ServerEventCallbackCustom< export type RageFW_ServerEventArguments<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, K extends keyof RageFW_ICustomServerEvent
> = ( ? Parameters<RageFW_ServerEventCallbackCustom<K>>
player: PlayerMp, : K extends keyof IServerEvents
args: Parameters<RageFW_ICustomServerEvent[K]>, ? Parameters<RageFW_ServerEventCallbackNative<K>>
) => ReturnType<RageFW_ICustomServerEvent[K]> : never
export type RageFW_ServerEventCallbackNative<
K extends keyof IServerEvents = keyof IServerEvents,
> = IServerEvents[K]
export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> = export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent K extends keyof RageFW_ICustomServerEvent
@ -24,3 +20,21 @@ export type RageFW_ServerEventCallback<K extends RageFW_ServerEvent> =
: K extends keyof IServerEvents : K extends keyof IServerEvents
? RageFW_ServerEventCallbackNative<K> ? RageFW_ServerEventCallbackNative<K>
: never : 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]