- types now use namespace to avoid bloating
- moved items for further usage into data folder
This commit is contained in:
Danya H 2024-10-28 15:20:43 +00:00
parent 3a4fe19df0
commit 588bb42d04
8 changed files with 61 additions and 109 deletions

View File

@ -1,20 +1,5 @@
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'
import type * as T from './types'
class Cef {
private _debugMode: boolean = false
@ -30,9 +15,9 @@ class Cef {
return this._rpc
}
public register<EventName extends keyof RageFW_ICustomCefEvent>(
public register<EventName extends keyof T.RageFW_ICustomCefEvent>(
eventName: EventName,
callback: RageFW_CefCallback<EventName>,
callback: T.RageFW_CefCallback<EventName>,
): void {
if (this._debugMode) {
console.log('[RPC](register):', eventName, callback)
@ -43,12 +28,12 @@ class Cef {
}
}
public async trigger<EventName extends keyof RageFW_ICustomCefEvent>(
public async trigger<EventName extends keyof T.RageFW_ICustomCefEvent>(
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
): Promise<T.RageFW_CefReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](trigger):', eventName, ...args)
}
@ -63,13 +48,13 @@ class Cef {
}
public async triggerServer<
EventName extends keyof RageFW_ICustomServerEvent,
EventName extends keyof T.RageFW_ICustomServerEvent,
>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerArgs<EventName>]
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<RageFW_ServerReturn<EventName>> {
): Promise<T.RageFW_ServerReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](triggerServer):', eventName, ...args)
}
@ -84,13 +69,13 @@ class Cef {
}
public async triggerClient<
EventName extends keyof RageFW_ICustomClientEvent,
EventName extends keyof T.RageFW_ICustomClientEvent,
>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
): Promise<T.RageFW_ClientReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](triggerClient):', eventName, ...args)
}

View File

@ -1,6 +1,6 @@
import { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types'
import type { RageFW_ICustomCefEvent } from '@entityseven/rage-fw-shared-types'
export { 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

View File

@ -1,10 +1,6 @@
import { Rpc } from '@entityseven/rage-fw-rpc'
import type {
RageFW_ClientArgs,
RageFW_ClientCallback,
RageFW_ClientEvent,
} from '../types'
import type * as T from '../types'
export class Client {
private _rpc: Rpc = new Rpc()
@ -13,19 +9,19 @@ export class Client {
return this._rpc
}
public register<EventName extends RageFW_ClientEvent>(
public register<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
callback: RageFW_ClientCallback<EventName>,
callback: T.RageFW_ClientCallback<EventName>,
): void {
this._rpc.register(
eventName,
async (data: RageFW_ClientArgs<EventName>) => {
async (data: T.RageFW_ClientArgs<EventName>) => {
return await callback(data)
},
)
}
public unregister<EventName extends RageFW_ClientEvent>(
public unregister<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
): void {
this._rpc.unregister(eventName)

View File

@ -1,19 +1,7 @@
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'
import type * as T from '../types'
export class Player {
private _rpc: Rpc = new Rpc()
@ -24,28 +12,28 @@ export class Player {
}
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
): Promise<T.RageFW_ClientReturn<EventName>> {
return this._rpc.call(eventName, args)
}
public triggerServer<EventName extends RageFW_ClientServerEvent>(
public triggerServer<EventName extends T.RageFW_ClientServerEvent>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ClientServerArgs<EventName>]
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ClientServerArgs<EventName>]
: []
): Promise<RageFW_ClientServerReturn<EventName>> {
): Promise<T.RageFW_ClientServerReturn<EventName>> {
return this._rpc.callServer(eventName, args)
}
public triggerBrowser<EventName extends RageFW_CefEvent>(
public triggerBrowser<EventName extends T.RageFW_CefEvent>(
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
): 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)

View File

@ -1,15 +1,6 @@
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'
import type * as T from '../types'
export class Player {
private _rpc: Rpc = new Rpc()
@ -18,23 +9,23 @@ export class Player {
return this._rpc
}
public triggerClient<EventName extends RageFW_ClientEvent>(
public triggerClient<EventName extends T.RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ServerClientArgs<EventName>]
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ServerClientArgs<EventName>]
: []
): Promise<RageFW_ServerClientReturn<EventName>> {
): Promise<T.RageFW_ServerClientReturn<EventName>> {
return this._rpc.callClient(player, eventName, args)
}
public triggerBrowser<EventName extends RageFW_CefEvent>(
public triggerBrowser<EventName extends T.RageFW_CefEvent>(
player: PlayerMp,
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
...args: T._CefEventHasArgs<EventName> extends true
? [T.RageFW_CefArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
): Promise<T.RageFW_CefReturn<EventName>> {
return this._rpc.callBrowser(player, eventName, args)
}
}

View File

@ -1,16 +1,8 @@
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'
import { nativeEvents } from '../data/nativeEvents'
import type * as T from '../types'
export class Server {
private _rpc: Rpc = new Rpc()
@ -25,12 +17,12 @@ export class Server {
private registerCustom<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
callback: RageFW_ServerCallbackCustom<EventName>,
callback: T.RageFW_ServerCallbackCustom<EventName>,
): void {
this._rpc.register(
eventName,
// fixme
async (args: RageFW_ServerArgs<EventName>, info) => {
async (args: T.RageFW_ServerArgs<EventName>, info) => {
await callback([info.player as PlayerMp, ...args])
},
)
@ -38,7 +30,7 @@ export class Server {
private registerNative<EventName extends keyof IServerEvents>(
eventName: EventName,
callback: RageFW_ServerCallbackNative<EventName>,
callback: T.RageFW_ServerCallbackNative<EventName>,
): void {
mp.events.add(
eventName,
@ -47,37 +39,37 @@ export class Server {
)
}
public register<EventName extends RageFW_ServerEvent>(
public register<EventName extends T.RageFW_ServerEvent>(
eventName: EventName,
callback: RageFW_ServerCallback<EventName>,
callback: T.RageFW_ServerCallback<EventName>,
): void {
if (this.isNativeEvent(eventName)) {
this.registerNative(
eventName,
callback as RageFW_ServerCallbackNative,
callback as T.RageFW_ServerCallbackNative,
)
} else {
this.registerCustom(
eventName,
callback as unknown as RageFW_ServerCallbackCustom,
callback as unknown as T.RageFW_ServerCallbackCustom,
)
}
}
public registerMany<EventName extends RageFW_ServerEvent>(events: {
[event in EventName]: RageFW_ServerCallback<event>
public registerMany<EventName extends T.RageFW_ServerEvent>(events: {
[event in EventName]: T.RageFW_ServerCallback<event>
}): void {
Object.entries<RageFW_ServerCallback<EventName>>(events).map(
Object.entries<T.RageFW_ServerCallback<EventName>>(events).map(
([eventName, callback]) => {
if (this.isNativeEvent(eventName)) {
this.registerNative(
eventName,
callback as RageFW_ServerCallbackNative,
callback as T.RageFW_ServerCallbackNative,
)
} else {
this.registerCustom(
eventName as keyof RageFW_ICustomServerEvent,
callback as unknown as RageFW_ServerCallbackCustom,
callback as unknown as T.RageFW_ServerCallbackCustom,
)
}
},
@ -96,7 +88,7 @@ export class Server {
mp.events.remove(eventName)
}
public unregister<EventName extends RageFW_ServerEvent>(
public unregister<EventName extends T.RageFW_ServerEvent>(
eventName: EventName,
): void {
if (this.isNativeEvent(eventName)) {
@ -108,10 +100,10 @@ export class Server {
public trigger<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerArgs<EventName>]
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<RageFW_ServerReturn<EventName>> {
): Promise<T.RageFW_ServerReturn<EventName>> {
return this._rpc.call(eventName, args)
}
}