Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

45 fix uncaught error #46

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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