Skip to content

Commit

Permalink
feat: added sending with frameId to foreground messenger
Browse files Browse the repository at this point in the history
  • Loading branch information
sghsri committed Mar 21, 2024
1 parent 40a98be commit 89c0f43
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/messaging/createMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import { MessageEndpoint, Message, MessageData, MessageResponse } from 'src/type
: (data: MessageData<M, K>) => Promise<MessageResponse<M, K>>;
};

/**
* Where the foreground message is being sent to specifically (which tab or frame)
*/
type ForegroundMessageOptions =
| {
tabId: number;
frameId?: number;
}
| {
tabId: 'ALL';
};

/**
* an object that can be used to send messages to the foreground (tabs OR extension pages (popup, options, etc.))
*/
export type ForegroundMessenger<M> = {
[K in keyof M]: (data: MessageData<M, K>, tab: number | 'ALL') => Promise<MessageResponse<M, K>>;
[K in keyof M]: (data: MessageData<M, K>, options: ForegroundMessageOptions) => Promise<MessageResponse<M, K>>;
};

/**
Expand Down Expand Up @@ -74,7 +86,7 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {
const sender = new Proxy({} as any, {
get(target, prop) {
const name = prop as keyof M;
return async (data: MessageData<M, any>, dest?: number | 'ALL') =>
return async (data: MessageData<M, any>, options?: ForegroundMessageOptions) =>
new Promise((resolve, reject) => {
const message: Message<M> = {
name,
Expand All @@ -83,12 +95,18 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {
to,
};

if (to === MessageEndpoint.FOREGROUND && dest) {
if (to === MessageEndpoint.FOREGROUND && options) {
// for messages sent to the tabs, we want to send to the tabs using chrome.tabs.sendMessage,
if (typeof dest === 'number') {
return chrome.tabs.sendMessage(dest, message, onMessageResponse(resolve, reject));
const { tabId } = options;
if (typeof tabId === 'number') {
return chrome.tabs.sendMessage(
tabId,
message,
{ frameId: options.frameId },
onMessageResponse(resolve, reject)
);
}
if (dest === 'ALL') {
if (tabId === 'ALL') {
return sendTabMessageToAllTabs(message);
}
}
Expand Down

0 comments on commit 89c0f43

Please sign in to comment.