Archived
Use Typescript
This commit is contained in:
1 parent
65241d1c07
commit
94ffe6c474
11 files changed
+1377
-93
No files matched your search
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
export as namespace rpc;
|
||||
|
||||
export function register(name: string, cb: ProcedureListener): void;
|
||||
export function unregister(name: string): void;
|
||||
export function call(name: string, args?: any): Promise<any>;
|
||||
export function callServer(name: string, args?: any): Promise<any>;
|
||||
export function callClient(player: Player, name: string, args?: any): Promise<any>;
|
||||
export function callClient(name: string, args?: any): Promise<any>;
|
||||
export function callBrowsers(player: Player, name: string, args?: any): Promise<any>;
|
||||
export function callBrowsers(name: string, args?: any): Promise<any>;
|
||||
export function callBrowser(browser: Browser, name: string, args?: any): Promise<any>;
|
||||
|
||||
export interface Player {
|
||||
call: (eventName: string, args?: any[]) => void;
|
||||
[property: string]: any;
|
||||
}
|
||||
|
||||
export interface Browser {
|
||||
execute: (code: string) => void;
|
||||
[property: string]: any;
|
||||
}
|
||||
|
||||
export interface ProcedureListenerInfo {
|
||||
environment: string;
|
||||
id?: string;
|
||||
player?: Player;
|
||||
}
|
||||
|
||||
export type ProcedureListener = (args: any, info: ProcedureListenerInfo) => any;
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.rpc=r():e.rpc=r()}("undefined"!=typeof self?self:this,function(){return function(e){var r={};function n(t){if(r[t])return r[t].exports;var c=r[t]={i:t,l:!1,exports:{}};return e[t].call(c.exports,c,c.exports,n),c.l=!0,c.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var c in e)n.d(t,c,function(r){return e[r]}.bind(null,c));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r,n){"use strict";function t(){let e=46656*Math.random()|0,r=46656*Math.random()|0;return(e=("000"+e.toString(36)).slice(-3))+(r=("000"+r.toString(36)).slice(-3))}function c(){if(mp)return mp.joaat?"server":mp.game&&mp.game.joaat?"client":mp.trigger?"cef":void 0}function o(e){return JSON.stringify(e)}function s(e){return JSON.parse(e)}n.d(r,"d",function(){return t}),n.d(r,"a",function(){return c}),n.d(r,"c",function(){return o}),n.d(r,"b",function(){return s})},function(e,r,n){"use strict";n.r(r),function(e){n.d(r,"register",function(){return p}),n.d(r,"unregister",function(){return m}),n.d(r,"call",function(){return d}),n.d(r,"callServer",function(){return v}),n.d(r,"callClient",function(){return h}),n.d(r,"callBrowsers",function(){return b}),n.d(r,"callBrowser",function(){return y});var t=n(0);const c=t.a();if(!c)throw"Unknown RAGE environment";const o="PROCEDURE_NOT_FOUND",s="__rpc:process",i="__rpc:exists",a="cef"===c?window:e;let l,u;async function f(e,r,n){const t=a.__rpcListeners[e];if(!t)throw o;return t(r,n)}function p(e,r){if(2!==arguments.length)throw'register expects 2 arguments: "name" and "cb"';a.__rpcListeners[e]=r}function m(e){if(1!==arguments.length)throw'unregister expects 1 argument: "name"';a.__rpcListeners[e]=void 0}function d(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('call expects 1 or 2 arguments: "name" and optional "args"'):f(e,r,{environment:c})}function g(e,r,n){switch(c){case"server":return d(e,r);case"client":{const o=t.d();return new Promise((i,l)=>{a.__rpcPending[o]={resolve:i,reject:l},mp.events.callRemote(s,t.c({req:1,id:o,name:e,env:c,args:r,...n}))})}case"cef":return h("__rpc:callServer",[e,r])}}function v(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('callServer expects 1 or 2 arguments: "name" and optional "args"'):g(e,r,{})}function h(e,r,n){switch(c){case"client":return n=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r?Promise.reject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"'):d(r,n);case"server":{if(2!==arguments.length&&3!==arguments.length||"object"!=typeof e)return Promise.reject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"');const o=t.d();return new Promise((i,l)=>{a.__rpcPending[o]={resolve:i,reject:l,player:e},e.call(s,[t.c({req:1,id:o,name:r,env:c,args:n})])})}case"cef":{if(n=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r)return Promise.reject('callClient from the browser expects 1 or 2 arguments: "name" and optional "args"');const o=t.d();return new Promise((e,i)=>{a.__rpcPending[o]={resolve:e,reject:i},mp.trigger(s,t.c({req:1,id:o,name:r,env:c,args:n}))})}}}function w(e,r,n,t,o){return new Promise((s,i)=>{a.__rpcPending[e]={resolve:s,reject:i},l(r,{req:1,id:e,name:n,env:c,args:t,...o},!1)})}async function _(e,r,n,s){switch(c){case"client":{n=r,r=e;const c=t.d(),a=mp.browsers.length;let l;for(let e=0;e<a;e++){const n=mp.browsers.at(e);if(await new Promise(e=>{const t=r=>{const o=r.split(":");o[0]===c&&+o[1]&&(l=n),mp.events.remove(i,t),e()};mp.events.add(i,t),n.execute(`var f = window["${i}"]; mp.trigger("${i}", "${c}:"+((f && f("${r}")) ? 1 : 0));`)}),l)break}if(l)return w(c,l,r,n,s);throw o}case"server":return h(e,"__rpc:callBrowsers",[r,n]);case"cef":return n=r,h("__rpc:callBrowsers",[r=e,n])}}function b(e,r,n){switch(c){case"client":if(1!==arguments.length&&2!==arguments.length)return Promise.reject('callBrowsers from the client expects 1 or 2 arguments: "name" and optional "args"');break;case"server":if(2!==arguments.length&&3!==arguments.length)return Promise.reject('callBrowsers from the server expects 2 or 3 arguments: "player", "name", and optional "args"');break;case"cef":if(1!==arguments.length&&2!==arguments.length)return
Line truncated
|
||||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.rpc=r():e.rpc=r()}("undefined"!=typeof self?self:this,function(){return function(e){var r={};function n(t){if(r[t])return r[t].exports;var c=r[t]={i:t,l:!1,exports:{}};return e[t].call(c.exports,c,c.exports,n),c.l=!0,c.exports}return n.m=e,n.c=r,n.d=function(e,r,t){n.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,r){if(1&r&&(e=n(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(n.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var c in e)n.d(t,c,function(r){return e[r]}.bind(null,c));return t},n.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(r,"a",r),r},n.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},n.p="",n(n.s=1)}([function(e,r,n){"use strict";function t(){const e=46656*Math.random()|0,r=46656*Math.random()|0;return("000"+e.toString(36)).slice(-3)+("000"+r.toString(36)).slice(-3)}function c(){return mp.joaat?"server":mp.game&&mp.game.joaat?"client":mp.trigger?"cef":void 0}function o(e){return JSON.stringify(e)}function s(e){return JSON.parse(e)}n.d(r,"d",function(){return t}),n.d(r,"a",function(){return c}),n.d(r,"c",function(){return o}),n.d(r,"b",function(){return s})},function(e,r,n){"use strict";n.r(r),function(e){n.d(r,"register",function(){return p}),n.d(r,"unregister",function(){return d}),n.d(r,"call",function(){return m}),n.d(r,"callServer",function(){return v}),n.d(r,"callClient",function(){return h}),n.d(r,"callBrowsers",function(){return y}),n.d(r,"callBrowser",function(){return b});var t=n(0);const c=t.a();if(!c)throw"Unknown RAGE environment";const o="PROCEDURE_NOT_FOUND",s="__rpc:process",i="__rpc:exists",a="cef"===c?window:e;function l(e,r,n){const c=t.c(r);e.execute(`var process = window["${s}"]; if(process){ process('${c}'); }else{ ${n?"":`mp.trigger("${s}", '{"ret":1,"id":"${r.id}","err":"${o}","env":"cef"}');`} }`)}function u(e,r){mp.browsers.forEach(n=>l(n,e,r))}async function f(e,r,n){const t=a.__rpcListeners[e];if(!t)throw o;return t(r,n)}function p(e,r){if(2!==arguments.length)throw'register expects 2 arguments: "name" and "cb"';a.__rpcListeners[e]=r}function d(e){if(1!==arguments.length)throw'unregister expects 1 argument: "name"';a.__rpcListeners[e]=void 0}function m(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('call expects 1 or 2 arguments: "name" and optional "args"'):f(e,r,{environment:c})}function g(e,r,n={}){switch(c){case"server":return m(e,r);case"client":{const o=t.d();return new Promise((i,l)=>{a.__rpcPending[o]={resolve:i,reject:l};const u={req:1,id:o,name:e,env:c,args:r,...n};mp.events.callRemote(s,t.c(u))})}case"cef":return h("__rpc:callServer",[e,r])}}function v(e,r){return 1!==arguments.length&&2!==arguments.length?Promise.reject('callServer expects 1 or 2 arguments: "name" and optional "args"'):g(e,r,{})}function h(e,r,n){switch(c){case"client":return n=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r?Promise.reject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"'):m(r,n);case"server":{if(2!==arguments.length&&3!==arguments.length||"object"!=typeof e)return Promise.reject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"');const o=t.d();return new Promise((i,l)=>{a.__rpcPending[o]={resolve:i,reject:l,player:e};const u={req:1,id:o,name:r,env:c,args:n};e.call(s,[t.c(u)])})}case"cef":{if(n=r,r=e,1!==arguments.length&&2!==arguments.length||"string"!=typeof r)return Promise.reject('callClient from the browser expects 1 or 2 arguments: "name" and optional "args"');const o=t.d();return new Promise((e,i)=>{a.__rpcPending[o]={resolve:e,reject:i};const l={req:1,id:o,name:r,env:c,args:n};mp.trigger(s,t.c(l))})}}}function _(e,r,n,t,o={}){return new Promise((s,i)=>{a.__rpcPending[e]={resolve:s,reject:i},l(r,{req:1,id:e,name:n,env:c,args:t,...o},!1)})}async function w(e,r,n,s={}){switch(c){case"client":{const e=t.d(),c=mp.browsers.length;let a;for(let n=0;n<c;n++){const t=mp.browsers.at(n);if(await new Promise(n=>{const c=r=>{const o=r.split(":");o[0]===e&&+o[1]&&(a=t),mp.events.remove(i,c),n()};mp.events.add(i,c),t.execute(`var f = window["${i}"]; mp.trigger("${i}", "${e}:"+((f && f("${r}")) ? 1 : 0));`)}),a)break}if(a)return _(e,a,r,n,s);throw o}case"server":return h(e,"__rpc:callBrowsers",[r,n]);case"cef":return h("__rpc:callBrowsers",[r,n])}}function y(e,r,n){switch(c){case"client":case"cef":return 1!==arguments.length&&2!==arguments.length?Promise.reject('callBrowsers from the client or browser expects 1 or 2 arguments: "name" and
Line truncated
|
||||
Generated
+1198
File diff suppressed because it is too large.
Load diff
+10
-3
@@ -3,10 +3,12 @@
|
||||
"version": "0.0.3",
|
||||
"description": "An asynchronous RPC implementation for RAGE Multiplayer",
|
||||
"main": "dist/rage-rpc.min.js",
|
||||
"types": "dist/rage-rpc.d.ts",
|
||||
"scripts": {
|
||||
"build": "webpack-cli --config ./webpack.config.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"type-check": "tsc"
|
||||
},
|
||||
"files": ["dist"],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/micaww/rage-rpc.git"
|
||||
@@ -19,7 +21,12 @@
|
||||
"homepage": "https://github.com/micaww/rage-rpc#readme",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"webpack-cli": "^3.1.2",
|
||||
"webpack": "^4.23.1"
|
||||
"@babel/core": "^7.2.2",
|
||||
"@babel/preset-env": "^7.2.3",
|
||||
"@babel/preset-typescript": "^7.1.0",
|
||||
"babel-loader": "^8.0.5",
|
||||
"typescript": "^3.2.2",
|
||||
"webpack": "^4.23.1",
|
||||
"webpack-cli": "^3.1.2"
|
||||
}
|
||||
}
|
||||
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
declare var mp: any;
|
||||
declare var global: any;
|
||||
declare var window: any;
|
||||
|
||||
declare type ProcedureListener = (args: any, info: ProcedureListenerInfo) => any;
|
||||
|
||||
declare interface Player {
|
||||
call: (eventName: string, args?: any[]) => void;
|
||||
[property: string]: any;
|
||||
}
|
||||
|
||||
declare interface Browser {
|
||||
execute: (code: string) => void;
|
||||
[property: string]: any;
|
||||
}
|
||||
|
||||
declare interface ProcedureListenerInfo {
|
||||
environment: string;
|
||||
id?: string;
|
||||
player?: Player;
|
||||
}
|
||||
|
||||
declare interface Event {
|
||||
req?: number;
|
||||
ret?: number;
|
||||
id: string;
|
||||
name?: string;
|
||||
args?: any;
|
||||
env: string;
|
||||
fenv?: string;
|
||||
res?: any;
|
||||
err?: any;
|
||||
}
|
||||
+54
-66
@@ -1,4 +1,4 @@
|
||||
import * as util from './util.js';
|
||||
import * as util from './util';
|
||||
|
||||
const environment = util.getEnvironment();
|
||||
if(!environment) throw 'Unknown RAGE environment';
|
||||
@@ -14,17 +14,16 @@ if(!glob[PROCESS_EVENT]){
|
||||
glob.__rpcListeners = {};
|
||||
glob.__rpcPending = {};
|
||||
|
||||
glob[PROCESS_EVENT] = (...args) => {
|
||||
let rawData = args[0];
|
||||
if(environment === "server") rawData = args[1];
|
||||
const data = util.parseData(rawData);
|
||||
glob[PROCESS_EVENT] = (player: Player | string, rawData?: string) => {
|
||||
if(environment !== "server") rawData = player as string;
|
||||
const data: Event = util.parseData(rawData);
|
||||
|
||||
if(data.req){ // someone is trying to remotely call a procedure
|
||||
const info = {
|
||||
const info: ProcedureListenerInfo = {
|
||||
id: data.id,
|
||||
environment: data.fenv || data.env
|
||||
};
|
||||
if(environment === "server") info.player = args[0];
|
||||
if(environment === "server") info.player = player as Player;
|
||||
const promise = callProcedure(data.name, data.args, info);
|
||||
const part = {
|
||||
ret: 1,
|
||||
@@ -90,7 +89,7 @@ if(!glob[PROCESS_EVENT]){
|
||||
}
|
||||
}else if(data.ret){ // a previously called remote procedure has returned
|
||||
const info = glob.__rpcPending[data.id];
|
||||
if(environment === "server" && info.player !== args[0]) return;
|
||||
if(environment === "server" && info.player !== player) return;
|
||||
if(info){
|
||||
if(data.err) info.reject(data.err);
|
||||
else info.resolve(data.res);
|
||||
@@ -100,7 +99,7 @@ if(!glob[PROCESS_EVENT]){
|
||||
};
|
||||
|
||||
if(environment === "cef"){
|
||||
window[PROCEDURE_EXISTS] = name => !!glob.__rpcListeners[name];
|
||||
window[PROCEDURE_EXISTS] = (name: string) => !!glob.__rpcListeners[name];
|
||||
}else{
|
||||
mp.events.add(PROCESS_EVENT, glob[PROCESS_EVENT]);
|
||||
|
||||
@@ -120,19 +119,16 @@ if(!glob[PROCESS_EVENT]){
|
||||
}
|
||||
}
|
||||
|
||||
let passEventToBrowser, passEventToBrowsers;
|
||||
if(environment === "client"){
|
||||
passEventToBrowser = (browser, data, ignore) => {
|
||||
const raw = util.stringifyData(data);
|
||||
browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ ${ignore ? '' : `mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}","env":"cef"}');`} }`);
|
||||
};
|
||||
|
||||
passEventToBrowsers = (data, ignore) => {
|
||||
mp.browsers.forEach(browser => passEventToBrowser(browser, data, ignore));
|
||||
};
|
||||
function passEventToBrowser(browser: Browser, data: Event, ignore: boolean): void {
|
||||
const raw = util.stringifyData(data);
|
||||
browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ ${ignore ? '' : `mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}","env":"cef"}');`} }`);
|
||||
}
|
||||
|
||||
async function callProcedure(name, args, info){
|
||||
function passEventToBrowsers(data: Event, ignore: boolean): void {
|
||||
mp.browsers.forEach((browser: Browser) => passEventToBrowser(browser, data, ignore));
|
||||
}
|
||||
|
||||
async function callProcedure(name: string, args: any, info: ProcedureListenerInfo){
|
||||
const listener = glob.__rpcListeners[name];
|
||||
if(!listener) throw ERR_NOT_FOUND;
|
||||
return listener(args, info);
|
||||
@@ -143,7 +139,7 @@ async function callProcedure(name, args, info){
|
||||
* @param {string} name - The name of the procedure.
|
||||
* @param {function} cb - The procedure's callback. The return value will be sent back to the caller.
|
||||
*/
|
||||
export function register(name, cb){
|
||||
export function register(name: string, cb: ProcedureListener): void {
|
||||
if(arguments.length !== 2) throw 'register expects 2 arguments: "name" and "cb"';
|
||||
glob.__rpcListeners[name] = cb;
|
||||
}
|
||||
@@ -152,7 +148,7 @@ export function register(name, cb){
|
||||
* Unregister a procedure.
|
||||
* @param {string} name - The name of the procedure.
|
||||
*/
|
||||
export function unregister(name){
|
||||
export function unregister(name: string): void {
|
||||
if(arguments.length !== 1) throw 'unregister expects 1 argument: "name"';
|
||||
glob.__rpcListeners[name] = undefined;
|
||||
}
|
||||
@@ -162,16 +158,16 @@ export function unregister(name){
|
||||
*
|
||||
* Can be called from any environment.
|
||||
*
|
||||
* @param {string} name - The name of the locally registered procedure.
|
||||
* @param name - The name of the locally registered procedure.
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
* @returns The result from the procedure.
|
||||
*/
|
||||
export function call(name, args){
|
||||
export function call(name: string, args?: any): Promise<any> {
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('call expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return callProcedure(name, args, { environment });
|
||||
}
|
||||
|
||||
function _callServer(name, args, extraData){
|
||||
function _callServer(name: string, args?: any, extraData = {}): Promise<any> {
|
||||
switch(environment){
|
||||
case "server": {
|
||||
return call(name, args);
|
||||
@@ -183,14 +179,15 @@ function _callServer(name, args, extraData){
|
||||
resolve,
|
||||
reject
|
||||
};
|
||||
mp.events.callRemote(PROCESS_EVENT, util.stringifyData({
|
||||
const event: Event = {
|
||||
req: 1,
|
||||
id,
|
||||
name,
|
||||
env: environment,
|
||||
args,
|
||||
...extraData
|
||||
}));
|
||||
};
|
||||
mp.events.callRemote(PROCESS_EVENT, util.stringifyData(event));
|
||||
});
|
||||
}
|
||||
case "cef": {
|
||||
@@ -204,11 +201,11 @@ function _callServer(name, args, extraData){
|
||||
*
|
||||
* Can be called from any environment.
|
||||
*
|
||||
* @param {string} name - The name of the registered procedure.
|
||||
* @param name - The name of the registered procedure.
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
* @returns The result from the procedure.
|
||||
*/
|
||||
export function callServer(name, args){
|
||||
export function callServer(name: string, args?: any): Promise<any> {
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callServer expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return _callServer(name, args, {});
|
||||
}
|
||||
@@ -219,16 +216,11 @@ export function callServer(name, args){
|
||||
* Can be called from any environment.
|
||||
*
|
||||
* @param player - The player to call the procedure on.
|
||||
* @param {string} name - The name of the registered procedure.
|
||||
* @param name - The name of the registered procedure.
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
* @returns The result from the procedure.
|
||||
*/
|
||||
// serverside
|
||||
// callClient(player, name, args)
|
||||
//
|
||||
// clientside or cef
|
||||
// callClient(name, args)
|
||||
export function callClient(player, name, args){
|
||||
export function callClient(player: Player | string, name?: string | any, args?: any): Promise<any> {
|
||||
switch(environment){
|
||||
case "client": {
|
||||
args = name;
|
||||
@@ -245,13 +237,14 @@ export function callClient(player, name, args){
|
||||
reject,
|
||||
player
|
||||
};
|
||||
player.call(PROCESS_EVENT, [util.stringifyData({
|
||||
const event: Event = {
|
||||
req: 1,
|
||||
id,
|
||||
name,
|
||||
env: environment,
|
||||
args
|
||||
})]);
|
||||
};
|
||||
player.call(PROCESS_EVENT, [util.stringifyData(event)]);
|
||||
});
|
||||
}
|
||||
case "cef": {
|
||||
@@ -264,19 +257,20 @@ export function callClient(player, name, args){
|
||||
resolve,
|
||||
reject
|
||||
};
|
||||
mp.trigger(PROCESS_EVENT, util.stringifyData({
|
||||
const event: Event = {
|
||||
req: 1,
|
||||
id,
|
||||
name,
|
||||
env: environment,
|
||||
args
|
||||
}));
|
||||
};
|
||||
mp.trigger(PROCESS_EVENT, util.stringifyData(event));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _callBrowser(id, browser, name, args, extraData){
|
||||
function _callBrowser(id: string, browser: Browser, name: string, args?: any, extraData = {}){
|
||||
return new Promise((resolve, reject) => {
|
||||
glob.__rpcPending[id] = {
|
||||
resolve,
|
||||
@@ -292,18 +286,17 @@ function _callBrowser(id, browser, name, args, extraData){
|
||||
}, false);
|
||||
});
|
||||
}
|
||||
async function _callBrowsers(player, name, args, extraData){
|
||||
|
||||
async function _callBrowsers(player: Player, name: string, args?: any, extraData = {}): Promise<any> {
|
||||
switch(environment){
|
||||
case "client": {
|
||||
args = name;
|
||||
name = player;
|
||||
const id = util.uid();
|
||||
const numBrowsers = mp.browsers.length;
|
||||
let browser;
|
||||
for(let i = 0; i < numBrowsers; i++){
|
||||
const b = mp.browsers.at(i);
|
||||
await new Promise(resolve => {
|
||||
const existsHandler = str => {
|
||||
const existsHandler = (str: string) => {
|
||||
const parts = str.split(':');
|
||||
if(parts[0] === id){
|
||||
if(+parts[1]){
|
||||
@@ -325,8 +318,6 @@ async function _callBrowsers(player, name, args, extraData){
|
||||
return callClient(player, '__rpc:callBrowsers', [name, args]);
|
||||
}
|
||||
case "cef": {
|
||||
args = name;
|
||||
name = player;
|
||||
return callClient('__rpc:callBrowsers', [name, args]);
|
||||
}
|
||||
}
|
||||
@@ -337,24 +328,21 @@ async function _callBrowsers(player, name, args, extraData){
|
||||
*
|
||||
* Can be called from any environment.
|
||||
*
|
||||
* @param {object} [player] - The player to call the procedure on.
|
||||
* @param {string} name - The name of the registered procedure.
|
||||
* @param player - The player to call the procedure on.
|
||||
* @param name - The name of the registered procedure.
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
* @returns The result from the procedure.
|
||||
*/
|
||||
export function callBrowsers(player, name, args){
|
||||
export function callBrowsers(player: Player | string, name?: string | any, args?: any): Promise<any> {
|
||||
switch(environment){
|
||||
case "client":
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the client expects 1 or 2 arguments: "name" and optional "args"');
|
||||
break;
|
||||
case "cef":
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the client or browser expects 1 or 2 arguments: "name" and optional "args"');
|
||||
return _callBrowsers(undefined, player as string, name, {});
|
||||
case "server":
|
||||
if(arguments.length !== 2 && arguments.length !== 3) return Promise.reject('callBrowsers from the server expects 2 or 3 arguments: "player", "name", and optional "args"');
|
||||
break;
|
||||
case "cef":
|
||||
if(arguments.length !== 1 && arguments.length !== 2) return Promise.reject('callBrowsers from the browser expects 1 or 2 arguments: "name" and optional "args"');
|
||||
break;
|
||||
return _callBrowsers(player as Player, name, args, {});
|
||||
}
|
||||
return _callBrowsers(player, name, args, {});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -362,13 +350,13 @@ export function callBrowsers(player, name, args){
|
||||
*
|
||||
* Client-side environment only.
|
||||
*
|
||||
* @param {object} browser - The browser instance.
|
||||
* @param {string} name - The name of the registered procedure.
|
||||
* @param browser - The browser instance.
|
||||
* @param name - The name of the registered procedure.
|
||||
* @param args - Any parameters for the procedure.
|
||||
* @returns {Promise} - The result from the procedure.
|
||||
* @returns The result from the procedure.
|
||||
*/
|
||||
export function callBrowser(browser, name, args){
|
||||
if(environment !== "client") return Promise.reject('callBrowser can only be used in the client environment');
|
||||
export function callBrowser(browser: Browser, name: string, args?: any): Promise<any> {
|
||||
if(environment !== 'client') return Promise.reject('callBrowser can only be used in the client environment');
|
||||
if(arguments.length !== 2 && arguments.length !== 3) return Promise.reject('callBrowser expects 2 or 3 arguments: "browser", "name", and optional "args"');
|
||||
const id = util.uid();
|
||||
return _callBrowser(id, browser, name, args, {});
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
export function uid(){
|
||||
let firstPart = (Math.random() * 46656) | 0;
|
||||
let secondPart = (Math.random() * 46656) | 0;
|
||||
firstPart = ('000' + firstPart.toString(36)).slice(-3);
|
||||
secondPart = ('000' + secondPart.toString(36)).slice(-3);
|
||||
return firstPart + secondPart;
|
||||
}
|
||||
|
||||
export function getEnvironment(){
|
||||
if(!mp) return undefined;
|
||||
if(mp.joaat) return 'server';
|
||||
else if(mp.game && mp.game.joaat) return 'client';
|
||||
else if(mp.trigger) return 'cef';
|
||||
}
|
||||
|
||||
export function stringifyData(data){
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
export function parseData(data){
|
||||
return JSON.parse(data);
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
export function uid(): string {
|
||||
const first = (Math.random() * 46656) | 0;
|
||||
const second = (Math.random() * 46656) | 0;
|
||||
const firstPart = ('000' + first.toString(36)).slice(-3);
|
||||
const secondPart = ('000' + second.toString(36)).slice(-3);
|
||||
return firstPart + secondPart;
|
||||
}
|
||||
|
||||
export function getEnvironment(): string {
|
||||
if(mp.joaat) return 'server';
|
||||
else if(mp.game && mp.game.joaat) return 'client';
|
||||
else if(mp.trigger) return 'cef';
|
||||
}
|
||||
|
||||
export function stringifyData(data: any): string {
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
export function parseData(data: string): any {
|
||||
return JSON.parse(data);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": ["es2015"],
|
||||
"noEmit": true,
|
||||
"isolatedModules": true,
|
||||
"noImplicitAny": true
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
+12
-1
@@ -1,8 +1,19 @@
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
entry: './src/index.js',
|
||||
entry: './src/index.ts',
|
||||
mode: 'production',
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.ts$/,
|
||||
loader: 'babel-loader'
|
||||
}
|
||||
]
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts']
|
||||
},
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
filename: 'rage-rpc.min.js',
|
||||
|
||||
Reference in new issue
Block a user