diff --git a/cef/package.json b/browser/package.json similarity index 93% rename from cef/package.json rename to browser/package.json index 42d4af5..6c41277 100644 --- a/cef/package.json +++ b/browser/package.json @@ -1,5 +1,5 @@ { - "name": "@entityseven/rage-fw-cef", + "name": "@entityseven/rage-fw-browser", "version": "0.1.0", "main": "dist/index.js", "types": "dist/src/index.d.ts", 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..95e2b59 --- /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 f3c7aad..0000000 --- a/cef/src/index.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { Rpc } from '@entityseven/rage-fw-rpc' -import type * as T 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: T.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: T._CefEventHasArgs extends true - ? [T.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 T.RageFW_ICustomServerEvent, - >( - eventName: EventName, - ...args: T._ServerEventHasArgs extends true - ? [T.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 T.RageFW_ICustomClientEvent, - >( - eventName: EventName, - ...args: T._ClientEventHasArgs extends true - ? [T.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 130bf48..0000000 --- a/cef/src/types/cef.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type { 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 - * 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/src/types/browser.ts b/client/src/types/browser.ts index 9b266ac..bcb0321 100644 --- a/client/src/types/browser.ts +++ b/client/src/types/browser.ts @@ -1,20 +1,21 @@ /// -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 +export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent export type RageFW_CefArgs = Parameters< - RageFW_ICustomCefEvent[K] + RageFW_ICustomBrowserEvent[K] > export type RageFW_CefReturn = ReturnType< - RageFW_ICustomCefEvent[K] + RageFW_ICustomBrowserEvent[K] > -export type _CefEventHasArgs = - keyof RageFW_ICustomCefEvent extends never - ? false - : Parameters[0] extends undefined - ? false - : true +export type _CefEventHasArgs< + EventName extends keyof RageFW_ICustomBrowserEvent, +> = keyof RageFW_ICustomBrowserEvent extends never + ? false + : Parameters[0] extends undefined + ? false + : true 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/server/src/types/browser.ts b/server/src/types/browser.ts index 4a40c56..c3ac629 100644 --- a/server/src/types/browser.ts +++ b/server/src/types/browser.ts @@ -1,18 +1,19 @@ -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 +export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent export type RageFW_CefArgs = Parameters< - RageFW_ICustomCefEvent[K] + RageFW_ICustomBrowserEvent[K] > export type RageFW_CefReturn = ReturnType< - RageFW_ICustomCefEvent[K] + RageFW_ICustomBrowserEvent[K] > -export type _CefEventHasArgs = - keyof RageFW_ICustomCefEvent extends never - ? false - : Parameters[0] extends undefined - ? false - : true +export type _CefEventHasArgs< + EventName extends keyof RageFW_ICustomBrowserEvent, +> = keyof RageFW_ICustomBrowserEvent extends never + ? false + : Parameters[0] extends undefined + ? false + : true 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 + } }