Add noRet option

This commit is contained in:
micah 2019-06-02 13:12:11 -04:00
parent 18deb4d908
commit d0fbc6993d
4 changed files with 241 additions and 65 deletions

19
dist/rage-rpc.d.ts vendored
View File

@ -2,13 +2,13 @@ export as namespace rpc;
export function register(name: string, cb: ProcedureListener): void; export function register(name: string, cb: ProcedureListener): void;
export function unregister(name: string): void; export function unregister(name: string): void;
export function call<T = any>(name: string, args?: any): Promise<T>; export function call<T = any>(name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callServer<T = any>(name: string, args?: any): Promise<T>; export function callServer<T = any>(name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callClient<T = any>(player: Player, name: string, args?: any): Promise<T>; export function callClient<T = any>(player: Player, name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callClient<T = any>(name: string, args?: any): Promise<T>; export function callClient<T = any>(name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callBrowsers<T = any>(player: Player, name: string, args?: any): Promise<T>; export function callBrowsers<T = any>(player: Player, name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callBrowsers<T = any>(name: string, args?: any): Promise<T>; export function callBrowsers<T = any>(name: string, args?: any, options: CallOptions = {}): Promise<T>;
export function callBrowser<T = any>(browser: Browser, name: string, args?: any): Promise<T>; export function callBrowser<T = any>(browser: Browser, name: string, args?: any, options: CallOptions = {}): Promise<T>;
export interface Player { export interface Player {
call: (eventName: string, args?: any[]) => void; call: (eventName: string, args?: any[]) => void;
@ -26,4 +26,9 @@ export interface ProcedureListenerInfo {
player?: Player; player?: Player;
} }
export interface CallOptions {
timeout?: number;
noRet?: boolean;
}
export type ProcedureListener = (args: any, info: ProcedureListenerInfo) => any; export type ProcedureListener = (args: any, info: ProcedureListenerInfo) => any;

136
dist/rage-rpc.min.js vendored

File diff suppressed because one or more lines are too long

2
src/defs.d.ts vendored
View File

@ -24,6 +24,7 @@ declare interface ProcedureListenerInfo {
declare interface CallOptions { declare interface CallOptions {
timeout?: number; timeout?: number;
noRet?: boolean;
} }
declare interface Event { declare interface Event {
@ -37,4 +38,5 @@ declare interface Event {
fenv?: string; fenv?: string;
res?: any; res?: any;
err?: any; err?: any;
noRet?: number;
} }

View File

@ -50,7 +50,10 @@ if(!glob[PROCESS_EVENT]){
ret = ev => mp.trigger(PROCESS_EVENT, util.stringifyData(ev)); ret = ev => mp.trigger(PROCESS_EVENT, util.stringifyData(ev));
} }
} }
if(ret) callProcedure(data.name, data.args, info).then(res => ret({ ...part, res })).catch(err => ret({ ...part, err })); if(ret){
const promise = callProcedure(data.name, data.args, info);
if(!data.noRet) 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;
@ -66,8 +69,8 @@ if(!glob[PROCESS_EVENT]){
if(environment === "client"){ if(environment === "client"){
// set up internal pass-through events // set up internal pass-through events
register('__rpc:callServer', ([name, args], info) => _callServer(name, args, { fenv: info.environment })); register('__rpc:callServer', ([name, args, noRet], info) => _callServer(name, args, { fenv: info.environment, noRet }));
register('__rpc:callBrowsers', ([name, args], info) => _callBrowsers(null, name, args, { fenv: info.environment })); register('__rpc:callBrowsers', ([name, args, noRet], info) => _callBrowsers(null, name, args, { fenv: info.environment, noRet }));
// set up browser identifiers // set up browser identifiers
glob.__rpcBrowsers = {}; glob.__rpcBrowsers = {};
@ -161,7 +164,7 @@ export function call(name: string, args?: any, options: CallOptions = {}): Promi
return util.promiseTimeout(callProcedure(name, args, { environment }), options.timeout); return util.promiseTimeout(callProcedure(name, args, { environment }), options.timeout);
} }
function _callServer(name: string, args?: any, extraData = {}): Promise<any> { function _callServer(name: string, args?: any, extraData: any = {}): Promise<any> {
switch(environment){ switch(environment){
case "server": { case "server": {
return call(name, args); return call(name, args);
@ -169,9 +172,11 @@ 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 => { return new Promise(resolve => {
if(!extraData.noRet){
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve resolve
}; };
}
const event: Event = { const event: Event = {
req: 1, req: 1,
id, id,
@ -184,7 +189,7 @@ function _callServer(name: string, args?: any, extraData = {}): Promise<any> {
}); });
} }
case "cef": { case "cef": {
return callClient('__rpc:callServer', [name, args]); return callClient('__rpc:callServer', [name, args, +extraData.noRet]);
} }
} }
} }
@ -201,7 +206,61 @@ function _callServer(name: string, args?: any, extraData = {}): Promise<any> {
*/ */
export function callServer(name: string, args?: any, options: CallOptions = {}): Promise<any> { export function callServer(name: string, args?: any, options: CallOptions = {}): Promise<any> {
if(arguments.length < 1 || arguments.length > 3) return util.promiseReject('callServer expects 1 to 3 arguments: "name", optional "args", and optional "options"'); if(arguments.length < 1 || arguments.length > 3) return util.promiseReject('callServer expects 1 to 3 arguments: "name", optional "args", and optional "options"');
return util.promiseTimeout(_callServer(name, args, {}), options.timeout);
let extraData: any = {};
if(options.noRet) extraData.noRet = 1;
return util.promiseTimeout(_callServer(name, args, extraData), options.timeout);
}
function _callClient(player: Player, name: string, args?: any, extraData: any = {}): Promise<any> {
switch(environment){
case 'client': {
return call(name, args);
}
case 'server': {
const id = util.uid();
return new Promise(resolve => {
if(!extraData.noRet){
glob.__rpcPending[id] = {
resolve,
player
};
}
const event: Event = {
req: 1,
id,
name,
env: environment,
args,
...extraData
};
player.call(PROCESS_EVENT, [util.stringifyData(event)]);
});
}
case 'cef': {
const id = util.uid();
return glob[IDENTIFIER].then((browserId: string) => {
return new Promise(resolve => {
if(!extraData.noRet){
glob.__rpcPending[id] = {
resolve
};
}
const event: Event = {
b: browserId,
req: 1,
id,
name,
env: environment,
args,
...extraData
};
mp.trigger(PROCESS_EVENT, util.stringifyData(event));
});
});
}
}
} }
/** /**
@ -216,72 +275,42 @@ export function callServer(name: string, args?: any, options: CallOptions = {}):
* @returns The result from the procedure. * @returns The result from the procedure.
*/ */
export function callClient(player: Player | string, name?: string | any, args?: any, options: CallOptions = {}): Promise<any> { export function callClient(player: Player | string, name?: string | any, args?: any, options: CallOptions = {}): Promise<any> {
let promise;
switch(environment){ switch(environment){
case 'client': { case 'client': {
options = args || {}; options = args || {};
args = name; args = name;
name = player; name = player;
player = null;
if((arguments.length < 1 || arguments.length > 3) || typeof name !== 'string') return util.promiseReject('callClient from the client expects 1 to 3 arguments: "name", optional "args", and optional "options"'); if((arguments.length < 1 || arguments.length > 3) || typeof name !== 'string') return util.promiseReject('callClient from the client expects 1 to 3 arguments: "name", optional "args", and optional "options"');
promise = call(name, args);
break; break;
} }
case 'server': { case 'server': {
if((arguments.length < 2 || arguments.length > 4) || typeof player !== 'object') return util.promiseReject('callClient from the server expects 2 to 4 arguments: "player", "name", optional "args", and optional "options"'); if((arguments.length < 2 || arguments.length > 4) || typeof player !== 'object') return util.promiseReject('callClient from the server expects 2 to 4 arguments: "player", "name", optional "args", and optional "options"');
const id = util.uid();
promise = new Promise(resolve => {
glob.__rpcPending[id] = {
resolve,
player
};
const event: Event = {
req: 1,
id,
name,
env: environment,
args
};
player.call(PROCESS_EVENT, [util.stringifyData(event)]);
});
break; break;
} }
case 'cef': { case 'cef': {
options = args || {}; options = args || {};
args = name; args = name;
name = player; name = player;
player = null;
if((arguments.length < 1 || arguments.length > 3) || typeof name !== 'string') return util.promiseReject('callClient from the browser expects 1 to 3 arguments: "name", optional "args", and optional "options"'); if((arguments.length < 1 || arguments.length > 3) || typeof name !== 'string') return util.promiseReject('callClient from the browser expects 1 to 3 arguments: "name", optional "args", and optional "options"');
const id = util.uid();
promise = glob[IDENTIFIER].then((browserId: string) => {
return new Promise(resolve => {
glob.__rpcPending[id] = {
resolve
};
const event: Event = {
b: browserId,
req: 1,
id,
name,
env: environment,
args
};
mp.trigger(PROCESS_EVENT, util.stringifyData(event));
});
});
break; break;
} }
} }
if(promise){ let extraData: any = {};
return util.promiseTimeout(promise, options.timeout); if(options.noRet) extraData.noRet = 1;
}
return util.promiseTimeout(_callClient(typeof player === 'object' ? player : null, name, args, extraData), options.timeout);
} }
function _callBrowser(id: string, browser: Browser, name: string, args?: any, extraData = {}): Promise<any> { function _callBrowser(id: string, browser: Browser, name: string, args?: any, extraData: any = {}): Promise<any> {
return new Promise(resolve => { return new Promise(resolve => {
if(!extraData.noRet){
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve resolve
}; };
}
passEventToBrowser(browser, { passEventToBrowser(browser, {
req: 1, req: 1,
id, id,
@ -293,7 +322,7 @@ function _callBrowser(id: string, browser: Browser, name: string, args?: any, ex
}); });
} }
function _callBrowsers(player: Player, name: string, args?: any, extraData = {}): Promise<any> { function _callBrowsers(player: Player, name: string, args?: any, extraData: any = {}): Promise<any> {
switch(environment){ switch(environment){
case 'client': case 'client':
const id = util.uid(); const id = util.uid();
@ -303,9 +332,9 @@ function _callBrowsers(player: Player, name: string, args?: any, extraData = {})
if(!browser || !util.isBrowserValid(browser)) return util.promiseReject(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, +extraData.noRet], extraData);
case 'cef': case 'cef':
return callClient('__rpc:callBrowsers', [name, args]); return _callClient(null, '__rpc:callBrowsers', [name, args, +extraData.noRet], extraData);
} }
} }
@ -322,6 +351,7 @@ function _callBrowsers(player: Player, name: string, args?: any, extraData = {})
*/ */
export function callBrowsers(player: Player | string, name?: string | any, args?: any, options: CallOptions = {}): Promise<any> { export function callBrowsers(player: Player | string, name?: string | any, args?: any, options: CallOptions = {}): Promise<any> {
let promise; let promise;
let extraData: any = {};
switch(environment){ switch(environment){
case 'client': case 'client':
@ -330,11 +360,13 @@ export function callBrowsers(player: Player | string, name?: string | any, args?
args = name; args = name;
name = player; name = player;
if(arguments.length < 1 || arguments.length > 3) return util.promiseReject('callBrowsers from the client or browser expects 1 to 3 arguments: "name", optional "args", and optional "options"'); if(arguments.length < 1 || arguments.length > 3) return util.promiseReject('callBrowsers from the client or browser expects 1 to 3 arguments: "name", optional "args", and optional "options"');
promise = _callBrowsers(null, name, args, {}); if(options.noRet) extraData.noRet = 1;
promise = _callBrowsers(null, name, args, extraData);
break; break;
case 'server': case 'server':
if(arguments.length < 2 || arguments.length > 4) return util.promiseReject('callBrowsers from the server expects 2 to 4 arguments: "player", "name", optional "args", and optional "options"'); if(arguments.length < 2 || arguments.length > 4) return util.promiseReject('callBrowsers from the server expects 2 to 4 arguments: "player", "name", optional "args", and optional "options"');
promise = _callBrowsers(player as Player, name, args, {}); if(options.noRet) extraData.noRet = 1;
promise = _callBrowsers(player as Player, name, args, extraData);
break; break;
} }
@ -359,7 +391,10 @@ export function callBrowser(browser: Browser, name: string, args?: any, options:
if(arguments.length < 2 || arguments.length > 4) return util.promiseReject('callBrowser expects 2 to 4 arguments: "browser", "name", optional "args", and optional "options"'); if(arguments.length < 2 || arguments.length > 4) return util.promiseReject('callBrowser expects 2 to 4 arguments: "browser", "name", optional "args", and optional "options"');
const id = util.uid(); const id = util.uid();
return util.promiseTimeout(_callBrowser(id, browser, name, args, {}), options.timeout); let extraData: any = {};
if(options.noRet) extraData.noRet = 1;
return util.promiseTimeout(_callBrowser(id, browser, name, args, extraData), options.timeout);
} }
export default { export default {