From 588bb42d04659c34f3b1b08c9b756a6c959e9c1c Mon Sep 17 00:00:00 2001 From: Danya H Date: Mon, 28 Oct 2024 15:20:43 +0000 Subject: [PATCH] upd core - types now use namespace to avoid bloating - moved items for further usage into data folder --- cef/src/index.ts | 45 +++++++------------ cef/src/types/cef.ts | 4 +- client/src/core/client.ts | 14 +++--- client/src/core/player.ts | 36 +++++---------- client/src/{ => data}/keys.ts | 0 server/src/core/player.ts | 27 ++++------- server/src/core/server.ts | 44 ++++++++---------- .../nativeEvents.ts} | 0 8 files changed, 61 insertions(+), 109 deletions(-) rename client/src/{ => data}/keys.ts (100%) rename server/src/{native.events.ts => data/nativeEvents.ts} (100%) diff --git a/cef/src/index.ts b/cef/src/index.ts index 0ee8496..f3c7aad 100644 --- a/cef/src/index.ts +++ b/cef/src/index.ts @@ -1,20 +1,5 @@ import { Rpc } from '@entityseven/rage-fw-rpc' - -import type { - _CefEventHasArgs, - _ClientEventHasArgs, - _ServerEventHasArgs, - RageFW_CefArgs, - RageFW_CefCallback, - RageFW_CefReturn, - RageFW_ClientArgs, - RageFW_ClientReturn, - RageFW_ICustomCefEvent, - RageFW_ICustomClientEvent, - RageFW_ICustomServerEvent, - RageFW_ServerArgs, - RageFW_ServerReturn, -} from './types' +import type * as T from './types' class Cef { private _debugMode: boolean = false @@ -30,9 +15,9 @@ class Cef { return this._rpc } - public register( + public register( eventName: EventName, - callback: RageFW_CefCallback, + callback: T.RageFW_CefCallback, ): void { if (this._debugMode) { console.log('[RPC](register):', eventName, callback) @@ -43,12 +28,12 @@ class Cef { } } - public async trigger( + public async trigger( eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] + ...args: T._CefEventHasArgs extends true + ? [T.RageFW_CefArgs] : [] - ): Promise> { + ): Promise> { if (this._debugMode) { console.log('[RPC](trigger):', eventName, ...args) } @@ -63,13 +48,13 @@ class Cef { } public async triggerServer< - EventName extends keyof RageFW_ICustomServerEvent, + EventName extends keyof T.RageFW_ICustomServerEvent, >( eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerArgs] + ...args: T._ServerEventHasArgs extends true + ? [T.RageFW_ServerArgs] : [] - ): Promise> { + ): Promise> { if (this._debugMode) { console.log('[RPC](triggerServer):', eventName, ...args) } @@ -84,13 +69,13 @@ class Cef { } public async triggerClient< - EventName extends keyof RageFW_ICustomClientEvent, + EventName extends keyof T.RageFW_ICustomClientEvent, >( eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientArgs] + ...args: T._ClientEventHasArgs extends true + ? [T.RageFW_ClientArgs] : [] - ): Promise> { + ): Promise> { if (this._debugMode) { console.log('[RPC](triggerClient):', eventName, ...args) } diff --git a/cef/src/types/cef.ts b/cef/src/types/cef.ts index 501b63a..130bf48 100644 --- a/cef/src/types/cef.ts +++ b/cef/src/types/cef.ts @@ -1,6 +1,6 @@ -import { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' +import type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' -export { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' +export type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' /** * Union of all available cef event names diff --git a/client/src/core/client.ts b/client/src/core/client.ts index 365cda7..0f40886 100644 --- a/client/src/core/client.ts +++ b/client/src/core/client.ts @@ -1,10 +1,6 @@ import { Rpc } from '@entityseven/rage-fw-rpc' -import type { - RageFW_ClientArgs, - RageFW_ClientCallback, - RageFW_ClientEvent, -} from '../types' +import type * as T from '../types' export class Client { private _rpc: Rpc = new Rpc() @@ -13,19 +9,19 @@ export class Client { return this._rpc } - public register( + public register( eventName: EventName, - callback: RageFW_ClientCallback, + callback: T.RageFW_ClientCallback, ): void { this._rpc.register( eventName, - async (data: RageFW_ClientArgs) => { + async (data: T.RageFW_ClientArgs) => { return await callback(data) }, ) } - public unregister( + public unregister( eventName: EventName, ): void { this._rpc.unregister(eventName) diff --git a/client/src/core/player.ts b/client/src/core/player.ts index 1af6056..6a31096 100644 --- a/client/src/core/player.ts +++ b/client/src/core/player.ts @@ -1,19 +1,7 @@ import { Rpc } from '@entityseven/rage-fw-rpc' import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types' -import { - _CefEventHasArgs, - _ClientEventHasArgs, - _ServerEventHasArgs, - RageFW_CefArgs, - RageFW_CefEvent, - RageFW_CefReturn, - RageFW_ClientArgs, - RageFW_ClientReturn, - RageFW_ClientServerEvent, - RageFW_ClientServerArgs, - RageFW_ClientServerReturn, -} from '../types' +import type * as T from '../types' export class Player { private _rpc: Rpc = new Rpc() @@ -24,28 +12,28 @@ export class Player { } public trigger( eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientArgs] + ...args: T._ClientEventHasArgs extends true + ? [T.RageFW_ClientArgs] : [] - ): Promise> { + ): Promise> { return this._rpc.call(eventName, args) } - public triggerServer( + public triggerServer( eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ClientServerArgs] + ...args: T._ServerEventHasArgs extends true + ? [T.RageFW_ClientServerArgs] : [] - ): Promise> { + ): Promise> { return this._rpc.callServer(eventName, args) } - public triggerBrowser( + public triggerBrowser( eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] + ...args: T._CefEventHasArgs extends true + ? [T.RageFW_CefArgs] : [] - ): Promise> { + ): Promise> { if (!this.browser) throw new Error('You need to initialize browser first') return this._rpc.callBrowser(this.browser, eventName, args) diff --git a/client/src/keys.ts b/client/src/data/keys.ts similarity index 100% rename from client/src/keys.ts rename to client/src/data/keys.ts diff --git a/server/src/core/player.ts b/server/src/core/player.ts index 32e80eb..4ab4330 100644 --- a/server/src/core/player.ts +++ b/server/src/core/player.ts @@ -1,15 +1,6 @@ import { Rpc } from '@entityseven/rage-fw-rpc' -import type { - _CefEventHasArgs, - _ClientEventHasArgs, - RageFW_CefArgs, - RageFW_CefEvent, - RageFW_CefReturn, - RageFW_ClientEvent, - RageFW_ServerClientArgs, - RageFW_ServerClientReturn, -} from '../types' +import type * as T from '../types' export class Player { private _rpc: Rpc = new Rpc() @@ -18,23 +9,23 @@ export class Player { return this._rpc } - public triggerClient( + public triggerClient( player: PlayerMp, eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ServerClientArgs] + ...args: T._ClientEventHasArgs extends true + ? [T.RageFW_ServerClientArgs] : [] - ): Promise> { + ): Promise> { return this._rpc.callClient(player, eventName, args) } - public triggerBrowser( + public triggerBrowser( player: PlayerMp, eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] + ...args: T._CefEventHasArgs extends true + ? [T.RageFW_CefArgs] : [] - ): Promise> { + ): Promise> { return this._rpc.callBrowser(player, eventName, args) } } diff --git a/server/src/core/server.ts b/server/src/core/server.ts index 8515511..15fc60b 100644 --- a/server/src/core/server.ts +++ b/server/src/core/server.ts @@ -1,16 +1,8 @@ import { Rpc } from '@entityseven/rage-fw-rpc' import { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types' -import { nativeEvents } from '../native.events' -import type { - _ServerEventHasArgs, - RageFW_ServerArgs, - RageFW_ServerCallback, - RageFW_ServerCallbackCustom, - RageFW_ServerCallbackNative, - RageFW_ServerEvent, - RageFW_ServerReturn, -} from '../types' +import { nativeEvents } from '../data/nativeEvents' +import type * as T from '../types' export class Server { private _rpc: Rpc = new Rpc() @@ -25,12 +17,12 @@ export class Server { private registerCustom( eventName: EventName, - callback: RageFW_ServerCallbackCustom, + callback: T.RageFW_ServerCallbackCustom, ): void { this._rpc.register( eventName, // fixme - async (args: RageFW_ServerArgs, info) => { + async (args: T.RageFW_ServerArgs, info) => { await callback([info.player as PlayerMp, ...args]) }, ) @@ -38,7 +30,7 @@ export class Server { private registerNative( eventName: EventName, - callback: RageFW_ServerCallbackNative, + callback: T.RageFW_ServerCallbackNative, ): void { mp.events.add( eventName, @@ -47,37 +39,37 @@ export class Server { ) } - public register( + public register( eventName: EventName, - callback: RageFW_ServerCallback, + callback: T.RageFW_ServerCallback, ): void { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, - callback as RageFW_ServerCallbackNative, + callback as T.RageFW_ServerCallbackNative, ) } else { this.registerCustom( eventName, - callback as unknown as RageFW_ServerCallbackCustom, + callback as unknown as T.RageFW_ServerCallbackCustom, ) } } - public registerMany(events: { - [event in EventName]: RageFW_ServerCallback + public registerMany(events: { + [event in EventName]: T.RageFW_ServerCallback }): void { - Object.entries>(events).map( + Object.entries>(events).map( ([eventName, callback]) => { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, - callback as RageFW_ServerCallbackNative, + callback as T.RageFW_ServerCallbackNative, ) } else { this.registerCustom( eventName as keyof RageFW_ICustomServerEvent, - callback as unknown as RageFW_ServerCallbackCustom, + callback as unknown as T.RageFW_ServerCallbackCustom, ) } }, @@ -96,7 +88,7 @@ export class Server { mp.events.remove(eventName) } - public unregister( + public unregister( eventName: EventName, ): void { if (this.isNativeEvent(eventName)) { @@ -108,10 +100,10 @@ export class Server { public trigger( eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerArgs] + ...args: T._ServerEventHasArgs extends true + ? [T.RageFW_ServerArgs] : [] - ): Promise> { + ): Promise> { return this._rpc.call(eventName, args) } } diff --git a/server/src/native.events.ts b/server/src/data/nativeEvents.ts similarity index 100% rename from server/src/native.events.ts rename to server/src/data/nativeEvents.ts