diff --git a/cef/package.json b/browser/package.json similarity index 90% rename from cef/package.json rename to browser/package.json index 42d4af5..4dbb75b 100644 --- a/cef/package.json +++ b/browser/package.json @@ -1,6 +1,6 @@ { - "name": "@entityseven/rage-fw-cef", - "version": "0.1.0", + "name": "@entityseven/rage-fw-browser", + "version": "0.1.2", "main": "dist/index.js", "types": "dist/src/index.d.ts", "files": [ diff --git a/cef/readme.md b/browser/readme.md similarity index 100% rename from cef/readme.md rename to browser/readme.md diff --git a/browser/src/core/browser.ts b/browser/src/core/browser.ts new file mode 100644 index 0000000..de7efdc --- /dev/null +++ b/browser/src/core/browser.ts @@ -0,0 +1,96 @@ +import { Helper } from './helper' +import { rpc } from './rpc' +import type * as T from '../types' +import { + RageFW_BrowserEvent, + RageFW_ClientEvent, + RageFW_ServerEvent, +} from '../types' + +export class Browser extends Helper { + constructor() { + super() + } + + set debugLogs(debug: boolean) { + this.debugLogs_ = debug + } + + set customLogger( + fn: (method: string, eventName: string, ...args: unknown[]) => unknown, + ) { + this.customLogger_ = fn + } + + public register( + eventName: EventName, + callback: T.RageFW_BrowserCallback, + ): Browser { + this.log_('register', eventName, callback) + + rpc.register< + Parameters, + ReturnType, + EventName + >(eventName, async (...data) => await callback(...data)) + + return this + } + + public unregister( + eventName: EventName, + ): Browser { + rpc.unregister(eventName) + + return this + } + + public async trigger( + eventName: EventName, + ...args: T._BrowserEventHasArgs extends true + ? [T.RageFW_BrowserArgs] + : [] + ): Promise> { + this.log_('[RPC](trigger):', eventName, ...args) + + return await rpc.call< + typeof args, + EventName, + T.RageFW_BrowserReturn + >(eventName, args) + } + + public async triggerServer( + eventName: EventName, + ...args: T._ServerEventHasArgs extends true + ? [T.RageFW_ServerArgs] + : [] + ): Promise> { + this.log_('[RPC](triggerServer):', eventName, ...args) + + return await rpc.callServer< + typeof args, + EventName, + T.RageFW_ServerReturn + >(eventName, args) + } + + public async triggerClient( + eventName: EventName, + ...args: T._ClientEventHasArgs extends true + ? [T.RageFW_ClientArgs] + : [] + ): Promise> { + this.log_('[RPC](triggerClient):', eventName, ...args) + + return await rpc.callClient< + typeof args, + EventName, + T.RageFW_ClientReturn + >(eventName, args) + } +} + +// new Browser() +// .register('customCefEvent', async (a, b) => true) +// .triggerServer('customServerEvent', ['', 1]) diff --git a/browser/src/core/helper.ts b/browser/src/core/helper.ts new file mode 100644 index 0000000..d861710 --- /dev/null +++ b/browser/src/core/helper.ts @@ -0,0 +1,21 @@ +export class Helper { + protected debugLogs_: boolean = false + protected customLogger_: + | undefined + | ((method: string, eventName: string, ...args: unknown[]) => unknown) = + undefined + + constructor() {} + + protected log_( + method: string, + eventName: string, + ...args: unknown[] + ): void { + if (this.customLogger_) { + this.customLogger_(method, eventName, ...args) + } else if (this.debugLogs_) { + console.log('[RPC](' + method + ')', eventName + ':', ...args) + } + } +} diff --git a/browser/src/core/index.ts b/browser/src/core/index.ts new file mode 100644 index 0000000..1408bd0 --- /dev/null +++ b/browser/src/core/index.ts @@ -0,0 +1,3 @@ +export * from './browser' +export * from './helper' +export * from './rpc' diff --git a/browser/src/core/rpc.ts b/browser/src/core/rpc.ts new file mode 100644 index 0000000..1b31ed2 --- /dev/null +++ b/browser/src/core/rpc.ts @@ -0,0 +1,7 @@ +import { Rpc } from '@entityseven/rage-fw-rpc' + +export const rpc = new Rpc({ + forceBrowserDevMode: + process.env.RageFW_forceBrowserDevMode === 'true' ?? false, + debugLogs: false, +}) diff --git a/browser/src/index.ts b/browser/src/index.ts new file mode 100644 index 0000000..ad01249 --- /dev/null +++ b/browser/src/index.ts @@ -0,0 +1,9 @@ +import { Browser, rpc } from './core' + +export const fw = { + event: new Browser(), + rpc, +} +;(async () => { + await fw.event.triggerClient('cefReady') +})() diff --git a/browser/src/types/browser.ts b/browser/src/types/browser.ts new file mode 100644 index 0000000..ea66151 --- /dev/null +++ b/browser/src/types/browser.ts @@ -0,0 +1,40 @@ +import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' + +export type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' + +/** + * Union of all available browser event names + * These only include custom events + */ +export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent + +/** + * Array of arguments of an event you pass as a generic + * These only include custom browser events + */ +export type RageFW_BrowserArgs = Parameters< + RageFW_ICustomBrowserEvent[K] +> + +/** + * Return type of event you pass as a generic + * These only include custom browser events + */ +export type RageFW_BrowserReturn = ReturnType< + RageFW_ICustomBrowserEvent[K] +> + +/** + * Callback (function) of event you pass as a generic + * These only include custom browser events + */ +export type RageFW_BrowserCallback = + (...args: RageFW_BrowserArgs) => Promise> + +export type _BrowserEventHasArgs< + EventName extends keyof RageFW_ICustomBrowserEvent, +> = keyof RageFW_ICustomBrowserEvent extends never + ? false + : Parameters[0] extends undefined + ? false + : true diff --git a/cef/src/types/client.ts b/browser/src/types/client.ts similarity index 100% rename from cef/src/types/client.ts rename to browser/src/types/client.ts diff --git a/cef/src/types/index.ts b/browser/src/types/index.ts similarity index 65% rename from cef/src/types/index.ts rename to browser/src/types/index.ts index ef9b6be..1b288ef 100644 --- a/cef/src/types/index.ts +++ b/browser/src/types/index.ts @@ -1,3 +1,3 @@ -export * from './cef' +export * from './browser' export * from './client' export * from './server' diff --git a/cef/src/types/server.ts b/browser/src/types/server.ts similarity index 100% rename from cef/src/types/server.ts rename to browser/src/types/server.ts diff --git a/cef/tsconfig.json b/browser/tsconfig.json similarity index 100% rename from cef/tsconfig.json rename to browser/tsconfig.json diff --git a/cef/tsup.config.ts b/browser/tsup.config.ts similarity index 100% rename from cef/tsup.config.ts rename to browser/tsup.config.ts diff --git a/cef/src/index.ts b/cef/src/index.ts deleted file mode 100644 index 0ee8496..0000000 --- a/cef/src/index.ts +++ /dev/null @@ -1,113 +0,0 @@ -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' - -class Cef { - private _debugMode: boolean = false - private _rpc: Rpc = new Rpc() - - constructor() {} - - set debug(debug: boolean) { - this._debugMode = debug - } - - get rpc(): Rpc { - return this._rpc - } - - public register( - eventName: EventName, - callback: RageFW_CefCallback, - ): void { - if (this._debugMode) { - console.log('[RPC](register):', eventName, callback) - } - - if ('mp' in window) { - this._rpc.register(eventName, callback) - } - } - - public async trigger( - eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] - : [] - ): Promise> { - if (this._debugMode) { - console.log('[RPC](trigger):', eventName, ...args) - } - - if ('mp' in window) { - return await this._rpc.call(eventName, args) - } - - return Promise.reject( - 'RageFW was started in window which does not contain MP', - ) - } - - public async triggerServer< - EventName extends keyof RageFW_ICustomServerEvent, - >( - eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerArgs] - : [] - ): Promise> { - if (this._debugMode) { - console.log('[RPC](triggerServer):', eventName, ...args) - } - - if ('mp' in window) { - return await this._rpc.callServer(eventName, args) - } - - return Promise.reject( - 'RageFW was started in window which does not contain MP', - ) - } - - public async triggerClient< - EventName extends keyof RageFW_ICustomClientEvent, - >( - eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientArgs] - : [] - ): Promise> { - if (this._debugMode) { - console.log('[RPC](triggerClient):', eventName, ...args) - } - - if ('mp' in window) { - return await this._rpc.callClient(eventName, args) - } - - return Promise.reject( - 'RageFW was started in window which does not contain MP', - ) - } -} - -export const fw = { - event: new Cef(), -} -;(async () => { - await fw.event.triggerClient('cefReady') -})() diff --git a/cef/src/types/cef.ts b/cef/src/types/cef.ts deleted file mode 100644 index 501b63a..0000000 --- a/cef/src/types/cef.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' - -export { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' - -/** - * Union of all available cef event names - * These only include custom events - */ -export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent - -/** - * Array of arguments of an event you pass as a generic - * These only include custom cef events - */ -export type RageFW_CefArgs = Parameters< - RageFW_ICustomCefEvent[K] -> - -/** - * Return type of event you pass as a generic - * These only include custom cef events - */ -export type RageFW_CefReturn = ReturnType< - RageFW_ICustomCefEvent[K] -> - -/** - * Callback (function) of event you pass as a generic - * These only include custom cef events - */ -export type RageFW_CefCallback = ( - args: RageFW_CefArgs, -) => Promise> - -export type _CefEventHasArgs = - keyof RageFW_ICustomCefEvent extends never - ? false - : Parameters[0] extends undefined - ? false - : true diff --git a/client/package.json b/client/package.json index 4092690..da68719 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "@entityseven/rage-fw-client", - "version": "0.1.0", + "version": "0.1.2", "main": "dist/index.js", "types": "dist/src/index.d.ts", "files": [ diff --git a/client/src/core/client.ts b/client/src/core/client.ts index 365cda7..27e0f64 100644 --- a/client/src/core/client.ts +++ b/client/src/core/client.ts @@ -1,33 +1,29 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' - -import type { - RageFW_ClientArgs, - RageFW_ClientCallback, - RageFW_ClientEvent, -} from '../types' +import { rpc } from './rpc' +import type * as T from '../types' export class Client { - private _rpc: Rpc = new Rpc() + public register( + eventName: EventName, + callback: T.RageFW_ClientCallback, + ): Client { + rpc.register< + Parameters, + ReturnType, + EventName + >(eventName, async (...data) => await callback(...data)) - get rpc(): Rpc { - return this._rpc + return this } - public register( + public unregister( eventName: EventName, - callback: RageFW_ClientCallback, - ): void { - this._rpc.register( - eventName, - async (data: RageFW_ClientArgs) => { - return await callback(data) - }, - ) - } + ): Client { + rpc.unregister(eventName) - public unregister( - eventName: EventName, - ): void { - this._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 1af6056..e4949f0 100644 --- a/client/src/core/player.ts +++ b/client/src/core/player.ts @@ -1,53 +1,57 @@ -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 { rpc } from './rpc' +import type * as T from '../types' export class Player { - private _rpc: Rpc = new Rpc() - public browser: BrowserMp | undefined + private _browser: BrowserMp | undefined = undefined - get rpc(): Rpc { - return this._rpc - } - public trigger( - eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientArgs] - : [] - ): Promise> { - return this._rpc.call(eventName, args) + set browser(browser: BrowserMp) { + this._browser = browser + rpc.browser = browser } - public triggerServer( + public async trigger( eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ClientServerArgs] + ...args: T._ClientEventHasArgs extends true + ? [T.RageFW_ClientArgs] : [] - ): Promise> { - return this._rpc.callServer(eventName, args) + ): Promise> { + return await rpc.call< + typeof args, + EventName, + T.RageFW_ClientReturn + >(eventName, args) } - public triggerBrowser( + public async triggerServer( eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] + ...args: T._ServerEventHasArgs extends true + ? [T.RageFW_ServerArgs] : [] - ): Promise> { - if (!this.browser) + ): Promise> { + return await rpc.callServer< + typeof args, + EventName, + T.RageFW_ClientServerReturn + >(eventName, args) + } + + public async triggerBrowser( + eventName: EventName, + ...args: T._BrowserEventHasArgs extends true + ? [T.RageFW_BrowserArgs] + : [] + ): Promise> { + if (!this._browser) throw new Error('You need to initialize browser first') - return this._rpc.callBrowser(this.browser, eventName, args) + + return await rpc.callBrowser< + typeof args, + EventName, + T.RageFW_BrowserReturn + >(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/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/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 9b266ac..27292f7 100644 --- a/client/src/types/browser.ts +++ b/client/src/types/browser.ts @@ -1,20 +1,29 @@ /// -import type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' +import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' -export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent +/** + * Union of all available browser event names callable from client + * These only include custom events + */ +export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent -export type RageFW_CefArgs = Parameters< - RageFW_ICustomCefEvent[K] +/** + * Array of arguments for an event, name of which you pass as a generic + * These only include custom events + */ +export type RageFW_BrowserArgs = Parameters< + RageFW_ICustomBrowserEvent[K] > -export type RageFW_CefReturn = ReturnType< - RageFW_ICustomCefEvent[K] +export type RageFW_BrowserReturn = ReturnType< + RageFW_ICustomBrowserEvent[K] > -export type _CefEventHasArgs = - keyof RageFW_ICustomCefEvent extends never - ? false - : Parameters[0] extends undefined - ? false - : true +export type _BrowserEventHasArgs< + EventName extends keyof RageFW_ICustomBrowserEvent, +> = keyof RageFW_ICustomBrowserEvent extends never + ? false + : Parameters[0] extends undefined + ? false + : true diff --git a/client/src/types/client.ts b/client/src/types/client.ts index a94eea9..dca0f87 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,23 +23,28 @@ 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 + * These include custom and system events */ 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 include custom and system 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 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 4dda521..c7bce69 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,5 +1,5 @@ packages: - "server" - "client" - - "cef" + - "browser" - "shared-types" \ No newline at end of file diff --git a/readme.md b/readme.md index 484be8a..ccb70b2 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ You can find out more about our CLI [here](https://git.entityseven.com/entitysev At the moment automation we have only works via [pnpm](https://pnpm.io/). To scaffold a basic project with minor settings you can use our CLI: -``pnpm create rage-fw`` +``pnpm create @entityseven/rage-fw`` This will give you a few options, among them, you can find ``Initialize new project``. Use that option to scaffold a new project for yourself using the preferred frontend framework diff --git a/server/package.json b/server/package.json index df3c0fe..1aa9796 100644 --- a/server/package.json +++ b/server/package.json @@ -1,6 +1,6 @@ { "name": "@entityseven/rage-fw-server", - "version": "0.1.0", + "version": "0.1.2", "main": "dist/index.js", "types": "dist/src/index.d.ts", "files": [ 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 32e80eb..571fd1f 100644 --- a/server/src/core/player.ts +++ b/server/src/core/player.ts @@ -1,40 +1,27 @@ -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 { 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: _ClientEventHasArgs extends true - ? [RageFW_ServerClientArgs] + ...args: T._ClientEventHasArgs extends true + ? [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: _CefEventHasArgs extends true - ? [RageFW_CefArgs] + ...args: T._BrowserEventHasArgs extends true + ? [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 8515511..c3cea7e 100644 --- a/server/src/core/server.ts +++ b/server/src/core/server.ts @@ -1,117 +1,39 @@ -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 { 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( + public register( eventName: EventName, - callback: RageFW_ServerCallbackCustom, - ): void { - this._rpc.register( - eventName, - // fixme - async (args: RageFW_ServerArgs, info) => { - await callback([info.player as PlayerMp, ...args]) - }, - ) + callback: T.RageFW_ServerCallback, + ): Server { + rpc.register< + Parameters, + ReturnType, + EventName + >(eventName, async (...data) => await callback(...data)) + + return this } - private registerNative( + public unregister( eventName: EventName, - callback: RageFW_ServerCallbackNative, - ): void { - mp.events.add( - eventName, - (...args: Parameters) => - callback([...args]), - ) + ): Server { + rpc.unregister(eventName) + + return this } - public register( - eventName: EventName, - callback: RageFW_ServerCallback, - ): void { - if (this.isNativeEvent(eventName)) { - this.registerNative( - eventName, - callback as RageFW_ServerCallbackNative, - ) - } else { - this.registerCustom( - eventName, - callback as unknown as RageFW_ServerCallbackCustom, - ) - } - } - - public registerMany(events: { - [event in EventName]: RageFW_ServerCallback - }): void { - Object.entries>(events).map( - ([eventName, callback]) => { - if (this.isNativeEvent(eventName)) { - this.registerNative( - eventName, - callback as RageFW_ServerCallbackNative, - ) - } else { - this.registerCustom( - eventName as keyof RageFW_ICustomServerEvent, - callback as unknown as RageFW_ServerCallbackCustom, - ) - } - }, - ) - } - - private unregisterCustom( - eventName: EventName, - ): void { - this._rpc.unregister(eventName) - } - - private unregisterNative( - eventName: EventName, - ): void { - mp.events.remove(eventName) - } - - public unregister( - eventName: EventName, - ): void { - if (this.isNativeEvent(eventName)) { - this.unregisterNative(eventName) - } else { - this.unregisterCustom(eventName) - } - } - - public trigger( - eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [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/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 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 4a40c56..41c4f29 100644 --- a/server/src/types/browser.ts +++ b/server/src/types/browser.ts @@ -1,18 +1,34 @@ -import type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types' +import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' -export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent +/** + * Union of all available browser event names + * These only include custom events + */ +export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent -export type RageFW_CefArgs = Parameters< - RageFW_ICustomCefEvent[K] +/** + * 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< - RageFW_ICustomCefEvent[K] +/** + * 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 = - keyof RageFW_ICustomCefEvent extends never - ? false - : Parameters[0] extends undefined - ? false - : true +/** + * + */ +export type _BrowserEventHasArgs< + EventName extends keyof RageFW_ICustomBrowserEvent, +> = keyof RageFW_ICustomBrowserEvent extends never + ? false + : Parameters[0] extends undefined + ? false + : true 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 diff --git a/shared-types/package.json b/shared-types/package.json index ad693a7..9ece53d 100644 --- a/shared-types/package.json +++ b/shared-types/package.json @@ -1,6 +1,6 @@ { "name": "@entityseven/rage-fw-shared-types", - "version": "0.1.0", + "version": "0.1.2", "types": "types/types/index.d.ts", "files": [ "types/**/*" diff --git a/shared-types/types/types/index.d.ts b/shared-types/types/types/index.d.ts index 4d7acb4..bf18c34 100644 --- a/shared-types/types/types/index.d.ts +++ b/shared-types/types/types/index.d.ts @@ -1,9 +1,14 @@ declare module '@entityseven/rage-fw-shared-types' { - export interface RageFW_ICustomServerEvent {} + export interface RageFW_ICustomServerEvent { + customServerEvent(arg1: string, arg2: number): boolean + } export interface RageFW_ICustomClientEvent { cefReady(): void + customClientEvent(arg1: string, arg2: number): boolean } - export interface RageFW_ICustomCefEvent {} + export interface RageFW_ICustomBrowserEvent { + customCefEvent(arg1: string, arg2: number): boolean + } }