Added: on/register return functions off/unregister

You can use:
const off = rpc.on('event', handler);
// do any actions
off();

Similarly:
const unregister = rpc.register('event', handler);
//do any actions
unregister();
This commit is contained in:
Pavel Torbeev 2019-12-18 22:48:40 +05:00
parent 4a08c2cbf7
commit a47449e7cb

View File

@ -143,11 +143,14 @@ function callProcedure(name: string, args: any, info: ProcedureListenerInfo): Pr
* Register a procedure. * Register a procedure.
* @param {string} name - The name of the procedure. * @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. * @param {function} cb - The procedure's callback. The return value will be sent back to the caller.
* @returns {Function} The function, which unregister the event.
*/ */
export function register(name: string, cb: ProcedureListener): void { export function register(name: string, cb: ProcedureListener): Function {
if(arguments.length !== 2) throw 'register expects 2 arguments: "name" and "cb"'; if(arguments.length !== 2) throw 'register expects 2 arguments: "name" and "cb"';
if(environment === "cef") glob[IDENTIFIER].then((id: string) => mp.trigger(BROWSER_REGISTER, JSON.stringify([id, name]))); if(environment === "cef") glob[IDENTIFIER].then((id: string) => mp.trigger(BROWSER_REGISTER, JSON.stringify([id, name])));
glob.__rpcListeners[name] = cb; glob.__rpcListeners[name] = cb;
return () => unregister(name);
} }
/** /**
@ -418,13 +421,16 @@ function callEvent(name: string, args: any, info: ProcedureListenerInfo){
* Register an event handler. * Register an event handler.
* @param {string} name - The name of the event. * @param {string} name - The name of the event.
* @param cb - The callback for the event. * @param cb - The callback for the event.
* @returns {Function} The function, which off the event.
*/ */
export function on(name: string, cb: ProcedureListener){ export function on(name: string, cb: ProcedureListener): Function {
if(arguments.length !== 2) throw 'on expects 2 arguments: "name" and "cb"'; if(arguments.length !== 2) throw 'on expects 2 arguments: "name" and "cb"';
const listeners = glob.__rpcEvListeners[name] || new Set(); const listeners = glob.__rpcEvListeners[name] || new Set();
listeners.add(cb); listeners.add(cb);
glob.__rpcEvListeners[name] = listeners; glob.__rpcEvListeners[name] = listeners;
return () => off(name, cb);
} }
/** /**