- typed server inner rpc
- client minor fixes
- sharing browser with rpc on client
This commit is contained in:
Danya H 2024-10-28 17:30:28 +00:00
parent 3bbf1e2209
commit cbbf4f38e6
12 changed files with 98 additions and 160 deletions

View File

@ -40,7 +40,7 @@ export class Browser extends Helper {
public unregister<EventName extends T.RageFW_BrowserEvent>( public unregister<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName, eventName: EventName,
): Browser { ): Browser {
rpc.unregister(eventName) rpc.unregister<EventName>(eventName)
return this return this
} }

View File

@ -18,7 +18,7 @@ export class Client {
public unregister<EventName extends T.RageFW_ClientEvent>( public unregister<EventName extends T.RageFW_ClientEvent>(
eventName: EventName, eventName: EventName,
): Client { ): Client {
rpc.unregister(eventName) rpc.unregister<EventName>(eventName)
return this return this
} }

View File

@ -2,47 +2,53 @@ import { rpc } from './rpc'
import type * as T from '../types' import type * as T from '../types'
export class Player { export class Player {
public browser: BrowserMp | undefined private _browser: BrowserMp | undefined = undefined
public trigger<EventName extends keyof T.RageFW_ICustomClientEvent>( set browser(browser: BrowserMp) {
this._browser = browser
rpc.browser = browser
}
public async trigger<EventName extends keyof T.RageFW_ICustomClientEvent>(
eventName: EventName, eventName: EventName,
...args: T._ClientEventHasArgs<EventName> extends true ...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>] ? [T.RageFW_ClientArgs<EventName>]
: [] : []
): Promise<T.RageFW_ClientReturn<EventName>> { ): Promise<T.RageFW_ClientReturn<EventName>> {
return rpc.call< return await rpc.call<
typeof args, typeof args,
EventName, EventName,
T.RageFW_ClientReturn<EventName> T.RageFW_ClientReturn<EventName>
>(eventName, args) >(eventName, args)
} }
public triggerServer<EventName extends T.RageFW_ServerEvent>( public async triggerServer<EventName extends T.RageFW_ServerEvent>(
eventName: EventName, eventName: EventName,
...args: T._ServerEventHasArgs<EventName> extends true ...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>] ? [T.RageFW_ServerArgs<EventName>]
: [] : []
): Promise<T.RageFW_ClientServerReturn<EventName>> { ): Promise<T.RageFW_ClientServerReturn<EventName>> {
return rpc.callServer< return await rpc.callServer<
typeof args, typeof args,
EventName, EventName,
T.RageFW_ClientServerReturn<EventName> T.RageFW_ClientServerReturn<EventName>
>(eventName, args) >(eventName, args)
} }
public triggerBrowser<EventName extends T.RageFW_CefEvent>( public async triggerBrowser<EventName extends T.RageFW_CefEvent>(
eventName: EventName, eventName: EventName,
...args: T._CefEventHasArgs<EventName> extends true ...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>] ? [T.RageFW_CefArgs<EventName>]
: [] : []
): Promise<T.RageFW_CefReturn<EventName>> { ): Promise<T.RageFW_CefReturn<EventName>> {
if (!this.browser) if (!this._browser)
throw new Error('You need to initialize browser first') throw new Error('You need to initialize browser first')
return rpc.callBrowser<
return await rpc.callBrowser<
typeof args, typeof args,
EventName, EventName,
T.RageFW_CefReturn<EventName> T.RageFW_CefReturn<EventName>
>(this.browser, eventName, args) >(eventName, args)
} }
} }

View File

@ -25,7 +25,7 @@ export type RageFW_ClientArgs<K extends RageFW_ClientEvent> =
/** /**
* Return type for an event, name of which you pass as a generic * Return type for an event, name of which you pass as a generic
* These only include custom events * These include custom and system events
*/ */
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> = export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent K extends keyof RageFW_ICustomClientEvent
@ -36,7 +36,7 @@ export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
/** /**
* Callback (function) for an event, name of which you pass as a generic * Callback (function) for an event, name of which you pass as a generic
* These only include custom events * These include custom and system events
*/ */
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = ( export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
...args: RageFW_ClientArgs<K> ...args: RageFW_ClientArgs<K>

View File

@ -1,3 +1,4 @@
export * from './player'
export * from './server'
export * from './logger' export * from './logger'
export * from './player'
export * from './rpc'
export * from './server'

View File

@ -1,31 +1,27 @@
import { Rpc } from '@entityseven/rage-fw-rpc' import { rpc } from './rpc'
import type * as T from '../types' import type * as T from '../types'
export class Player { export class Player {
private _rpc: Rpc = new Rpc() public async triggerClient<EventName extends T.RageFW_ClientEvent>(
get rpc(): Rpc {
return this._rpc
}
public triggerClient<EventName extends T.RageFW_ClientEvent>(
player: PlayerMp, player: PlayerMp,
eventName: EventName, eventName: EventName,
...args: T._ClientEventHasArgs<EventName> extends true ...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ServerClientArgs<EventName>] ? [T.RageFW_ClientArgs<EventName>]
: [] : []
): Promise<T.RageFW_ServerClientReturn<EventName>> { ): Promise<T.RageFW_ClientReturn<EventName>> {
return this._rpc.callClient(player, eventName, args) return await rpc.callClient(player, eventName, args)
} }
public triggerBrowser<EventName extends T.RageFW_CefEvent>( public async triggerBrowser<EventName extends T.RageFW_BrowserEvent>(
player: PlayerMp, player: PlayerMp,
eventName: EventName, eventName: EventName,
...args: T._CefEventHasArgs<EventName> extends true ...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>] ? [T.RageFW_BrowserArgs<EventName>]
: [] : []
): Promise<T.RageFW_CefReturn<EventName>> { ): Promise<T.RageFW_BrowserReturn<EventName>> {
return this._rpc.callBrowser(player, eventName, args) return await rpc.callBrowser(player, eventName, args)
} }
} }
// new Player().triggerBrowser({} as PlayerMp, 'customCefEvent', ['', 1])
// new Player().triggerClient({} as PlayerMp, 'customClientEvent', ['', 1])

5
server/src/core/rpc.ts Normal file
View File

@ -0,0 +1,5 @@
import { Rpc } from '@entityseven/rage-fw-rpc'
export const rpc = new Rpc({
debugLogs: false,
})

View File

@ -1,109 +1,39 @@
import { Rpc } from '@entityseven/rage-fw-rpc' import { rpc } from './rpc'
import { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types'
import { nativeEvents } from '../data/nativeEvents'
import type * as T from '../types' import type * as T from '../types'
export class Server { export class Server {
private _rpc: Rpc = new Rpc()
get rpc(): Rpc {
return this._rpc
}
private isNativeEvent(eventName: string): eventName is keyof IServerEvents {
return nativeEvents.includes(eventName)
}
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
callback: T.RageFW_ServerCallbackCustom<EventName>,
): void {
this._rpc.register(
eventName,
// fixme
async (args: T.RageFW_ServerArgs<EventName>, info) => {
await callback([info.player as PlayerMp, ...args])
},
)
}
private registerNative<EventName extends keyof IServerEvents>(
eventName: EventName,
callback: T.RageFW_ServerCallbackNative<EventName>,
): void {
mp.events.add(
eventName,
(...args: Parameters<IServerEvents[EventName]>) =>
callback([...args]),
)
}
public register<EventName extends T.RageFW_ServerEvent>( public register<EventName extends T.RageFW_ServerEvent>(
eventName: EventName, eventName: EventName,
callback: T.RageFW_ServerCallback<EventName>, callback: T.RageFW_ServerCallback<EventName>,
): void { ): Server {
if (this.isNativeEvent(eventName)) { rpc.register<
this.registerNative( Parameters<typeof callback>,
eventName, ReturnType<typeof callback>,
callback as T.RageFW_ServerCallbackNative, EventName
) >(eventName, async (...data) => await callback(...data))
} else {
this.registerCustom(
eventName,
callback as unknown as T.RageFW_ServerCallbackCustom,
)
}
}
public registerMany<EventName extends T.RageFW_ServerEvent>(events: { return this
[event in EventName]: T.RageFW_ServerCallback<event>
}): void {
Object.entries<T.RageFW_ServerCallback<EventName>>(events).map(
([eventName, callback]) => {
if (this.isNativeEvent(eventName)) {
this.registerNative(
eventName,
callback as T.RageFW_ServerCallbackNative,
)
} else {
this.registerCustom(
eventName as keyof RageFW_ICustomServerEvent,
callback as unknown as T.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 T.RageFW_ServerEvent>( public unregister<EventName extends T.RageFW_ServerEvent>(
eventName: EventName, eventName: EventName,
): void { ): Server {
if (this.isNativeEvent(eventName)) { rpc.unregister<EventName>(eventName)
this.unregisterNative(eventName)
} else { return this
this.unregisterCustom(eventName)
}
} }
public trigger<EventName extends keyof RageFW_ICustomServerEvent>( // fixme
eventName: EventName, // public trigger<EventName extends keyof T.RageFW_ICustomServerEvent>(
...args: T._ServerEventHasArgs<EventName> extends true // eventName: EventName,
? [T.RageFW_ServerArgs<EventName>] // ...args: T._ServerEventHasArgs<EventName> extends true
: [] // ? [T.RageFW_ServerArgs<EventName>]
): Promise<T.RageFW_ServerReturn<EventName>> { // : []
return this._rpc.call(eventName, args) // ): Promise<T.RageFW_ServerReturn<EventName>> {
} // return rpc.call(eventName, args)
// }
} }
// new Server()
// .register('customServerEvent', async (a, b, c) => true)
// .unregister('customServerEvent')

View File

@ -1,4 +1,4 @@
import { Logger, Player, Server } from './core' import { Logger, Player, Server, rpc } from './core'
export const fw = { export const fw = {
event: new Server(), event: new Server(),
@ -6,6 +6,7 @@ export const fw = {
system: { system: {
log: new Logger(), log: new Logger(),
}, },
rpc,
} }
fw.system.log.info( fw.system.log.info(

View File

@ -1,15 +1,30 @@
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types' import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
export type RageFW_CefEvent = keyof RageFW_ICustomBrowserEvent /**
* Union of all available browser event names
* These only include custom events
*/
export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent
export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters< /**
* 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] RageFW_ICustomBrowserEvent[K]
> >
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType< /**
* 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] RageFW_ICustomBrowserEvent[K]
> >
/**
*
*/
export type _CefEventHasArgs< export type _CefEventHasArgs<
EventName extends keyof RageFW_ICustomBrowserEvent, EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never > = keyof RageFW_ICustomBrowserEvent extends never

View File

@ -12,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent
* Array of arguments of an event you pass as a generic * Array of arguments of an event you pass as a generic
* These only include custom events * These only include custom events
*/ */
export type RageFW_ServerClientArgs<K extends RageFW_ClientEvent> = export type RageFW_ClientArgs<K extends RageFW_ClientEvent> =
K extends RageFW_ClientEvent K extends RageFW_ClientEvent
? Parameters<RageFW_ICustomClientEvent[K]> ? Parameters<RageFW_ICustomClientEvent[K]>
: never : never
@ -21,11 +21,14 @@ export type RageFW_ServerClientArgs<K extends RageFW_ClientEvent> =
* Return type of event you pass as a generic * Return type of event you pass as a generic
* These only include custom events * These only include custom events
*/ */
export type RageFW_ServerClientReturn<K extends RageFW_ClientEvent> = export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
K extends RageFW_ClientEvent K extends RageFW_ClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]> ? ReturnType<RageFW_ICustomClientEvent[K]>
: never : never
/**
*
*/
export type _ClientEventHasArgs< export type _ClientEventHasArgs<
EventName extends keyof RageFW_ICustomClientEvent, EventName extends keyof RageFW_ICustomClientEvent,
> = keyof RageFW_ICustomClientEvent extends never > = keyof RageFW_ICustomClientEvent extends never

View File

@ -4,6 +4,7 @@ import type {
RageFW_ICustomClientEvent, RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent, RageFW_ICustomServerEvent,
} from '@entityseven/rage-fw-shared-types' } from '@entityseven/rage-fw-shared-types'
export type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types' export type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types'
/** /**
@ -20,20 +21,9 @@ export type RageFW_ServerEvent =
*/ */
export type RageFW_ServerArgs<K extends RageFW_ServerEvent> = export type RageFW_ServerArgs<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent K extends keyof RageFW_ICustomServerEvent
? Parameters<RageFW_ICustomServerEvent[K]> ? [PlayerMp, ...Parameters<RageFW_ICustomServerEvent[K]>]
: K extends keyof IServerEvents : K extends keyof IServerEvents
? Parameters<IServerEvents[K]> ? [PlayerMp, 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 : never
/** /**
@ -45,28 +35,19 @@ export type RageFW_ServerReturn<K extends RageFW_ServerEvent> =
? ReturnType<RageFW_ICustomServerEvent[K]> ? ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents : K extends keyof IServerEvents
? ReturnType<IServerEvents[K]> ? ReturnType<IServerEvents[K]>
: never : void
/** /**
* Array of arguments for an event, name of which you pass as a generic * Callback (function) for an event, name of which you pass as a generic
* These only include custom events * These include system and custom events
*/ */
export type RageFW_ServerCallbackCustom< export type RageFW_ServerCallback<K extends RageFW_ServerEvent> = (
K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, ...args: RageFW_ServerArgs<K>
> = (
payload: [player: PlayerMp, ...args: RageFW_ServerArgs<K>],
) => Promise<RageFW_ServerReturn<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> = export type _ServerEventHasArgs<EventName extends RageFW_ServerEvent> =
EventName extends keyof RageFW_ICustomServerEvent EventName extends keyof RageFW_ICustomServerEvent
? keyof RageFW_ICustomClientEvent extends never ? keyof RageFW_ICustomClientEvent extends never