import rpc from 'rage-rpc' import Logger from './logger' import { _CefEventHasArgs, _ClientEventHasArgs, _ServerEventHasArgs, RageFW_CefArgs, RageFW_CefEvent, RageFW_CefReturn, RageFW_ClientEvent, RageFW_ClientEventArguments, RageFW_ClientEventCallback, RageFW_ClientEventReturn, RageFW_ClientServerEvent, RageFW_ClientServerEventArguments, RageFW_ClientServerEventReturn, } from './types' import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' class Client { public register( eventName: EventName, callback: RageFW_ClientEventCallback, ): void { rpc.register(eventName, data => { return callback(data) }) } public unregister( eventName: EventName, ): void { rpc.unregister(eventName) } } class Player { public browser: BrowserMp | undefined public trigger( eventName: EventName, ...args: _ClientEventHasArgs extends true ? [RageFW_ClientEventArguments] : [] ): Promise> { return rpc.call>(eventName, args) } public triggerServer( eventName: EventName, ...args: _ServerEventHasArgs extends true ? [RageFW_ClientServerEventArguments] : [] ): Promise> { return rpc.callServer(eventName, args) } public triggerBrowser( eventName: EventName, ...args: _CefEventHasArgs extends true ? [RageFW_CefArgs] : [] ): Promise> { if (!this.browser) throw new Error('You need to initialize browser first!') return rpc.callBrowser(this.browser, eventName, args) } } class Browser extends Player { public registerBrowser(browser: BrowserMp) { this.browser = browser return browser } } export const fw = { event: new Client(), player: new Player(), browser: new Browser(), system: { log: new Logger(), }, }