- renamed cef -> browser
- added browser logger + custom logger option
This commit is contained in:
Danya H 2024-10-28 16:05:18 +00:00
parent 588bb42d04
commit 9fe9bad840
19 changed files with 208 additions and 163 deletions

View File

@ -1,5 +1,5 @@
{ {
"name": "@entityseven/rage-fw-cef", "name": "@entityseven/rage-fw-browser",
"version": "0.1.0", "version": "0.1.0",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/src/index.d.ts", "types": "dist/src/index.d.ts",

View File

@ -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 extends T.RageFW_BrowserEvent>(
eventName: EventName,
callback: T.RageFW_BrowserCallback<EventName>,
): Browser {
this.log_('register', eventName, callback)
rpc.register<
Parameters<typeof callback>,
ReturnType<typeof callback>,
EventName
>(eventName, async (...data) => await callback(...data))
return this
}
public unregister<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
): Browser {
rpc.unregister(eventName)
return this
}
public async trigger<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
...args: T._BrowserEventHasArgs<EventName> extends true
? [T.RageFW_BrowserArgs<EventName>]
: []
): Promise<T.RageFW_BrowserReturn<EventName>> {
this.log_('[RPC](trigger):', eventName, ...args)
return await rpc.call<
typeof args,
EventName,
T.RageFW_BrowserReturn<EventName>
>(eventName, args)
}
public async triggerServer<EventName extends T.RageFW_ServerEvent>(
eventName: EventName,
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<T.RageFW_ServerReturn<EventName>> {
this.log_('[RPC](triggerServer):', eventName, ...args)
return await rpc.callServer<
typeof args,
EventName,
T.RageFW_ServerReturn<EventName>
>(eventName, args)
}
public async triggerClient<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<T.RageFW_ClientReturn<EventName>> {
this.log_('[RPC](triggerClient):', eventName, ...args)
return await rpc.callClient<
typeof args,
EventName,
T.RageFW_ClientReturn<EventName>
>(eventName, args)
}
}
// new Browser()
// .register('customCefEvent', async (a, b) => true)
// .triggerServer('customServerEvent', ['', 1])

View File

@ -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)
}
}
}

View File

@ -0,0 +1,3 @@
export * from './browser'
export * from './helper'
export * from './rpc'

7
browser/src/core/rpc.ts Normal file
View File

@ -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,
})

9
browser/src/index.ts Normal file
View File

@ -0,0 +1,9 @@
import { Browser, rpc } from './core'
export const fw = {
event: new Browser(),
rpc,
}
;(async () => {
await fw.event.triggerClient('cefReady')
})()

View File

@ -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<K extends RageFW_BrowserEvent> = Parameters<
RageFW_ICustomBrowserEvent[K]
>
/**
* Return type of event you pass as a generic
* These only include custom browser events
*/
export type RageFW_BrowserReturn<K extends RageFW_BrowserEvent> = ReturnType<
RageFW_ICustomBrowserEvent[K]
>
/**
* Callback (function) of event you pass as a generic
* These only include custom browser events
*/
export type RageFW_BrowserCallback<K extends keyof RageFW_ICustomBrowserEvent> =
(...args: RageFW_BrowserArgs<K>) => Promise<RageFW_BrowserReturn<K>>
export type _BrowserEventHasArgs<
EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never
? false
: Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,3 +1,3 @@
export * from './cef' export * from './browser'
export * from './client' export * from './client'
export * from './server' export * from './server'

View File

@ -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 extends keyof T.RageFW_ICustomCefEvent>(
eventName: EventName,
callback: T.RageFW_CefCallback<EventName>,
): void {
if (this._debugMode) {
console.log('[RPC](register):', eventName, callback)
}
if ('mp' in window) {
this._rpc.register(eventName, callback)
}
}
public async trigger<EventName extends keyof T.RageFW_ICustomCefEvent>(
eventName: EventName,
...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>]
: []
): Promise<T.RageFW_CefReturn<EventName>> {
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<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<T.RageFW_ServerReturn<EventName>> {
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<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<T.RageFW_ClientReturn<EventName>> {
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')
})()

View File

@ -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<K extends RageFW_CefEvent> = Parameters<
RageFW_ICustomCefEvent[K]
>
/**
* Return type of event you pass as a generic
* These only include custom cef events
*/
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
RageFW_ICustomCefEvent[K]
>
/**
* Callback (function) of event you pass as a generic
* These only include custom cef events
*/
export type RageFW_CefCallback<K extends keyof RageFW_ICustomCefEvent> = (
args: RageFW_CefArgs<K>,
) => Promise<RageFW_CefReturn<K>>
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
keyof RageFW_ICustomCefEvent extends never
? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,20 +1,21 @@
/// <reference types="@ragempcommunity/types-client" /> /// <reference types="@ragempcommunity/types-client" />
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<K extends RageFW_CefEvent> = Parameters< export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters<
RageFW_ICustomCefEvent[K] RageFW_ICustomBrowserEvent[K]
> >
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType< export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
RageFW_ICustomCefEvent[K] RageFW_ICustomBrowserEvent[K]
> >
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> = export type _CefEventHasArgs<
keyof RageFW_ICustomCefEvent extends never EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never
? false ? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined : Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
? false ? false
: true : true

View File

@ -1,5 +1,5 @@
packages: packages:
- "server" - "server"
- "client" - "client"
- "cef" - "browser"
- "shared-types" - "shared-types"

View File

@ -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<K extends RageFW_CefEvent> = Parameters< export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters<
RageFW_ICustomCefEvent[K] RageFW_ICustomBrowserEvent[K]
> >
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType< export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
RageFW_ICustomCefEvent[K] RageFW_ICustomBrowserEvent[K]
> >
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> = export type _CefEventHasArgs<
keyof RageFW_ICustomCefEvent extends never EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never
? false ? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined : Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
? false ? false
: true : true

View File

@ -1,9 +1,14 @@
declare module '@entityseven/rage-fw-shared-types' { 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 { export interface RageFW_ICustomClientEvent {
cefReady(): void cefReady(): void
customClientEvent(arg1: string, arg2: number): boolean
} }
export interface RageFW_ICustomCefEvent {} export interface RageFW_ICustomBrowserEvent {
customCefEvent(arg1: string, arg2: number): boolean
}
} }