Compare commits
No commits in common. "d56475d08a1c857e2aa8e89f8d53c520486687fd" and "a675114b41ea25b631f5c04f3a81dc20e766a78b" have entirely different histories.
d56475d08a
...
a675114b41
@ -1,96 +0,0 @@
|
||||
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>(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])
|
@ -1,21 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export * from './browser'
|
||||
export * from './helper'
|
||||
export * from './rpc'
|
@ -1,7 +0,0 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
export const rpc = new Rpc({
|
||||
forceBrowserDevMode:
|
||||
process.env.RageFW_forceBrowserDevMode === 'true' ?? false,
|
||||
debugLogs: false,
|
||||
})
|
@ -1,9 +0,0 @@
|
||||
import { Browser, rpc } from './core'
|
||||
|
||||
export const fw = {
|
||||
event: new Browser(),
|
||||
rpc,
|
||||
}
|
||||
;(async () => {
|
||||
await fw.event.triggerClient('cefReady')
|
||||
})()
|
@ -1,40 +0,0 @@
|
||||
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
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@entityseven/rage-fw-browser",
|
||||
"version": "0.1.2",
|
||||
"name": "@entityseven/rage-fw-cef",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"files": [
|
113
cef/src/index.ts
Normal file
113
cef/src/index.ts
Normal file
@ -0,0 +1,113 @@
|
||||
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 extends keyof RageFW_ICustomCefEvent>(
|
||||
eventName: EventName,
|
||||
callback: 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 RageFW_ICustomCefEvent>(
|
||||
eventName: EventName,
|
||||
...args: _CefEventHasArgs<EventName> extends true
|
||||
? [RageFW_CefArgs<EventName>]
|
||||
: []
|
||||
): Promise<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 RageFW_ICustomServerEvent,
|
||||
>(
|
||||
eventName: EventName,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerArgs<EventName>]
|
||||
: []
|
||||
): Promise<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 RageFW_ICustomClientEvent,
|
||||
>(
|
||||
eventName: EventName,
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ClientArgs<EventName>]
|
||||
: []
|
||||
): Promise<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')
|
||||
})()
|
40
cef/src/types/cef.ts
Normal file
40
cef/src/types/cef.ts
Normal file
@ -0,0 +1,40 @@
|
||||
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<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
|
@ -1,3 +1,3 @@
|
||||
export * from './browser'
|
||||
export * from './cef'
|
||||
export * from './client'
|
||||
export * from './server'
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@entityseven/rage-fw-client",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"files": [
|
||||
|
@ -1,29 +1,33 @@
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
import type {
|
||||
RageFW_ClientArgs,
|
||||
RageFW_ClientCallback,
|
||||
RageFW_ClientEvent,
|
||||
} from '../types'
|
||||
|
||||
export class Client {
|
||||
public register<EventName extends T.RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
callback: T.RageFW_ClientCallback<EventName>,
|
||||
): Client {
|
||||
rpc.register<
|
||||
Parameters<typeof callback>,
|
||||
ReturnType<typeof callback>,
|
||||
EventName
|
||||
>(eventName, async (...data) => await callback(...data))
|
||||
private _rpc: Rpc = new Rpc()
|
||||
|
||||
return this
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
|
||||
public unregister<EventName extends T.RageFW_ClientEvent>(
|
||||
public register<EventName extends RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
): Client {
|
||||
rpc.unregister<EventName>(eventName)
|
||||
callback: RageFW_ClientCallback<EventName>,
|
||||
): void {
|
||||
this._rpc.register(
|
||||
eventName,
|
||||
async (data: RageFW_ClientArgs<EventName>) => {
|
||||
return await callback(data)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
return this
|
||||
public unregister<EventName extends RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
this._rpc.unregister(eventName)
|
||||
}
|
||||
}
|
||||
|
||||
// new Client()
|
||||
// .register('customClientEvent', async (a, b) => true)
|
||||
// .unregister('customClientEvent')
|
||||
|
@ -1,4 +1,3 @@
|
||||
export * from './client'
|
||||
export * from './logger'
|
||||
export * from './player'
|
||||
export * from './rpc'
|
||||
export * from './logger'
|
||||
|
@ -1,57 +1,53 @@
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
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'
|
||||
|
||||
export class Player {
|
||||
private _browser: BrowserMp | undefined = undefined
|
||||
private _rpc: Rpc = new Rpc()
|
||||
public browser: BrowserMp | undefined
|
||||
|
||||
set browser(browser: BrowserMp) {
|
||||
this._browser = browser
|
||||
rpc.browser = browser
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
|
||||
eventName: EventName,
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ClientArgs<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ClientReturn<EventName>> {
|
||||
return this._rpc.call(eventName, args)
|
||||
}
|
||||
|
||||
public async trigger<EventName extends keyof T.RageFW_ICustomClientEvent>(
|
||||
public triggerServer<EventName extends RageFW_ClientServerEvent>(
|
||||
eventName: EventName,
|
||||
...args: T._ClientEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_ClientArgs<EventName>]
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ClientServerArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_ClientReturn<EventName>> {
|
||||
return await rpc.call<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_ClientReturn<EventName>
|
||||
>(eventName, args)
|
||||
): Promise<RageFW_ClientServerReturn<EventName>> {
|
||||
return this._rpc.callServer(eventName, args)
|
||||
}
|
||||
|
||||
public async triggerServer<EventName extends T.RageFW_ServerEvent>(
|
||||
public triggerBrowser<EventName extends RageFW_CefEvent>(
|
||||
eventName: EventName,
|
||||
...args: T._ServerEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_ServerArgs<EventName>]
|
||||
...args: _CefEventHasArgs<EventName> extends true
|
||||
? [RageFW_CefArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_ClientServerReturn<EventName>> {
|
||||
return await rpc.callServer<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_ClientServerReturn<EventName>
|
||||
>(eventName, args)
|
||||
}
|
||||
|
||||
public async triggerBrowser<EventName extends T.RageFW_BrowserEvent>(
|
||||
eventName: EventName,
|
||||
...args: T._BrowserEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_BrowserArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_BrowserReturn<EventName>> {
|
||||
if (!this._browser)
|
||||
): Promise<RageFW_CefReturn<EventName>> {
|
||||
if (!this.browser)
|
||||
throw new Error('You need to initialize browser first')
|
||||
|
||||
return await rpc.callBrowser<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_BrowserReturn<EventName>
|
||||
>(eventName, args)
|
||||
return this._rpc.callBrowser(this.browser, eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
// new Player().trigger('customClientEvent', ['', 1])
|
||||
// new Player().triggerServer('customServerEvent', ['', 1])
|
||||
// new Player().triggerBrowser('customCefEvent', ['', 1])
|
||||
|
@ -1,5 +0,0 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
export const rpc = new Rpc({
|
||||
debugLogs: false,
|
||||
})
|
@ -1,4 +1,4 @@
|
||||
import { Client, Logger, Player, rpc } from './core'
|
||||
import { Client, Logger, Player } from './core'
|
||||
|
||||
export const fw = {
|
||||
event: new Client(),
|
||||
@ -6,5 +6,4 @@ export const fw = {
|
||||
system: {
|
||||
log: new Logger(),
|
||||
},
|
||||
rpc,
|
||||
}
|
||||
|
@ -1,29 +1,20 @@
|
||||
/// <reference types="@ragempcommunity/types-client" />
|
||||
|
||||
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
|
||||
import type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types'
|
||||
|
||||
/**
|
||||
* 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_CefEvent = keyof RageFW_ICustomCefEvent
|
||||
|
||||
/**
|
||||
* Array of arguments for an event, name of which you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_BrowserArgs<K extends RageFW_BrowserEvent> = Parameters<
|
||||
RageFW_ICustomBrowserEvent[K]
|
||||
export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters<
|
||||
RageFW_ICustomCefEvent[K]
|
||||
>
|
||||
|
||||
export type RageFW_BrowserReturn<K extends RageFW_BrowserEvent> = ReturnType<
|
||||
RageFW_ICustomBrowserEvent[K]
|
||||
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
|
||||
RageFW_ICustomCefEvent[K]
|
||||
>
|
||||
|
||||
export type _BrowserEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomBrowserEvent,
|
||||
> = keyof RageFW_ICustomBrowserEvent extends never
|
||||
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
|
||||
keyof RageFW_ICustomCefEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
|
||||
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
|
@ -2,8 +2,6 @@
|
||||
|
||||
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
|
||||
@ -23,28 +21,23 @@ export type RageFW_ClientArgs<K extends RageFW_ClientEvent> =
|
||||
? Parameters<IClientEvents[K]>
|
||||
: never
|
||||
|
||||
/**
|
||||
* Callback (function) for an event, name of which you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
|
||||
args: RageFW_ClientArgs<K>,
|
||||
) => Promise<RageFW_ClientReturn<K>>
|
||||
|
||||
/**
|
||||
* Return type for an event, name of which you pass as a generic
|
||||
* These include custom and system events
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
|
||||
K extends keyof RageFW_ICustomClientEvent
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: K extends keyof IClientEvents
|
||||
? ReturnType<IClientEvents[K]>
|
||||
: void
|
||||
|
||||
/**
|
||||
* Callback (function) for an event, name of which you pass as a generic
|
||||
* These include custom and system events
|
||||
*/
|
||||
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
|
||||
...args: RageFW_ClientArgs<K>
|
||||
) => Promise<RageFW_ClientReturn<K>>
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export type _ClientEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomClientEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
|
@ -9,13 +9,13 @@ import type {
|
||||
* Union of all available server event names callable from client
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ServerEvent = keyof RageFW_ICustomServerEvent
|
||||
export type RageFW_ClientServerEvent = 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_ServerArgs<K extends RageFW_ServerEvent> =
|
||||
export type RageFW_ClientServerArgs<K extends RageFW_ClientServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? Parameters<RageFW_ICustomServerEvent[K]>
|
||||
: never
|
||||
@ -24,7 +24,7 @@ export type RageFW_ServerArgs<K extends RageFW_ServerEvent> =
|
||||
* Return type for an event, name of which you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ClientServerReturn<K extends RageFW_ServerEvent> =
|
||||
export type RageFW_ClientServerReturn<K extends RageFW_ClientServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
: never
|
||||
|
@ -1,5 +1,5 @@
|
||||
packages:
|
||||
- "server"
|
||||
- "client"
|
||||
- "browser"
|
||||
- "cef"
|
||||
- "shared-types"
|
@ -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 @entityseven/rage-fw``
|
||||
``pnpm create 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
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@entityseven/rage-fw-server",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/src/index.d.ts",
|
||||
"files": [
|
||||
|
@ -1,4 +1,3 @@
|
||||
export * from './logger'
|
||||
export * from './player'
|
||||
export * from './rpc'
|
||||
export * from './server'
|
||||
export * from './logger'
|
||||
|
@ -1,27 +1,40 @@
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
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'
|
||||
|
||||
export class Player {
|
||||
public async triggerClient<EventName extends T.RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: T._ClientEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_ClientArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_ClientReturn<EventName>> {
|
||||
return await rpc.callClient(player, eventName, args)
|
||||
private _rpc: Rpc = new Rpc()
|
||||
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
|
||||
public async triggerBrowser<EventName extends T.RageFW_BrowserEvent>(
|
||||
public triggerClient<EventName extends RageFW_ClientEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: T._BrowserEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_BrowserArgs<EventName>]
|
||||
...args: _ClientEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerClientArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_BrowserReturn<EventName>> {
|
||||
return await rpc.callBrowser(player, eventName, args)
|
||||
): Promise<RageFW_ServerClientReturn<EventName>> {
|
||||
return this._rpc.callClient(player, eventName, args)
|
||||
}
|
||||
|
||||
public triggerBrowser<EventName extends RageFW_CefEvent>(
|
||||
player: PlayerMp,
|
||||
eventName: EventName,
|
||||
...args: _CefEventHasArgs<EventName> extends true
|
||||
? [RageFW_CefArgs<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_CefReturn<EventName>> {
|
||||
return this._rpc.callBrowser(player, eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
// new Player().triggerBrowser({} as PlayerMp, 'customCefEvent', ['', 1])
|
||||
// new Player().triggerClient({} as PlayerMp, 'customClientEvent', ['', 1])
|
||||
|
@ -1,5 +0,0 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
export const rpc = new Rpc({
|
||||
debugLogs: false,
|
||||
})
|
@ -1,39 +1,117 @@
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
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'
|
||||
|
||||
export class Server {
|
||||
public register<EventName extends T.RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: T.RageFW_ServerCallback<EventName>,
|
||||
): Server {
|
||||
rpc.register<
|
||||
Parameters<typeof callback>,
|
||||
ReturnType<typeof callback>,
|
||||
EventName
|
||||
>(eventName, async (...data) => await callback(...data))
|
||||
private _rpc: Rpc = new Rpc()
|
||||
|
||||
return this
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
|
||||
public unregister<EventName extends T.RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
): Server {
|
||||
rpc.unregister<EventName>(eventName)
|
||||
|
||||
return this
|
||||
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
|
||||
return nativeEvents.includes(eventName)
|
||||
}
|
||||
|
||||
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerCallbackCustom<EventName>,
|
||||
): void {
|
||||
this._rpc.register(
|
||||
eventName,
|
||||
// fixme
|
||||
// public trigger<EventName extends keyof T.RageFW_ICustomServerEvent>(
|
||||
// eventName: EventName,
|
||||
// ...args: T._ServerEventHasArgs<EventName> extends true
|
||||
// ? [T.RageFW_ServerArgs<EventName>]
|
||||
// : []
|
||||
// ): Promise<T.RageFW_ServerReturn<EventName>> {
|
||||
// return rpc.call(eventName, args)
|
||||
// }
|
||||
}
|
||||
async (args: RageFW_ServerArgs<EventName>, info) => {
|
||||
await callback([info.player as PlayerMp, ...args])
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// new Server()
|
||||
// .register('customServerEvent', async (a, b, c) => true)
|
||||
// .unregister('customServerEvent')
|
||||
private registerNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerCallbackNative<EventName>,
|
||||
): void {
|
||||
mp.events.add(
|
||||
eventName,
|
||||
(...args: Parameters<IServerEvents[EventName]>) =>
|
||||
callback([...args]),
|
||||
)
|
||||
}
|
||||
|
||||
public register<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
callback: RageFW_ServerCallback<EventName>,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.registerNative(
|
||||
eventName,
|
||||
callback as RageFW_ServerCallbackNative,
|
||||
)
|
||||
} else {
|
||||
this.registerCustom(
|
||||
eventName,
|
||||
callback as unknown as RageFW_ServerCallbackCustom,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public registerMany<EventName extends RageFW_ServerEvent>(events: {
|
||||
[event in EventName]: RageFW_ServerCallback<event>
|
||||
}): void {
|
||||
Object.entries<RageFW_ServerCallback<EventName>>(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 extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
this._rpc.unregister(eventName)
|
||||
}
|
||||
|
||||
private unregisterNative<EventName extends keyof IServerEvents>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
mp.events.remove(eventName)
|
||||
}
|
||||
|
||||
public unregister<EventName extends RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
if (this.isNativeEvent(eventName)) {
|
||||
this.unregisterNative(eventName)
|
||||
} else {
|
||||
this.unregisterCustom(eventName)
|
||||
}
|
||||
}
|
||||
|
||||
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
|
||||
eventName: EventName,
|
||||
...args: _ServerEventHasArgs<EventName> extends true
|
||||
? [RageFW_ServerArgs<EventName>]
|
||||
: []
|
||||
): Promise<RageFW_ServerReturn<EventName>> {
|
||||
return this._rpc.call(eventName, args)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Logger, Player, Server, rpc } from './core'
|
||||
import { Logger, Player, Server } from './core'
|
||||
|
||||
export const fw = {
|
||||
event: new Server(),
|
||||
@ -6,7 +6,6 @@ export const fw = {
|
||||
system: {
|
||||
log: new Logger(),
|
||||
},
|
||||
rpc,
|
||||
}
|
||||
|
||||
fw.system.log.info(
|
||||
|
@ -1,34 +1,18 @@
|
||||
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
|
||||
import type { RageFW_ICustomCefEvent } 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
|
||||
export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent
|
||||
|
||||
/**
|
||||
* Array of arguments of an event you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_BrowserArgs<K extends RageFW_BrowserEvent> = Parameters<
|
||||
RageFW_ICustomBrowserEvent[K]
|
||||
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 events
|
||||
*/
|
||||
export type RageFW_BrowserReturn<K extends RageFW_BrowserEvent> = ReturnType<
|
||||
RageFW_ICustomBrowserEvent[K]
|
||||
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
|
||||
RageFW_ICustomCefEvent[K]
|
||||
>
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export type _BrowserEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomBrowserEvent,
|
||||
> = keyof RageFW_ICustomBrowserEvent extends never
|
||||
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
|
||||
keyof RageFW_ICustomCefEvent extends never
|
||||
? false
|
||||
: Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
|
||||
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
|
||||
? false
|
||||
: true
|
||||
|
@ -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_ClientArgs<K extends RageFW_ClientEvent> =
|
||||
export type RageFW_ServerClientArgs<K extends RageFW_ClientEvent> =
|
||||
K extends RageFW_ClientEvent
|
||||
? Parameters<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
@ -21,14 +21,11 @@ export type RageFW_ClientArgs<K extends RageFW_ClientEvent> =
|
||||
* Return type of event you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
|
||||
export type RageFW_ServerClientReturn<K extends RageFW_ClientEvent> =
|
||||
K extends RageFW_ClientEvent
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: never
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
export type _ClientEventHasArgs<
|
||||
EventName extends keyof RageFW_ICustomClientEvent,
|
||||
> = keyof RageFW_ICustomClientEvent extends never
|
||||
|
@ -4,7 +4,6 @@ import type {
|
||||
RageFW_ICustomClientEvent,
|
||||
RageFW_ICustomServerEvent,
|
||||
} from '@entityseven/rage-fw-shared-types'
|
||||
|
||||
export type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types'
|
||||
|
||||
/**
|
||||
@ -21,9 +20,20 @@ export type RageFW_ServerEvent =
|
||||
*/
|
||||
export type RageFW_ServerArgs<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? [PlayerMp, ...Parameters<RageFW_ICustomServerEvent[K]>]
|
||||
? Parameters<RageFW_ICustomServerEvent[K]>
|
||||
: K extends keyof IServerEvents
|
||||
? [PlayerMp, Parameters<IServerEvents[K]>]
|
||||
? Parameters<IServerEvents[K]>
|
||||
: 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 RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? RageFW_ServerCallbackCustom<K>
|
||||
: K extends keyof IServerEvents
|
||||
? RageFW_ServerCallbackNative<K>
|
||||
: never
|
||||
|
||||
/**
|
||||
@ -35,19 +45,28 @@ export type RageFW_ServerReturn<K extends RageFW_ServerEvent> =
|
||||
? ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
: K extends keyof IServerEvents
|
||||
? ReturnType<IServerEvents[K]>
|
||||
: void
|
||||
: never
|
||||
|
||||
/**
|
||||
* Callback (function) for an event, name of which you pass as a generic
|
||||
* These include system and custom events
|
||||
* Array of arguments for an event, name of which you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_ServerCallback<K extends RageFW_ServerEvent> = (
|
||||
...args: RageFW_ServerArgs<K>
|
||||
export type RageFW_ServerCallbackCustom<
|
||||
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent,
|
||||
> = (
|
||||
payload: [player: PlayerMp, ...args: RageFW_ServerArgs<K>],
|
||||
) => Promise<RageFW_ServerReturn<K>>
|
||||
|
||||
/**
|
||||
*
|
||||
* 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<IServerEvents[K]>,
|
||||
) => Promise<ReturnType<IServerEvents[K]>>
|
||||
|
||||
export type _ServerEventHasArgs<EventName extends RageFW_ServerEvent> =
|
||||
EventName extends keyof RageFW_ICustomServerEvent
|
||||
? keyof RageFW_ICustomClientEvent extends never
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@entityseven/rage-fw-shared-types",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.0",
|
||||
"types": "types/types/index.d.ts",
|
||||
"files": [
|
||||
"types/**/*"
|
||||
|
9
shared-types/types/types/index.d.ts
vendored
9
shared-types/types/types/index.d.ts
vendored
@ -1,14 +1,9 @@
|
||||
declare module '@entityseven/rage-fw-shared-types' {
|
||||
export interface RageFW_ICustomServerEvent {
|
||||
customServerEvent(arg1: string, arg2: number): boolean
|
||||
}
|
||||
export interface RageFW_ICustomServerEvent {}
|
||||
|
||||
export interface RageFW_ICustomClientEvent {
|
||||
cefReady(): void
|
||||
customClientEvent(arg1: string, arg2: number): boolean
|
||||
}
|
||||
|
||||
export interface RageFW_ICustomBrowserEvent {
|
||||
customCefEvent(arg1: string, arg2: number): boolean
|
||||
}
|
||||
export interface RageFW_ICustomCefEvent {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user