Increase outgoing CEF return performance

This commit is contained in:
Micah Allen 2019-01-11 00:40:38 -05:00
parent 94ffe6c474
commit 92250e1f29
4 changed files with 33 additions and 47 deletions

File diff suppressed because one or more lines are too long

2
src/defs.d.ts vendored
View File

@ -10,6 +10,7 @@ declare interface Player {
} }
declare interface Browser { declare interface Browser {
url: string;
execute: (code: string) => void; execute: (code: string) => void;
[property: string]: any; [property: string]: any;
} }
@ -23,6 +24,7 @@ declare interface ProcedureListenerInfo {
declare interface Event { declare interface Event {
req?: number; req?: number;
ret?: number; ret?: number;
b?: string;
id: string; id: string;
name?: string; name?: string;
args?: any; args?: any;

View File

@ -5,6 +5,7 @@ if(!environment) throw 'Unknown RAGE environment';
const ERR_NOT_FOUND = 'PROCEDURE_NOT_FOUND'; const ERR_NOT_FOUND = 'PROCEDURE_NOT_FOUND';
const IDENTIFIER = '__rpc:id';
const PROCESS_EVENT = '__rpc:process'; const PROCESS_EVENT = '__rpc:process';
const PROCEDURE_EXISTS = '__rpc:exists'; const PROCEDURE_EXISTS = '__rpc:exists';
@ -30,63 +31,25 @@ if(!glob[PROCESS_EVENT]){
id: data.id, id: data.id,
env: environment env: environment
}; };
let ret: (ev: Event) => void;
switch(environment){ switch(environment){
case "server": { case "server":
promise.then(res => { ret = ev => info.player.call(PROCESS_EVENT, [util.stringifyData(ev)]);
info.player.call(PROCESS_EVENT, [util.stringifyData({
...part,
res
})]);
}).catch(err => {
info.player.call(PROCESS_EVENT, [util.stringifyData({
...part,
err
})]);
});
break; break;
}
case "client": { case "client": {
if(data.env === "server"){ if(data.env === "server"){
promise.then(res => { ret = ev => mp.events.callRemote(PROCESS_EVENT, util.stringifyData(ev));
mp.events.callRemote(PROCESS_EVENT, util.stringifyData({
...part,
res
}));
}).catch(err => {
mp.events.callRemote(PROCESS_EVENT, util.stringifyData({
...part,
err
}));
});
}else if(data.env === "cef"){ }else if(data.env === "cef"){
promise.then(res => { const browser = data.b && glob.__rpcBrowsers[data.b];
passEventToBrowsers({ ret = ev => browser && util.isBrowserValid(browser) && passEventToBrowser(browser, ev, true);
...part,
res
}, true);
}).catch(err => {
passEventToBrowsers({
...part,
err
}, true);
});
} }
break; break;
} }
case "cef": { case "cef": {
promise.then(res => { ret = ev => mp.trigger(PROCESS_EVENT, util.stringifyData(ev));
mp.trigger(PROCESS_EVENT, util.stringifyData({
...part,
res
}));
}).catch(err => {
mp.trigger(PROCESS_EVENT, util.stringifyData({
...part,
err
}));
});
} }
} }
if(ret) promise.then(res => ret({ ...part, res })).catch(err => ret({ ...part, err }));
}else if(data.ret){ // a previously called remote procedure has returned }else if(data.ret){ // a previously called remote procedure has returned
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;
@ -115,6 +78,19 @@ if(!glob[PROCESS_EVENT]){
fenv: info.environment fenv: info.environment
}); });
}); });
glob.__rpcBrowsers = {};
const initBrowser = (browser: Browser): void => {
const id = util.uid();
Object.keys(glob.__rpcBrowsers).forEach(key => {
if(glob.__rpcBrowsers[key] === browser) delete glob.__rpcBrowsers[key];
});
glob.__rpcBrowsers[id] = browser;
browser.execute(`window['${IDENTIFIER}'] = '${id}';`);
};
mp.browsers.forEach(initBrowser);
mp.events.add('browserCreated', initBrowser);
} }
} }
} }
@ -258,6 +234,7 @@ export function callClient(player: Player | string, name?: string | any, args?:
reject reject
}; };
const event: Event = { const event: Event = {
b: glob[IDENTIFIER],
req: 1, req: 1,
id, id,
name, name,

View File

@ -19,3 +19,10 @@ export function stringifyData(data: any): string {
export function parseData(data: string): any { export function parseData(data: string): any {
return JSON.parse(data); return JSON.parse(data);
} }
export function isBrowserValid(browser: Browser): boolean {
try {
browser.url;
}catch(e){ return false; }
return true;
}