Skip to content

Commit

Permalink
45 fix uncaught error (#46)
Browse files Browse the repository at this point in the history
* default to empty object

* fix rpc
  • Loading branch information
au-re authored Nov 30, 2024
1 parent c713aee commit 59d874e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateId, get, isNodeEnv, isWorker, set } from "./helpers";
import { generateId, get, isNodeEnv, isWorker, set, addEventListener, removeEventListener, getEventData } from "./helpers";
import { actions, events, IRPCRequestPayload, IRPCResolvePayload, ISchema } from "./types";

/**
Expand All @@ -20,7 +20,8 @@ export function registerLocalMethods(
methods.forEach((methodName) => {
// handle a remote calling a local method
async function handleCall(event: any) {
const { action, callID, connectionID, callName, args = [] } = event.data as IRPCRequestPayload;
const eventData = getEventData(event);
const { action, callID, connectionID, callName, args = [] } = eventData as IRPCRequestPayload;

if (action !== actions.RPC_REQUEST) return;
if (!callID || !callName) return;
Expand All @@ -39,7 +40,7 @@ export function registerLocalMethods(
// run function and return the results to the remote
try {
const result = await get(schema, methodName)(...args);
payload.result = JSON.parse(JSON.stringify(result));
payload.result = JSON.parse(JSON.stringify(result || {}));
} catch (error) {
payload.action = actions.RPC_REJECT;
payload.error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));
Expand All @@ -51,10 +52,13 @@ export function registerLocalMethods(
}

// subscribe to the call event
if (guest) guest.addEventListener(events.MESSAGE, handleCall);
else self.addEventListener(events.MESSAGE, handleCall);

listeners.push(() => self.removeEventListener(events.MESSAGE, handleCall));
if (guest) {
addEventListener(guest, events.MESSAGE, handleCall);
listeners.push(() => removeEventListener(guest, events.MESSAGE, handleCall));
} else {
addEventListener(self, events.MESSAGE, handleCall);
listeners.push(() => removeEventListener(self, events.MESSAGE, handleCall));
}
});

return () => listeners.forEach((unregister) => unregister());
Expand Down Expand Up @@ -85,7 +89,8 @@ export function createRPC(

// on RPC response
function handleResponse(event: any) {
const { callID, connectionID, callName, result, error, action } = event.data as IRPCResolvePayload;
const eventData = getEventData(event);
const { callID, connectionID, callName, result, error, action } = eventData as IRPCResolvePayload;

if (!callID || !callName) return;
if (callName !== _callName) return;
Expand All @@ -105,12 +110,12 @@ export function createRPC(
connectionID: _connectionID,
};

if (guest || isNodeEnv()) {
guest?.addEventListener(events.MESSAGE, handleResponse);
listeners.push(() => guest?.removeEventListener(events.MESSAGE, handleResponse));
if (guest) {
addEventListener(guest, events.MESSAGE, handleResponse);
listeners.push(() => removeEventListener(guest, events.MESSAGE, handleResponse));
} else {
self.addEventListener(events.MESSAGE, handleResponse);
listeners.push(() => self.removeEventListener(events.MESSAGE, handleResponse));
addEventListener(self, events.MESSAGE, handleResponse);
listeners.push(() => removeEventListener(self, events.MESSAGE, handleResponse));
}

if (guest) guest.postMessage(payload);
Expand Down

0 comments on commit 59d874e

Please sign in to comment.