- added more comments
This commit is contained in:
Danya H 2024-10-27 15:39:13 +00:00
parent 830a69e91c
commit 2bcced5a56
4 changed files with 36 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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