Consistency updates + cleanup #5
@ -1,29 +1,29 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
|
||||
export class Client {
|
||||
private _rpc: Rpc = new Rpc()
|
||||
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
|
||||
public register<EventName extends T.RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
callback: T.RageFW_ClientCallback<EventName>,
|
||||
): void {
|
||||
this._rpc.register(
|
||||
eventName,
|
||||
async (data: T.RageFW_ClientArgs<EventName>) => {
|
||||
return await callback(data)
|
||||
},
|
||||
)
|
||||
): Client {
|
||||
rpc.register<
|
||||
Parameters<typeof callback>,
|
||||
ReturnType<typeof callback>,
|
||||
EventName
|
||||
>(eventName, async (...data) => await callback(...data))
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public unregister<EventName extends T.RageFW_ClientEvent>(
|
||||
eventName: EventName,
|
||||
): void {
|
||||
this._rpc.unregister(eventName)
|
||||
): Client {
|
||||
rpc.unregister(eventName)
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
// new Client()
|
||||
// .register('customClientEvent', async (a, b) => true)
|
||||
// .unregister('customClientEvent')
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from './client'
|
||||
export * from './player'
|
||||
export * from './logger'
|
||||
export * from './player'
|
||||
export * from './rpc'
|
||||
|
@ -1,31 +1,33 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types'
|
||||
|
||||
import { rpc } from './rpc'
|
||||
import type * as T from '../types'
|
||||
|
||||
export class Player {
|
||||
private _rpc: Rpc = new Rpc()
|
||||
public browser: BrowserMp | undefined
|
||||
|
||||
get rpc(): Rpc {
|
||||
return this._rpc
|
||||
}
|
||||
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
|
||||
public trigger<EventName extends keyof T.RageFW_ICustomClientEvent>(
|
||||
eventName: EventName,
|
||||
...args: T._ClientEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_ClientArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_ClientReturn<EventName>> {
|
||||
return this._rpc.call(eventName, args)
|
||||
return rpc.call<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_ClientReturn<EventName>
|
||||
>(eventName, args)
|
||||
}
|
||||
|
||||
public triggerServer<EventName extends T.RageFW_ClientServerEvent>(
|
||||
public triggerServer<EventName extends T.RageFW_ServerEvent>(
|
||||
eventName: EventName,
|
||||
...args: T._ServerEventHasArgs<EventName> extends true
|
||||
? [T.RageFW_ClientServerArgs<EventName>]
|
||||
? [T.RageFW_ServerArgs<EventName>]
|
||||
: []
|
||||
): Promise<T.RageFW_ClientServerReturn<EventName>> {
|
||||
return this._rpc.callServer(eventName, args)
|
||||
return rpc.callServer<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_ClientServerReturn<EventName>
|
||||
>(eventName, args)
|
||||
}
|
||||
|
||||
public triggerBrowser<EventName extends T.RageFW_CefEvent>(
|
||||
@ -36,6 +38,14 @@ export class Player {
|
||||
): Promise<T.RageFW_CefReturn<EventName>> {
|
||||
if (!this.browser)
|
||||
throw new Error('You need to initialize browser first')
|
||||
return this._rpc.callBrowser(this.browser, eventName, args)
|
||||
return rpc.callBrowser<
|
||||
typeof args,
|
||||
EventName,
|
||||
T.RageFW_CefReturn<EventName>
|
||||
>(this.browser, eventName, args)
|
||||
}
|
||||
}
|
||||
|
||||
// new Player().trigger('customClientEvent', ['', 1])
|
||||
// new Player().triggerServer('customServerEvent', ['', 1])
|
||||
// new Player().triggerBrowser('customCefEvent', ['', 1])
|
||||
|
5
client/src/core/rpc.ts
Normal file
5
client/src/core/rpc.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { Rpc } from '@entityseven/rage-fw-rpc'
|
||||
|
||||
export const rpc = new Rpc({
|
||||
debugLogs: false,
|
||||
})
|
@ -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,
|
||||
}
|
||||
|
@ -2,8 +2,16 @@
|
||||
|
||||
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
|
||||
|
||||
/**
|
||||
* Union of all available browser event names callable from client
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent
|
||||
|
||||
/**
|
||||
* Array of arguments for an event, name of which you pass as a generic
|
||||
* These only include custom events
|
||||
*/
|
||||
export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters<
|
||||
RageFW_ICustomBrowserEvent[K]
|
||||
>
|
||||
|
@ -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,14 +23,6 @@ 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 only include custom events
|
||||
@ -36,8 +30,21 @@ export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
|
||||
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
|
||||
K extends keyof RageFW_ICustomClientEvent
|
||||
? ReturnType<RageFW_ICustomClientEvent[K]>
|
||||
: void
|
||||
: K extends keyof IClientEvents
|
||||
? ReturnType<IClientEvents[K]>
|
||||
: void
|
||||
|
||||
/**
|
||||
* 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>>
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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_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<K extends RageFW_ClientServerEvent> =
|
||||
export type RageFW_ServerArgs<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? Parameters<RageFW_ICustomServerEvent[K]>
|
||||
: never
|
||||
@ -24,7 +24,7 @@ export type RageFW_ClientServerArgs<K extends RageFW_ClientServerEvent> =
|
||||
* 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_ClientServerEvent> =
|
||||
export type RageFW_ClientServerReturn<K extends RageFW_ServerEvent> =
|
||||
K extends keyof RageFW_ICustomServerEvent
|
||||
? ReturnType<RageFW_ICustomServerEvent[K]>
|
||||
: never
|
||||
|
Loading…
Reference in New Issue
Block a user