2024-10-28 16:53:42 +00:00
|
|
|
import { rpc } from './rpc'
|
2024-10-29 17:03:07 +00:00
|
|
|
import { Middleware } from './middleware'
|
2024-10-28 15:20:43 +00:00
|
|
|
import type * as T from '../types'
|
2024-06-18 20:03:33 +00:00
|
|
|
|
2024-10-30 15:27:22 +00:00
|
|
|
/** Client-side interactions */
|
2024-06-18 20:03:33 +00:00
|
|
|
export class Client {
|
2024-10-30 15:27:22 +00:00
|
|
|
/**
|
|
|
|
* Registers a client event with an associated callback
|
|
|
|
*
|
|
|
|
* @param eventName - The name of the event to register
|
|
|
|
* @param callback - The callback function to be executed when the event is triggered
|
|
|
|
* @param [options] - Optional settings for callback execution
|
|
|
|
* @param [options.middlewares] - Middleware functions to be checked before the callback executes
|
|
|
|
* @returns {Client} The current client instance, enabling method chaining
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Registering an event
|
|
|
|
* fw.event.register("playerDeath", (player, reason, killer) => {
|
|
|
|
* fw.system.log.info(player, reason, killer)
|
|
|
|
* })
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Registering an event with middlewares
|
|
|
|
* fw.event.register("playerDeath", (player, reason, killer) => {
|
|
|
|
* fw.system.log.info(player, reason, killer)
|
|
|
|
* }, {
|
|
|
|
* middlewares: [ignoreSuicide] // <- your middlewares here
|
|
|
|
* })
|
|
|
|
*
|
|
|
|
* // or
|
|
|
|
*
|
|
|
|
* fw.event.register("playerDeath", (player, reason, killer) => {
|
|
|
|
* fw.system.log.info(player, reason, killer)
|
|
|
|
* }, {
|
|
|
|
* middlewares: {
|
|
|
|
* executables: [ignoreSuicide], // <- your middlewares here
|
|
|
|
* onError: (msg) => fw.system.log.info(`${player.socialClub} has commited suicide`)
|
|
|
|
* }
|
|
|
|
* })
|
|
|
|
*
|
|
|
|
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
|
|
|
|
*/
|
2024-10-28 15:20:43 +00:00
|
|
|
public register<EventName extends T.RageFW_ClientEvent>(
|
2024-06-18 20:03:33 +00:00
|
|
|
eventName: EventName,
|
2024-10-28 15:20:43 +00:00
|
|
|
callback: T.RageFW_ClientCallback<EventName>,
|
2024-10-29 17:03:07 +00:00
|
|
|
options?: {
|
|
|
|
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
|
|
|
},
|
2024-10-28 16:53:42 +00:00
|
|
|
): Client {
|
|
|
|
rpc.register<
|
|
|
|
Parameters<typeof callback>,
|
2024-10-29 17:03:07 +00:00
|
|
|
ReturnType<typeof callback> | Promise<unknown>,
|
2024-10-28 16:53:42 +00:00
|
|
|
EventName
|
2024-10-29 17:03:07 +00:00
|
|
|
>(eventName, async (...data) => {
|
|
|
|
if (!options?.middlewares) return await callback(...data)
|
|
|
|
|
|
|
|
await Middleware.process(options.middlewares, callback, data)
|
|
|
|
})
|
2024-10-28 16:53:42 +00:00
|
|
|
|
|
|
|
return this
|
2024-06-18 20:03:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 15:27:22 +00:00
|
|
|
/**
|
|
|
|
* Unregisters a client event, removing the associated callback
|
|
|
|
*
|
|
|
|
* @param eventName - The name of the event to unregister
|
|
|
|
* @returns {Client} The current client instance, enabling method chaining
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Unregistering an event
|
|
|
|
* fw.event.unregister("playerDeath")
|
|
|
|
*
|
|
|
|
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
|
|
|
|
*/
|
2024-10-28 15:20:43 +00:00
|
|
|
public unregister<EventName extends T.RageFW_ClientEvent>(
|
2024-06-18 20:03:33 +00:00
|
|
|
eventName: EventName,
|
2024-10-28 16:53:42 +00:00
|
|
|
): Client {
|
2024-10-28 17:30:28 +00:00
|
|
|
rpc.unregister<EventName>(eventName)
|
2024-10-28 16:53:42 +00:00
|
|
|
|
|
|
|
return this
|
2024-06-18 20:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-28 16:53:42 +00:00
|
|
|
|
|
|
|
// new Client()
|
|
|
|
// .register('customClientEvent', async (a, b) => true)
|
|
|
|
// .unregister('customClientEvent')
|