From 3bbf1e220925c0deb1da4d26cd3d5e62bccb5d7b Mon Sep 17 00:00:00 2001 From: Danya H Date: Mon, 28 Oct 2024 16:53:42 +0000 Subject: [PATCH] upd core - typed client inner rpc - better type organization --- client/src/core/client.ts | 34 +++++++++++++++++----------------- client/src/core/index.ts | 3 ++- client/src/core/player.ts | 36 +++++++++++++++++++++++------------- client/src/core/rpc.ts | 5 +++++ client/src/index.ts | 3 ++- client/src/types/browser.ts | 8 ++++++++ client/src/types/client.ts | 25 ++++++++++++++++--------- client/src/types/server.ts | 6 +++--- 8 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 client/src/core/rpc.ts diff --git a/client/src/core/client.ts b/client/src/core/client.ts index 0f40886..7b75cf8 100644 --- a/client/src/core/client.ts +++ b/client/src/core/client.ts @@ -1,29 +1,29 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' - +import { rpc } from './rpc' import type * as T from '../types' export class Client { - private _rpc: Rpc = new Rpc() - - get rpc(): Rpc { - return this._rpc - } - public register( eventName: EventName, callback: T.RageFW_ClientCallback, - ): void { - this._rpc.register( - eventName, - async (data: T.RageFW_ClientArgs) => { - return await callback(data) - }, - ) + ): Client { + rpc.register< + Parameters, + ReturnType, + EventName + >(eventName, async (...data) => await callback(...data)) + + return this } public unregister( eventName: EventName, - ): void { - this._rpc.unregister(eventName) + ): Client { + rpc.unregister(eventName) + + return this } } + +// new Client() +// .register('customClientEvent', async (a, b) => true) +// .unregister('customClientEvent') diff --git a/client/src/core/index.ts b/client/src/core/index.ts index cde62c3..375ef93 100644 --- a/client/src/core/index.ts +++ b/client/src/core/index.ts @@ -1,3 +1,4 @@ export * from './client' -export * from './player' export * from './logger' +export * from './player' +export * from './rpc' diff --git a/client/src/core/player.ts b/client/src/core/player.ts index 6a31096..0e7a511 100644 --- a/client/src/core/player.ts +++ b/client/src/core/player.ts @@ -1,31 +1,33 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' -import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types' - +import { rpc } from './rpc' import type * as T from '../types' export class Player { - private _rpc: Rpc = new Rpc() public browser: BrowserMp | undefined - get rpc(): Rpc { - return this._rpc - } - public trigger( + public trigger( eventName: EventName, ...args: T._ClientEventHasArgs extends true ? [T.RageFW_ClientArgs] : [] ): Promise> { - return this._rpc.call(eventName, args) + return rpc.call< + typeof args, + EventName, + T.RageFW_ClientReturn + >(eventName, args) } - public triggerServer( + public triggerServer( eventName: EventName, ...args: T._ServerEventHasArgs extends true - ? [T.RageFW_ClientServerArgs] + ? [T.RageFW_ServerArgs] : [] ): Promise> { - return this._rpc.callServer(eventName, args) + return rpc.callServer< + typeof args, + EventName, + T.RageFW_ClientServerReturn + >(eventName, args) } public triggerBrowser( @@ -36,6 +38,14 @@ export class Player { ): Promise> { if (!this.browser) throw new Error('You need to initialize browser first') - return this._rpc.callBrowser(this.browser, eventName, args) + return rpc.callBrowser< + typeof args, + EventName, + T.RageFW_CefReturn + >(this.browser, eventName, args) } } + +// new Player().trigger('customClientEvent', ['', 1]) +// new Player().triggerServer('customServerEvent', ['', 1]) +// new Player().triggerBrowser('customCefEvent', ['', 1]) diff --git a/client/src/core/rpc.ts b/client/src/core/rpc.ts new file mode 100644 index 0000000..4972ca0 --- /dev/null +++ b/client/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/client/src/index.ts b/client/src/index.ts index d6c1455..f653742 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -1,4 +1,4 @@ -import { Client, Logger, Player } from './core' +import { Client, Logger, Player, rpc } from './core' export const fw = { event: new Client(), @@ -6,4 +6,5 @@ export const fw = { system: { log: new Logger(), }, + rpc, } diff --git a/client/src/types/browser.ts b/client/src/types/browser.ts index bcb0321..a4b343d 100644 --- a/client/src/types/browser.ts +++ b/client/src/types/browser.ts @@ -2,8 +2,16 @@ import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' +/** + * Union of all available browser event names callable from client + * These only include custom events + */ export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent +/** + * Array of arguments for an event, name of which you pass as a generic + * These only include custom events + */ export type RageFW_CefArgs = Parameters< RageFW_ICustomBrowserEvent[K] > diff --git a/client/src/types/client.ts b/client/src/types/client.ts index a94eea9..08e0719 100644 --- a/client/src/types/client.ts +++ b/client/src/types/client.ts @@ -2,6 +2,8 @@ import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types' +export type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types' + /** * Union of all available client event names * These include custom and system events @@ -21,14 +23,6 @@ export type RageFW_ClientArgs = ? Parameters : never -/** - * Callback (function) for an event, name of which you pass as a generic - * These only include custom events - */ -export type RageFW_ClientCallback = ( - args: RageFW_ClientArgs, -) => Promise> - /** * Return type for an event, name of which you pass as a generic * These only include custom events @@ -36,8 +30,21 @@ export type RageFW_ClientCallback = ( export type RageFW_ClientReturn = K extends keyof RageFW_ICustomClientEvent ? ReturnType - : void + : K extends keyof IClientEvents + ? ReturnType + : void +/** + * Callback (function) for an event, name of which you pass as a generic + * These only include custom events + */ +export type RageFW_ClientCallback = ( + ...args: RageFW_ClientArgs +) => Promise> + +/** + * + */ export type _ClientEventHasArgs< EventName extends keyof RageFW_ICustomClientEvent, > = keyof RageFW_ICustomClientEvent extends never diff --git a/client/src/types/server.ts b/client/src/types/server.ts index 23ce9df..3cc520e 100644 --- a/client/src/types/server.ts +++ b/client/src/types/server.ts @@ -9,13 +9,13 @@ import type { * Union of all available server event names callable from client * These only include custom events */ -export type RageFW_ClientServerEvent = keyof RageFW_ICustomServerEvent +export type RageFW_ServerEvent = keyof RageFW_ICustomServerEvent /** * Array of arguments for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientServerArgs = +export type RageFW_ServerArgs = K extends keyof RageFW_ICustomServerEvent ? Parameters : never @@ -24,7 +24,7 @@ export type RageFW_ClientServerArgs = * Return type for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientServerReturn = +export type RageFW_ClientServerReturn = K extends keyof RageFW_ICustomServerEvent ? ReturnType : never