Skip to content

Commit 83919f8

Browse files
committed
Part 2
1 parent 6a3e3ac commit 83919f8

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

source/receiver.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ export function isMessengerMessage(message: unknown): message is Message {
2626
type SendResponse = (response: unknown) => void;
2727

2828
/** Thin Promise-to-sendResponse converter. Don't add logic here */
29-
function adaptResponse(
29+
async function adaptResponse(
3030
sendResponse: SendResponse,
3131
response: Promise<unknown>,
32-
): true {
33-
response.then(sendResponse, (error) => {
34-
// Errors should be already wrapped by this point. If `response` rejects it's a bug in the Messenger
35-
sendResponse({
36-
error: serializeError(
37-
new MessengerError("Internal Error", { cause: error }),
38-
),
39-
});
40-
});
41-
return true;
32+
): Promise<void> {
33+
try {
34+
sendResponse(await response);
35+
} catch (error) {
36+
sendResponse({ __webextMessenger: true, error: serializeError(error) });
37+
}
4238
}
4339

4440
function onMessageListener(
@@ -62,7 +58,8 @@ function onMessageListener(
6258
return;
6359
}
6460

65-
return adaptResponse(sendResponse, handleMessage(message, sender, action));
61+
void adaptResponse(sendResponse, handleMessage(message, sender, action));
62+
return true;
6663
}
6764

6865
// This function can only be called when the message *will* be handled locally.

source/test/contentscript/fixtures/unrelatedMessageListener.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ chrome.runtime.onMessage.addListener(
77
{ message },
88
);
99

10-
void Promise.resolve("User is on Windows 95").then(sendResponse);
10+
void Promise.resolve("Buonanotte").then(sendResponse);
1111

1212
return true;
1313
}

0 commit comments

Comments
 (0)