Skip to content

Commit

Permalink
reverting response on foreground
Browse files Browse the repository at this point in the history
  • Loading branch information
sghsri committed May 5, 2024
1 parent b511fb6 commit 7edd3ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-extension-toolkit",
"version": "0.0.93",
"version": "0.0.94",
"description": "A template for creating npm packages using TypeScript and VSCode",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 9 additions & 4 deletions src/messaging/createMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {

async function sendTabMessageToAllTabs(message: Message<M>) {
const tabs = (await chrome.tabs.query({})).filter(tab => tab.id !== undefined && tab.url);
const promises: Promise<void>[] = [];

return Promise.all([
await Promise.all([
...tabs.map(tab => chrome.tabs.sendMessage(tab.id!, message)),
chrome.runtime.sendMessage(message),
]);

return Promise.all(promises);
}

const sender = new Proxy({} as any, {
get(target, prop) {
const name = prop as keyof M;
return async (data: MessageData<M, any>, options?: ForegroundMessageOptions) =>
new Promise((resolve, reject) => {
new Promise<void>((resolve, reject) => {
const message: Message<M> = {
name,
data,
Expand All @@ -87,10 +90,12 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {
// for messages sent to the tabs, we want to send to the tabs using chrome.tabs.sendMessage,
const { tabId } = options;
if (typeof tabId === 'number') {
return chrome.tabs.sendMessage(tabId, message, { frameId: options.frameId });
return chrome.tabs
.sendMessage(tabId, message, { frameId: options.frameId })
.then(() => resolve());
}
if (tabId === 'ALL') {
return sendTabMessageToAllTabs(message);
return sendTabMessageToAllTabs(message).then(() => resolve());
}
}
return chrome.runtime.sendMessage(message, onMessageResponse(resolve, reject));
Expand Down

1 comment on commit 7edd3ec

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 8%
7.94% (27/340) 12.22% (11/90) 8.23% (7/85)

Please sign in to comment.