From cbbf4f38e66fd123f9ed0efc85679a707beb8927 Mon Sep 17 00:00:00 2001 From: Danya H Date: Mon, 28 Oct 2024 17:30:28 +0000 Subject: [PATCH] upd core - typed server inner rpc - client minor fixes - sharing browser with rpc on client --- browser/src/core/browser.ts | 2 +- client/src/core/client.ts | 2 +- client/src/core/player.ts | 24 +++++--- client/src/types/client.ts | 4 +- server/src/core/index.ts | 5 +- server/src/core/player.ts | 28 ++++----- server/src/core/rpc.ts | 5 ++ server/src/core/server.ts | 120 ++++++++---------------------------- server/src/index.ts | 3 +- server/src/types/browser.ts | 21 ++++++- server/src/types/client.ts | 7 ++- server/src/types/server.ts | 37 +++-------- 12 files changed, 98 insertions(+), 160 deletions(-) create mode 100644 server/src/core/rpc.ts diff --git a/browser/src/core/browser.ts b/browser/src/core/browser.ts index 95e2b59..de7efdc 100644 --- a/browser/src/core/browser.ts +++ b/browser/src/core/browser.ts @@ -40,7 +40,7 @@ export class Browser extends Helper { public unregister( eventName: EventName, ): Browser { - rpc.unregister(eventName) + rpc.unregister(eventName) return this } diff --git a/client/src/core/client.ts b/client/src/core/client.ts index 7b75cf8..27e0f64 100644 --- a/client/src/core/client.ts +++ b/client/src/core/client.ts @@ -18,7 +18,7 @@ export class Client { public unregister( eventName: EventName, ): Client { - rpc.unregister(eventName) + rpc.unregister(eventName) return this } diff --git a/client/src/core/player.ts b/client/src/core/player.ts index 0e7a511..3572715 100644 --- a/client/src/core/player.ts +++ b/client/src/core/player.ts @@ -2,47 +2,53 @@ import { rpc } from './rpc' import type * as T from '../types' export class Player { - public browser: BrowserMp | undefined + private _browser: BrowserMp | undefined = undefined - public trigger( + set browser(browser: BrowserMp) { + this._browser = browser + rpc.browser = browser + } + + public async trigger( eventName: EventName, ...args: T._ClientEventHasArgs extends true ? [T.RageFW_ClientArgs] : [] ): Promise> { - return rpc.call< + return await rpc.call< typeof args, EventName, T.RageFW_ClientReturn >(eventName, args) } - public triggerServer( + public async triggerServer( eventName: EventName, ...args: T._ServerEventHasArgs extends true ? [T.RageFW_ServerArgs] : [] ): Promise> { - return rpc.callServer< + return await rpc.callServer< typeof args, EventName, T.RageFW_ClientServerReturn >(eventName, args) } - public triggerBrowser( + public async triggerBrowser( eventName: EventName, ...args: T._CefEventHasArgs extends true ? [T.RageFW_CefArgs] : [] ): Promise> { - if (!this.browser) + if (!this._browser) throw new Error('You need to initialize browser first') - return rpc.callBrowser< + + return await rpc.callBrowser< typeof args, EventName, T.RageFW_CefReturn - >(this.browser, eventName, args) + >(eventName, args) } } diff --git a/client/src/types/client.ts b/client/src/types/client.ts index 08e0719..dca0f87 100644 --- a/client/src/types/client.ts +++ b/client/src/types/client.ts @@ -25,7 +25,7 @@ export type RageFW_ClientArgs = /** * Return type for an event, name of which you pass as a generic - * These only include custom events + * These include custom and system events */ export type RageFW_ClientReturn = K extends keyof RageFW_ICustomClientEvent @@ -36,7 +36,7 @@ export type RageFW_ClientReturn = /** * Callback (function) for an event, name of which you pass as a generic - * These only include custom events + * These include custom and system events */ export type RageFW_ClientCallback = ( ...args: RageFW_ClientArgs diff --git a/server/src/core/index.ts b/server/src/core/index.ts index 48fa963..b6900db 100644 --- a/server/src/core/index.ts +++ b/server/src/core/index.ts @@ -1,3 +1,4 @@ -export * from './player' -export * from './server' export * from './logger' +export * from './player' +export * from './rpc' +export * from './server' diff --git a/server/src/core/player.ts b/server/src/core/player.ts index 4ab4330..2838221 100644 --- a/server/src/core/player.ts +++ b/server/src/core/player.ts @@ -1,31 +1,27 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' - +import { rpc } from './rpc' import type * as T from '../types' export class Player { - private _rpc: Rpc = new Rpc() - - get rpc(): Rpc { - return this._rpc - } - - public triggerClient( + public async triggerClient( player: PlayerMp, eventName: EventName, ...args: T._ClientEventHasArgs extends true - ? [T.RageFW_ServerClientArgs] + ? [T.RageFW_ClientArgs] : [] - ): Promise> { - return this._rpc.callClient(player, eventName, args) + ): Promise> { + return await rpc.callClient(player, eventName, args) } - public triggerBrowser( + public async triggerBrowser( player: PlayerMp, eventName: EventName, ...args: T._CefEventHasArgs extends true - ? [T.RageFW_CefArgs] + ? [T.RageFW_BrowserArgs] : [] - ): Promise> { - return this._rpc.callBrowser(player, eventName, args) + ): Promise> { + return await rpc.callBrowser(player, eventName, args) } } + +// new Player().triggerBrowser({} as PlayerMp, 'customCefEvent', ['', 1]) +// new Player().triggerClient({} as PlayerMp, 'customClientEvent', ['', 1]) diff --git a/server/src/core/rpc.ts b/server/src/core/rpc.ts new file mode 100644 index 0000000..4972ca0 --- /dev/null +++ b/server/src/core/rpc.ts @@ -0,0 +1,5 @@ +import { Rpc } from '@entityseven/rage-fw-rpc' + +export const rpc = new Rpc({ + debugLogs: false, +}) diff --git a/server/src/core/server.ts b/server/src/core/server.ts index 15fc60b..c3cea7e 100644 --- a/server/src/core/server.ts +++ b/server/src/core/server.ts @@ -1,109 +1,39 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' -import { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types' - -import { nativeEvents } from '../data/nativeEvents' +import { rpc } from './rpc' import type * as T from '../types' export class Server { - private _rpc: Rpc = new Rpc() - - get rpc(): Rpc { - return this._rpc - } - - private isNativeEvent(eventName: string): eventName is keyof IServerEvents { - return nativeEvents.includes(eventName) - } - - private registerCustom( - eventName: EventName, - callback: T.RageFW_ServerCallbackCustom, - ): void { - this._rpc.register( - eventName, - // fixme - async (args: T.RageFW_ServerArgs, info) => { - await callback([info.player as PlayerMp, ...args]) - }, - ) - } - - private registerNative( - eventName: EventName, - callback: T.RageFW_ServerCallbackNative, - ): void { - mp.events.add( - eventName, - (...args: Parameters) => - callback([...args]), - ) - } - public register( eventName: EventName, callback: T.RageFW_ServerCallback, - ): void { - if (this.isNativeEvent(eventName)) { - this.registerNative( - eventName, - callback as T.RageFW_ServerCallbackNative, - ) - } else { - this.registerCustom( - eventName, - callback as unknown as T.RageFW_ServerCallbackCustom, - ) - } - } + ): Server { + rpc.register< + Parameters, + ReturnType, + EventName + >(eventName, async (...data) => await callback(...data)) - public registerMany(events: { - [event in EventName]: T.RageFW_ServerCallback - }): void { - Object.entries>(events).map( - ([eventName, callback]) => { - if (this.isNativeEvent(eventName)) { - this.registerNative( - eventName, - callback as T.RageFW_ServerCallbackNative, - ) - } else { - this.registerCustom( - eventName as keyof RageFW_ICustomServerEvent, - callback as unknown as T.RageFW_ServerCallbackCustom, - ) - } - }, - ) - } - - private unregisterCustom( - eventName: EventName, - ): void { - this._rpc.unregister(eventName) - } - - private unregisterNative( - eventName: EventName, - ): void { - mp.events.remove(eventName) + return this } public unregister( eventName: EventName, - ): void { - if (this.isNativeEvent(eventName)) { - this.unregisterNative(eventName) - } else { - this.unregisterCustom(eventName) - } + ): Server { + rpc.unregister(eventName) + + return this } - public trigger( - eventName: EventName, - ...args: T._ServerEventHasArgs extends true - ? [T.RageFW_ServerArgs] - : [] - ): Promise> { - return this._rpc.call(eventName, args) - } + // fixme + // public trigger( + // eventName: EventName, + // ...args: T._ServerEventHasArgs extends true + // ? [T.RageFW_ServerArgs] + // : [] + // ): Promise> { + // return rpc.call(eventName, args) + // } } + +// new Server() +// .register('customServerEvent', async (a, b, c) => true) +// .unregister('customServerEvent') diff --git a/server/src/index.ts b/server/src/index.ts index 0bbda5a..555ca74 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,4 @@ -import { Logger, Player, Server } from './core' +import { Logger, Player, Server, rpc } from './core' export const fw = { event: new Server(), @@ -6,6 +6,7 @@ export const fw = { system: { log: new Logger(), }, + rpc, } fw.system.log.info( diff --git a/server/src/types/browser.ts b/server/src/types/browser.ts index c3ac629..eb410a0 100644 --- a/server/src/types/browser.ts +++ b/server/src/types/browser.ts @@ -1,15 +1,30 @@ import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' -export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent +/** + * Union of all available browser event names + * These only include custom events + */ +export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent -export type RageFW_CefArgs = Parameters< +/** + * Array of arguments of an event you pass as a generic + * These only include custom events + */ +export type RageFW_BrowserArgs = Parameters< RageFW_ICustomBrowserEvent[K] > -export type RageFW_CefReturn = ReturnType< +/** + * Return type of event you pass as a generic + * These only include custom events + */ +export type RageFW_BrowserReturn = ReturnType< RageFW_ICustomBrowserEvent[K] > +/** + * + */ export type _CefEventHasArgs< EventName extends keyof RageFW_ICustomBrowserEvent, > = keyof RageFW_ICustomBrowserEvent extends never diff --git a/server/src/types/client.ts b/server/src/types/client.ts index 0416299..44fc16f 100644 --- a/server/src/types/client.ts +++ b/server/src/types/client.ts @@ -12,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent * Array of arguments of an event you pass as a generic * These only include custom events */ -export type RageFW_ServerClientArgs = +export type RageFW_ClientArgs = K extends RageFW_ClientEvent ? Parameters : never @@ -21,11 +21,14 @@ export type RageFW_ServerClientArgs = * Return type of event you pass as a generic * These only include custom events */ -export type RageFW_ServerClientReturn = +export type RageFW_ClientReturn = K extends RageFW_ClientEvent ? ReturnType : never +/** + * + */ export type _ClientEventHasArgs< EventName extends keyof RageFW_ICustomClientEvent, > = keyof RageFW_ICustomClientEvent extends never diff --git a/server/src/types/server.ts b/server/src/types/server.ts index 244f9b7..8cfd2dc 100644 --- a/server/src/types/server.ts +++ b/server/src/types/server.ts @@ -4,6 +4,7 @@ import type { RageFW_ICustomClientEvent, RageFW_ICustomServerEvent, } from '@entityseven/rage-fw-shared-types' + export type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types' /** @@ -20,20 +21,9 @@ export type RageFW_ServerEvent = */ export type RageFW_ServerArgs = K extends keyof RageFW_ICustomServerEvent - ? Parameters + ? [PlayerMp, ...Parameters] : K extends keyof IServerEvents - ? Parameters - : never - -/** - * Callback (function) for an event, name of which you pass as a generic - * These include system and custom events - */ -export type RageFW_ServerCallback = - K extends keyof RageFW_ICustomServerEvent - ? RageFW_ServerCallbackCustom - : K extends keyof IServerEvents - ? RageFW_ServerCallbackNative + ? [PlayerMp, Parameters] : never /** @@ -45,28 +35,19 @@ export type RageFW_ServerReturn = ? ReturnType : K extends keyof IServerEvents ? ReturnType - : never + : void /** - * Array of arguments for an event, name of which you pass as a generic - * These only include custom events + * Callback (function) for an event, name of which you pass as a generic + * These include system and custom events */ -export type RageFW_ServerCallbackCustom< - K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, -> = ( - payload: [player: PlayerMp, ...args: RageFW_ServerArgs], +export type RageFW_ServerCallback = ( + ...args: RageFW_ServerArgs ) => Promise> /** - * Array of arguments for an event, name of which you pass as a generic - * These only include system events + * */ -export type RageFW_ServerCallbackNative< - K extends keyof IServerEvents = keyof IServerEvents, -> = ( - payload: Parameters, -) => Promise> - export type _ServerEventHasArgs = EventName extends keyof RageFW_ICustomServerEvent ? keyof RageFW_ICustomClientEvent extends never