Skip to content

Commit 6f62fb0

Browse files
committed
fix handshake reply
1 parent 4ba8808 commit 6f62fb0

File tree

5 files changed

+30
-39
lines changed

5 files changed

+30
-39
lines changed

src/guest.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ function connect(schema: Schema = {}, eventHandlers?: EventHandlers): Promise<Co
1414
if (eventData?.action !== actions.HANDSHAKE_REPLY) return;
1515

1616
// register local methods
17-
const unregisterLocal = registerLocalMethods(schema, localMethods, eventData.connectionID, listenTo, sendTo);
17+
const unregisterLocal = registerLocalMethods(localMethods, eventData.connectionID, listenTo, sendTo);
1818

1919
// register remote methods
2020
const { remote, unregisterRemote } = registerRemoteMethods(
2121
eventData.schema,
22-
eventData.methods,
22+
eventData.methodNames,
2323
eventData.connectionID,
2424
event,
2525
listenTo,
26-
sendTo,
26+
sendTo
2727
);
2828

2929
await eventHandlers?.onConnectionSetup?.(remote);
@@ -53,8 +53,8 @@ function connect(schema: Schema = {}, eventHandlers?: EventHandlers): Promise<Co
5353

5454
const payload = {
5555
action: actions.HANDSHAKE_REQUEST,
56-
methods: localMethods,
57-
schema: JSON.parse(JSON.stringify(schema)),
56+
methodNames: Object.keys(localMethods),
57+
schema: schema,
5858
};
5959

6060
postMessageToTarget(sendTo, payload);

src/helpers.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ export function isIframe() {
3434
* @param obj
3535
*/
3636
export function extractMethods(obj: any) {
37-
const paths: string[] = [];
37+
const methods: Record<string, (...args: any) => any> = {};
3838
(function parse(obj: any, path = "") {
3939
Object.keys(obj).forEach((prop) => {
4040
const propPath = path ? `${path}.${prop}` : prop;
4141
if (obj[prop] === Object(obj[prop])) {
4242
parse(obj[prop], propPath);
4343
}
4444
if (typeof obj[prop] === "function") {
45-
paths.push(propPath);
45+
methods[propPath] = obj[prop];
46+
delete obj[prop];
4647
}
4748
});
4849
})(obj);
49-
return paths;
50+
return methods;
5051
}
5152

5253
const urlRegex = /^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/;

src/host.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,24 @@ function connect(guest: Guest, schema: Schema = {}): Promise<Connection> {
6666

6767
// register local methods
6868
const localMethods = extractMethods(schema);
69-
const unregisterLocal = registerLocalMethods(schema, localMethods, connectionID, listenTo, sendTo);
69+
const unregisterLocal = registerLocalMethods(localMethods, connectionID, listenTo, sendTo);
7070

7171
// register remote methods
7272
const { remote, unregisterRemote } = registerRemoteMethods(
7373
eventData.schema,
74-
eventData.methods,
74+
eventData.methodNames,
7575
connectionID,
7676
event,
7777
listenTo,
78-
sendTo,
78+
sendTo
7979
);
8080

8181
// send a HANDSHAKE REPLY to the guest
8282
const payload = {
8383
action: actions.HANDSHAKE_REPLY,
8484
connectionID,
85-
schema,
86-
methods: localMethods,
85+
schema: schema,
86+
methodNames: Object.keys(localMethods),
8787
};
8888

8989
postMessageToTarget(sendTo, payload, event.origin);

src/rpc.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
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";
102
import {
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
*/
3022
export 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
*/
152142
export 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,

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export interface RimlessEvent extends EventListener {
3838
export interface HandshakeRequestPayload {
3939
action: actions.HANDSHAKE_REQUEST;
4040
connectionID: string;
41-
methods: string[];
41+
methodNames: string[];
4242
schema: Schema;
4343
}
4444

4545
export interface HandshakeConfirmationPayload {
4646
action: actions.HANDSHAKE_REPLY;
4747
connectionID: string;
48-
methods: string[];
48+
methodNames: string[];
4949
schema: Schema;
5050
}
5151

0 commit comments

Comments
 (0)