Rpc integration + type fixes #3

Merged
rilaxik merged 26 commits from dev into master 2024-10-28 12:13:19 +00:00
4 changed files with 36 additions and 1 deletions
Showing only changes of commit 2bcced5a56 - Show all commits

View File

@ -1,7 +1,7 @@
{
"name": "ragefw-rpc",
"description": "RageFW RPC",
"version": "0.2.0",
"version": "0.2.1",
"scripts": {
"build": "tsup",
"start": "npx ./dist create"

View File

@ -8,6 +8,9 @@ import {
Utils,
} from './utils'
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
export class Browser extends Wrapper {
constructor(
options: RpcWrapperConfig = {
@ -17,6 +20,9 @@ export class Browser extends Wrapper {
super(options)
}
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
public _resolveEmitDestination(dataRaw: string) {
let state = Utils.prepareExecution(dataRaw)
@ -35,15 +41,18 @@ export class Browser extends Wrapper {
mp.trigger(Events.LOCAL_EVENT_LISTENER, dataRaw)
}
// called to browser
private async emit(dataRaw: string) {
let state = Utils.prepareExecution(dataRaw)
const responseEventName = Utils.generateResponseEventName(state.uuid)
// check availability
state = this.verifyEvent_(state)
if (state.knownError) {
this.triggerError_(state, state.knownError)
}
// execute + generate response
const response = await this.state_[state.eventName](
...(Array.isArray(state.data) ? state.data : []),
)
@ -58,6 +67,7 @@ export class Browser extends Wrapper {
}
const responseDataRaw = Utils.prepareTransfer(responseState)
// send response
switch (state.calledFrom) {
case Environment.BROWSER:
try {

View File

@ -9,6 +9,9 @@ import {
Utils,
} from './utils'
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
export class Client extends Wrapper {
private _browser: any = null
@ -24,6 +27,9 @@ export class Client extends Wrapper {
this._browser = browser
}
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
public _resolveEmitDestination(dataRaw: string) {
const state = Utils.prepareExecution(dataRaw)
@ -46,14 +52,17 @@ export class Client extends Wrapper {
}
}
// called to client
private async emit(state: RPCState) {
this.errorNoBrowser()
// check availability
state = this.verifyEvent_(state)
if (state.knownError) {
this.triggerError_(state, state.knownError)
}
// execute + generate response
const responseEventName = Utils.generateResponseEventName(state.uuid)
const response = await this.state_[state.eventName](
...(Array.isArray(state.data) ? state.data : []),
@ -68,6 +77,7 @@ export class Client extends Wrapper {
type: RPCEventType.RESPONSE,
}
// send response
switch (state.calledFrom) {
case Environment.CLIENT:
try {
@ -103,11 +113,13 @@ export class Client extends Wrapper {
}
}
// called to server
private emitServer(dataRaw: string) {
this.errorNoBrowser()
const state = Utils.prepareExecution(dataRaw)
// if event is called from browser we will forward response through client via this
if (state.calledFrom === Environment.BROWSER) {
const responseEventName = Utils.generateResponseEventName(
state.uuid,
@ -129,11 +141,13 @@ export class Client extends Wrapper {
mp.events.callRemote(Events.SERVER_EVENT_LISTENER, dataRaw)
}
// called to browser
private emitBrowser(dataRaw: string) {
this.errorNoBrowser()
const state = Utils.prepareExecution(dataRaw)
// if event is called from server we will forward response through client via this
if (state.calledFrom === Environment.SERVER) {
const responseEventName = Utils.generateResponseEventName(
state.uuid,

View File

@ -9,6 +9,9 @@ import {
Utils,
} from './utils'
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
export class Server extends Wrapper {
constructor(
options: RpcWrapperConfig = {
@ -19,6 +22,7 @@ export class Server extends Wrapper {
if (!!options.forceBrowserDevMode) return
// specific event to save player in context as it is not available on server -> server calls
mp.events.add(
Events.SERVER_EVENT_LISTENER,
async (player: PlayerMp, dataRaw: string) => {
@ -27,6 +31,9 @@ export class Server extends Wrapper {
)
}
/**
* NOT INTENDED FOR OUT-OF-CONTEXT USE
*/
public _resolveEmitDestination(player: PlayerMp, dataRaw: string) {
let state = Utils.prepareExecution(dataRaw)
@ -45,15 +52,18 @@ export class Server extends Wrapper {
player.call(Events.LOCAL_EVENT_LISTENER, [dataRaw])
}
// called to server
private async emit(player: PlayerMp, dataRaw: string) {
let state = Utils.prepareExecution(dataRaw)
const responseEventName = Utils.generateResponseEventName(state.uuid)
// check availability
state = this.verifyEvent_(state)
if (state.knownError) {
this.triggerError_(state, state.knownError)
}
// execute + generate response
const response = await this.state_[state.eventName](
player,
...(Array.isArray(state.data) ? state.data : []),
@ -68,6 +78,7 @@ export class Server extends Wrapper {
type: RPCEventType.RESPONSE,
}
// send response
switch (state.calledFrom) {
case Environment.SERVER:
try {