Add hack to fix RAGE promise bug

This commit is contained in:
micah 2019-02-11 00:07:13 -05:00
parent 50486ec049
commit b1fd6fea4d
5 changed files with 1176 additions and 1088 deletions

2191
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
"main": "dist/rage-rpc.min.js", "main": "dist/rage-rpc.min.js",
"types": "dist/rage-rpc.d.ts", "types": "dist/rage-rpc.d.ts",
"scripts": { "scripts": {
"watch": "webpack-cli --config ./webpack.config.js --watch",
"build": "webpack-cli --config ./webpack.config.js", "build": "webpack-cli --config ./webpack.config.js",
"type-check": "tsc" "type-check": "tsc"
}, },
@ -24,12 +25,12 @@
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@babel/core": "^7.2.2", "@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3", "@babel/preset-env": "^7.3.1",
"@babel/preset-typescript": "^7.1.0", "@babel/preset-typescript": "^7.1.0",
"babel-loader": "^8.0.5", "babel-loader": "^8.0.5",
"replace-in-file-webpack-plugin": "^1.0.6", "replace-in-file-webpack-plugin": "^1.0.6",
"typescript": "^3.2.2", "typescript": "^3.3.3",
"webpack": "^4.23.1", "webpack": "^4.29.3",
"webpack-cli": "^3.1.2" "webpack-cli": "^3.2.3"
} }
} }

View File

@ -55,8 +55,7 @@ if(!glob[PROCESS_EVENT]){
const info = glob.__rpcPending[data.id]; const info = glob.__rpcPending[data.id];
if(environment === "server" && info.player !== player) return; if(environment === "server" && info.player !== player) return;
if(info){ if(info){
if(data.err) info.reject(data.err); info.resolve(data.err ? util.promiseReject(data.err) : util.promiseResolve(data.res));
else info.resolve(data.res);
delete glob.__rpcPending[data.id]; delete glob.__rpcPending[data.id];
} }
} }
@ -117,10 +116,10 @@ function passEventToBrowser(browser: Browser, data: Event, ignoreNotFound: boole
browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ ${ignoreNotFound ? '' : `mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}","env":"cef"}');`} }`); browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ ${ignoreNotFound ? '' : `mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}","env":"cef"}');`} }`);
} }
async function callProcedure(name: string, args: any, info: ProcedureListenerInfo){ function callProcedure(name: string, args: any, info: ProcedureListenerInfo): Promise<any> {
const listener = glob.__rpcListeners[name]; const listener = glob.__rpcListeners[name];
if(!listener) throw ERR_NOT_FOUND; if(!listener) return util.promiseReject(ERR_NOT_FOUND);
return listener(args, info); return util.promiseResolve(listener(args, info));
} }
/** /**
@ -154,7 +153,7 @@ export function unregister(name: string): void {
* @returns The result from the procedure. * @returns The result from the procedure.
*/ */
export function call(name: string, args?: any): Promise<any> { 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"'); if(arguments.length !== 1 && arguments.length !== 2) return util.promiseReject('call expects 1 or 2 arguments: "name" and optional "args"');
return callProcedure(name, args, { environment }); return callProcedure(name, args, { environment });
} }
@ -165,10 +164,9 @@ function _callServer(name: string, args?: any, extraData = {}): Promise<any> {
} }
case "client": { case "client": {
const id = util.uid(); const id = util.uid();
return new Promise((resolve, reject) => { return new Promise(resolve => {
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve, resolve
reject
}; };
const event: Event = { const event: Event = {
req: 1, req: 1,
@ -197,7 +195,7 @@ function _callServer(name: string, args?: any, extraData = {}): Promise<any> {
* @returns The result from the procedure. * @returns The result from the procedure.
*/ */
export function callServer(name: string, args?: any): Promise<any> { 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"'); if(arguments.length !== 1 && arguments.length !== 2) return util.promiseReject('callServer expects 1 or 2 arguments: "name" and optional "args"');
return _callServer(name, args, {}); return _callServer(name, args, {});
} }
@ -216,16 +214,15 @@ export function callClient(player: Player | string, name?: string | any, args?:
case "client": { case "client": {
args = name; args = name;
name = player; name = player;
if((arguments.length !== 1 && arguments.length !== 2) || typeof name !== "string") return Promise.reject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"'); if((arguments.length !== 1 && arguments.length !== 2) || typeof name !== "string") return util.promiseReject('callClient from the client expects 1 or 2 arguments: "name" and optional "args"');
return call(name, args); return call(name, args);
} }
case "server": { case "server": {
if((arguments.length !== 2 && arguments.length !== 3) || typeof player !== "object") return Promise.reject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"'); if((arguments.length !== 2 && arguments.length !== 3) || typeof player !== "object") return util.promiseReject('callClient from the server expects 2 or 3 arguments: "player", "name", and optional "args"');
const id = util.uid(); const id = util.uid();
return new Promise((resolve, reject) => { return new Promise(resolve => {
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve, resolve,
reject,
player player
}; };
const event: Event = { const event: Event = {
@ -241,13 +238,12 @@ export function callClient(player: Player | string, name?: string | any, args?:
case "cef": { case "cef": {
args = name; args = name;
name = player; name = player;
if((arguments.length !== 1 && arguments.length !== 2) || typeof name !== "string") return Promise.reject('callClient from the browser expects 1 or 2 arguments: "name" and optional "args"'); if((arguments.length !== 1 && arguments.length !== 2) || typeof name !== "string") return util.promiseReject('callClient from the browser expects 1 or 2 arguments: "name" and optional "args"');
const id = util.uid(); const id = util.uid();
return glob[IDENTIFIER].then((browserId: string) => { return glob[IDENTIFIER].then((browserId: string) => {
return new Promise((resolve, reject) => { return new Promise(resolve => {
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve, resolve
reject
}; };
const event: Event = { const event: Event = {
b: browserId, b: browserId,
@ -264,11 +260,10 @@ export function callClient(player: Player | string, name?: string | any, args?:
} }
} }
function _callBrowser(id: string, browser: Browser, name: string, args?: any, extraData = {}){ function _callBrowser(id: string, browser: Browser, name: string, args?: any, extraData = {}): Promise<any> {
return new Promise((resolve, reject) => { return new Promise(resolve => {
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve, resolve
reject
}; };
passEventToBrowser(browser, { passEventToBrowser(browser, {
req: 1, req: 1,
@ -281,14 +276,14 @@ function _callBrowser(id: string, browser: Browser, name: string, args?: any, ex
}); });
} }
async function _callBrowsers(player: Player, name: string, args?: any, extraData = {}): Promise<any> { function _callBrowsers(player: Player, name: string, args?: any, extraData = {}): Promise<any> {
switch(environment){ switch(environment){
case "client": case "client":
const id = util.uid(); const id = util.uid();
const browserId = glob.__rpcBrowserProcedures[name]; const browserId = glob.__rpcBrowserProcedures[name];
if(!browserId) throw ERR_NOT_FOUND; if(!browserId) return util.promiseReject(ERR_NOT_FOUND);
const browser = glob.__rpcBrowsers[browserId]; const browser = glob.__rpcBrowsers[browserId];
if(!browser || !util.isBrowserValid(browser)) throw ERR_NOT_FOUND; if(!browser || !util.isBrowserValid(browser)) return util.promiseReject(ERR_NOT_FOUND);
return _callBrowser(id, browser, name, args, extraData); return _callBrowser(id, browser, name, args, extraData);
case "server": case "server":
return callClient(player, '__rpc:callBrowsers', [name, args]); return callClient(player, '__rpc:callBrowsers', [name, args]);
@ -311,10 +306,10 @@ export function callBrowsers(player: Player | string, name?: string | any, args?
switch(environment){ switch(environment){
case "client": case "client":
case "cef": 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"'); if(arguments.length !== 1 && arguments.length !== 2) return util.promiseReject('callBrowsers from the client or browser expects 1 or 2 arguments: "name" and optional "args"');
return _callBrowsers(null, player as string, name, {}); return _callBrowsers(null, player as string, name, {});
case "server": 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"'); if(arguments.length !== 2 && arguments.length !== 3) return util.promiseReject('callBrowsers from the server expects 2 or 3 arguments: "player", "name", and optional "args"');
return _callBrowsers(player as Player, name, args, {}); return _callBrowsers(player as Player, name, args, {});
} }
} }
@ -330,8 +325,8 @@ export function callBrowsers(player: Player | string, name?: string | any, args?
* @returns The result from the procedure. * @returns The result from the procedure.
*/ */
export function callBrowser(browser: Browser, name: string, args?: any): Promise<any> { 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(environment !== 'client') return util.promiseReject('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"'); if(arguments.length !== 2 && arguments.length !== 3) return util.promiseReject('callBrowser expects 2 or 3 arguments: "browser", "name", and optional "args"');
const id = util.uid(); const id = util.uid();
return _callBrowser(id, browser, name, args, {}); return _callBrowser(id, browser, name, args, {});
} }

View File

@ -20,6 +20,14 @@ export function parseData(data: string): any {
return JSON.parse(data); return JSON.parse(data);
} }
export function promiseResolve(result: any): Promise<any> {
return new Promise(resolve => setTimeout(() => resolve(result), 0));
}
export function promiseReject(error: any): Promise<any> {
return new Promise((_, reject) => setTimeout(() => reject(error), 0));
}
export function isBrowserValid(browser: Browser): boolean { export function isBrowserValid(browser: Browser): boolean {
try { try {
browser.url; browser.url;

View File

@ -32,6 +32,9 @@ module.exports = {
rules: [{ rules: [{
search: `exports.${LIBRARY_NAME}`, search: `exports.${LIBRARY_NAME}`,
replace: 'exports' replace: 'exports'
}, {
search: `exports["${LIBRARY_NAME}"]`,
replace: 'exports'
}] }]
}]) }])
] ]