Rpc integration + type fixes #3

Merged
rilaxik merged 26 commits from dev into master 2024-10-28 12:13:19 +00:00
10 changed files with 74 additions and 35 deletions
Showing only changes of commit 59910303f7 - Show all commits

View File

@ -11,7 +11,7 @@
"build": "tsup"
},
"dependencies": {
"ragefw-rpc": "workspace:^"
"rage-fw-rpc": "workspace:^"
},
"peerDependencies": {
"@ragempcommunity/types-cef": "^2.1.8",

View File

@ -1,6 +1,6 @@
import rpc from 'rage-rpc'
import { Rpc } from 'rage-fw-rpc'
import {
import type {
_CefEventHasArgs,
_ClientEventHasArgs,
_ServerEventHasArgs,
@ -18,11 +18,18 @@ import {
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>,
@ -32,11 +39,11 @@ class Cef {
}
if ('mp' in window) {
rpc.register(eventName, callback)
this._rpc.register(eventName, callback)
}
}
public trigger<EventName extends keyof RageFW_ICustomCefEvent>(
public async trigger<EventName extends keyof RageFW_ICustomCefEvent>(
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
@ -47,7 +54,7 @@ class Cef {
}
if ('mp' in window) {
return rpc.call(eventName, args)
return await this._rpc.call(eventName, args)
}
return Promise.reject(
@ -55,7 +62,9 @@ class Cef {
)
}
public triggerServer<EventName extends keyof RageFW_ICustomServerEvent>(
public async triggerServer<
EventName extends keyof RageFW_ICustomServerEvent,
>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerArgs<EventName>]
@ -66,7 +75,7 @@ class Cef {
}
if ('mp' in window) {
return rpc.callServer(eventName, args)
return await this._rpc.callServer(eventName, args)
}
return Promise.reject(
@ -74,7 +83,9 @@ class Cef {
)
}
public triggerClient<EventName extends keyof RageFW_ICustomClientEvent>(
public async triggerClient<
EventName extends keyof RageFW_ICustomClientEvent,
>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
@ -85,7 +96,7 @@ class Cef {
}
if ('mp' in window) {
return rpc.callClient(eventName, args)
return await this._rpc.callClient(eventName, args)
}
return Promise.reject(

View File

@ -16,13 +16,11 @@
],
"dependencies": {
"@inquirer/prompts": "^5.0.5",
"axios": "^1.7.2",
"ky": "^1.7.2",
"chalk": "4.1.2",
"git-clone": "^0.2.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/git-clone": "^0.2.4",
"@types/node": "^20.14.2",
"@types/yargs": "^17.0.32",
"prettier": "^3.3.2",

View File

@ -11,7 +11,7 @@
"build": "tsup"
},
"dependencies": {
"ragefw-rpc": "workspace:^"
"rage-fw-rpc": "workspace:^"
},
"peerDependencies": {
"@ragempcommunity/types-client": "^2.1.8",

View File

@ -1,20 +1,33 @@
import rpc from 'rage-rpc'
import { Rpc } from 'rage-fw-rpc'
import { RageFW_ClientCallback, RageFW_ClientEvent } from '../types'
import type {
RageFW_ClientArgs,
RageFW_ClientCallback,
RageFW_ClientEvent,
} from '../types'
export class Client {
private _rpc: Rpc = new Rpc()
get rpc(): Rpc {
return this._rpc
}
public register<EventName extends RageFW_ClientEvent>(
eventName: EventName,
callback: RageFW_ClientCallback<EventName>,
): void {
rpc.register(eventName, async data => {
return await callback(data)
})
this._rpc.register(
eventName,
async (data: RageFW_ClientArgs<EventName>) => {
return await callback(data)
},
)
}
public unregister<EventName extends RageFW_ClientEvent>(
eventName: EventName,
): void {
rpc.unregister(eventName)
this._rpc.unregister(eventName)
}
}

View File

@ -1,4 +1,4 @@
import rpc from 'rage-rpc'
import { Rpc } from 'rage-fw-rpc'
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
import {
@ -16,15 +16,19 @@ import {
} 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>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
return rpc.call<RageFW_ClientReturn<EventName>>(eventName, args)
return this._rpc.call(eventName, args)
}
public triggerServer<EventName extends RageFW_ClientServerEvent>(
@ -33,7 +37,7 @@ export class Player {
? [RageFW_ClientServerArgs<EventName>]
: []
): Promise<RageFW_ClientServerReturn<EventName>> {
return rpc.callServer(eventName, args)
return this._rpc.callServer(eventName, args)
}
public triggerBrowser<EventName extends RageFW_CefEvent>(
@ -44,6 +48,6 @@ export class Player {
): Promise<RageFW_CefReturn<EventName>> {
if (!this.browser)
throw new Error('You need to initialize browser first')
return rpc.callBrowser(this.browser, eventName, args)
return this._rpc.callBrowser(this.browser, eventName, args)
}
}

View File

@ -1,5 +1,5 @@
{
"name": "ragefw-rpc",
"name": "rage-fw-rpc",
"description": "RageFW RPC",
"version": "0.2.1",
"scripts": {

View File

@ -11,7 +11,7 @@
"build": "tsup"
},
"dependencies": {
"ragefw-rpc": "workspace:^"
"rage-fw-rpc": "workspace:^"
},
"peerDependencies": {
"@ragempcommunity/types-server": "^2.1.8",

View File

@ -1,6 +1,6 @@
import rpc from 'rage-rpc'
import { Rpc } from 'rage-fw-rpc'
import {
import type {
_CefEventHasArgs,
_ClientEventHasArgs,
RageFW_CefArgs,
@ -12,6 +12,12 @@ import {
} from '../types'
export class Player {
private _rpc: Rpc = new Rpc()
get rpc(): Rpc {
return this._rpc
}
public triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
@ -19,7 +25,7 @@ export class Player {
? [RageFW_ServerClientArgs<EventName>]
: []
): Promise<RageFW_ServerClientReturn<EventName>> {
return rpc.callClient(player, eventName, args)
return this._rpc.callClient(player, eventName, args)
}
public triggerBrowser<EventName extends RageFW_CefEvent>(
@ -29,6 +35,6 @@ export class Player {
? [RageFW_CefArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
return rpc.callBrowsers(player, eventName, args)
return this._rpc.callBrowser(player, eventName, args)
}
}

View File

@ -1,8 +1,8 @@
import rpc from 'rage-rpc'
import { Rpc } from 'rage-fw-rpc'
import { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
import { nativeEvents } from '../native.events'
import {
import type {
_ServerEventHasArgs,
RageFW_ServerArgs,
RageFW_ServerCallback,
@ -13,6 +13,12 @@ import {
} from '../types'
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)
}
@ -21,8 +27,9 @@ export class Server {
eventName: EventName,
callback: RageFW_ServerCallbackCustom<EventName>,
): void {
rpc.register(
this._rpc.register(
eventName,
// fixme
async (args: RageFW_ServerArgs<EventName>, info) => {
await callback([info.player as PlayerMp, ...args])
},
@ -80,7 +87,7 @@ export class Server {
private unregisterCustom<EventName extends keyof RageFW_ICustomServerEvent>(
eventName: EventName,
): void {
rpc.unregister(eventName)
this._rpc.unregister(eventName)
}
private unregisterNative<EventName extends keyof IServerEvents>(
@ -105,6 +112,6 @@ export class Server {
? [RageFW_ServerArgs<EventName>]
: []
): Promise<RageFW_ServerReturn<EventName>> {
return rpc.call<RageFW_ServerReturn<EventName>>(eventName, args)
return this._rpc.call(eventName, args)
}
}