1- import {
2- addEventListener ,
3- generateId ,
4- get ,
5- getEventData ,
6- postMessageToTarget ,
7- removeEventListener ,
8- set ,
9- } from "./helpers" ;
1+ import { addEventListener , generateId , getEventData , postMessageToTarget , removeEventListener , set } from "./helpers" ;
102import {
113 actions ,
124 Environment ,
@@ -19,24 +11,22 @@ import {
1911} from "./types" ;
2012
2113/**
22- * for each function in the schema
14+ * for each function in methods
2315 * 1. subscribe to an event that the remote can call
2416 * 2. listen for calls from the remote. When called execute the function and emit the results.
2517 *
26- * @param methods an array of method ids from the local schema
18+ * @param methods an object of method ids : methods from the local schema
2719 * @param rpcConnectionID
2820 * @return a function to cancel all subscriptions
2921 */
3022export function registerLocalMethods (
31- schema : Schema = { } ,
32- methods : string [ ] = [ ] ,
23+ methods : Record < string , ( ...args : any [ ] ) => any > = { } ,
3324 rpcConnectionID : string ,
3425 listenTo : Environment ,
35- sendTo : Target ,
26+ sendTo : Target
3627) {
3728 const listeners : any [ ] = [ ] ;
38-
39- methods . forEach ( ( methodName ) => {
29+ for ( const [ methodName , method ] of Object . entries ( methods ) ) {
4030 // handle a remote calling a local method
4131 async function handleCall ( event : any ) {
4232 const eventData = getEventData ( event ) ;
@@ -58,7 +48,7 @@ export function registerLocalMethods(
5848
5949 // run function and return the results to the remote
6050 try {
61- const result = await get ( schema , methodName ) ( ...args ) ;
51+ const result = await method ( ...args ) ;
6252
6353 if ( ! result ) {
6454 // if the result is falsy (null, undefined, "", etc), set it directly
@@ -78,7 +68,7 @@ export function registerLocalMethods(
7868 // subscribe to the call event
7969 addEventListener ( listenTo , events . MESSAGE , handleCall ) ;
8070 listeners . push ( ( ) => removeEventListener ( listenTo , events . MESSAGE , handleCall ) ) ;
81- } ) ;
71+ }
8272
8373 return ( ) => listeners . forEach ( ( unregister ) => unregister ( ) ) ;
8474}
@@ -101,7 +91,7 @@ export function createRPC(
10191 event : RimlessEvent ,
10292 listeners : Array < ( ) => void > = [ ] ,
10393 listenTo : Environment ,
104- sendTo : Target ,
94+ sendTo : Target
10595) {
10696 return ( ...args : any ) => {
10797 return new Promise ( ( resolve , reject ) => {
@@ -140,7 +130,7 @@ export function createRPC(
140130}
141131
142132/**
143- * create an object based on the remote schema and methods. Functions in that object will
133+ * create an object based on the remote schema's methods. Functions in that object will
144134 * emit an event that will trigger the RPC on the remote.
145135 *
146136 * @param schema
@@ -151,19 +141,19 @@ export function createRPC(
151141 */
152142export function registerRemoteMethods (
153143 schema : Schema = { } ,
154- methods : string [ ] = [ ] ,
144+ methodNames : Iterable < string > = [ ] ,
155145 connectionID : string ,
156146 event : RimlessEvent ,
157147 listenTo : Environment ,
158- sendTo : Target ,
148+ sendTo : Target
159149) {
160150 const remote = { ...schema } ;
161151 const listeners : Array < ( ) => void > = [ ] ;
162152
163- methods . forEach ( ( methodName ) => {
153+ for ( const methodName of methodNames ) {
164154 const rpc = createRPC ( methodName , connectionID , event , listeners , listenTo , sendTo ) ;
165155 set ( remote , methodName , rpc ) ;
166- } ) ;
156+ }
167157
168158 return {
169159 remote,
0 commit comments