From 9c3d64c79acaef96dfdeadaeed7944bbcca352a4 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:55:30 +0530 Subject: [PATCH] move from sendToPopup to shared/messages Also add some `#region` comments for organization (VS Code only?) --- cspell-dictionary.txt | 1 + src/background/services/sendToPopup.ts | 24 ++++++------------ src/popup/lib/context.tsx | 4 +-- src/shared/messages.ts | 35 +++++++++++++++++++++----- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/cspell-dictionary.txt b/cspell-dictionary.txt index ae840d16..84d509a4 100644 --- a/cspell-dictionary.txt +++ b/cspell-dictionary.txt @@ -19,6 +19,7 @@ typecheck prettiercache corepack linkcode +endregion # packages and 3rd party tools/libraries awilix diff --git a/src/background/services/sendToPopup.ts b/src/background/services/sendToPopup.ts index b0a4ee76..ee21a149 100644 --- a/src/background/services/sendToPopup.ts +++ b/src/background/services/sendToPopup.ts @@ -1,19 +1,9 @@ -import type { AmountValue, Storage } from '@/shared/types' import type { Browser, Runtime } from 'webextension-polyfill' - -interface BackgroundToPopupMessages { - SET_BALANCE: Record<'recurring' | 'oneTime' | 'total', AmountValue> - SET_STATE: { state: Storage['state']; prevState: Storage['state'] } -} - -export type BackgroundToPopupMessage = { - [K in keyof BackgroundToPopupMessages]: { - type: K - data: BackgroundToPopupMessages[K] - } -}[keyof BackgroundToPopupMessages] - -export const CONNECTION_NAME = 'popup' +import { + BACKGROUND_TO_POPUP_CONNECTION_NAME as CONNECTION_NAME, + type BackgroundToPopupMessage, + type BackgroundToPopupMessagesMap +} from '@/shared/messages' export class SendToPopup { private isConnected = false @@ -39,9 +29,9 @@ export class SendToPopup { return this.isConnected } - async send( + async send( type: T, - data: BackgroundToPopupMessages[T] + data: BackgroundToPopupMessagesMap[T] ) { if (!this.isConnected) { return diff --git a/src/popup/lib/context.tsx b/src/popup/lib/context.tsx index 72bb9f0f..e656cad4 100644 --- a/src/popup/lib/context.tsx +++ b/src/popup/lib/context.tsx @@ -7,9 +7,9 @@ import { type ContentToBackgroundMessage } from '@/shared/messages' import { - CONNECTION_NAME, + BACKGROUND_TO_POPUP_CONNECTION_NAME as CONNECTION_NAME, type BackgroundToPopupMessage -} from '@/background/services/sendToPopup' +} from '@/shared/messages' export enum ReducerActionType { SET_DATA = 'SET_DATA', diff --git a/src/shared/messages.ts b/src/shared/messages.ts index ad16d468..3e337547 100644 --- a/src/shared/messages.ts +++ b/src/shared/messages.ts @@ -1,5 +1,6 @@ import type { WalletAddress, OutgoingPayment } from '@interledger/open-payments' -import { type Browser } from 'webextension-polyfill' +import type { Browser } from 'webextension-polyfill' +import type { AmountValue, Storage } from '@/shared/types' export interface SuccessResponse { success: true @@ -22,6 +23,7 @@ export type MessageHKT< ? { action: TAction } : { action: TAction; payload: TPayload } +// #region Popup ↦ BG export enum PopupToBackgroundAction { GET_CONTEXT_DATA = 'GET_CONTEXT_DATA', CONNECT_WALLET = 'CONNECT_WALLET', @@ -60,7 +62,9 @@ export type PopupToBackgroundMessage = { PopupToBackgroundActionPayload[K] > }[PopupToBackgroundAction] +// #endregion +// #region Content ↦ BG export enum ContentToBackgroundAction { CHECK_WALLET_ADDRESS_URL = 'CHECK_WALLET_ADDRESS_URL', START_MONETIZATION = 'START_MONETIZATION', @@ -107,7 +111,14 @@ export type ContentToBackgroundMessage = { ContentToBackgroundActionPayload[K] > }[ContentToBackgroundAction] +// #endregion +export type ToBackgroundMessage = + | PopupToBackgroundMessage + | ContentToBackgroundMessage + | BackgroundToContentMessage + +// #region BG ↦ Content export type BackgroundToContentMessage = { [K in BackgroundToContentAction]: MessageHKT< K, @@ -119,11 +130,6 @@ export interface BackgroundToContentActionPayload { [BackgroundToContentAction.MONETIZATION_EVENT]: MonetizationEventPayload } -export type ToBackgroundMessage = - | PopupToBackgroundMessage - | ContentToBackgroundMessage - | BackgroundToContentMessage - export enum BackgroundToContentAction { MONETIZATION_EVENT = 'MONETIZATION_EVENT', EMIT_TOGGLE_WM = 'EMIT_TOGGLE_WM' @@ -154,6 +160,7 @@ export type BackgroundToContentBackgroundMessage = { }[BackgroundToContentAction] export type ToContentMessage = BackgroundToContentBackgroundMessage +// #endregion export class MessageManager { constructor(private browser: Browser) {} @@ -184,3 +191,19 @@ export class MessageManager { return await this.browser.tabs.sendMessage(activeTab.id as number, message) } } + +// #region BG ↦ Popup +export interface BackgroundToPopupMessagesMap { + SET_BALANCE: Record<'recurring' | 'oneTime' | 'total', AmountValue> + SET_STATE: { state: Storage['state']; prevState: Storage['state'] } +} + +export type BackgroundToPopupMessage = { + [K in keyof BackgroundToPopupMessagesMap]: { + type: K + data: BackgroundToPopupMessagesMap[K] + } +}[keyof BackgroundToPopupMessagesMap] + +export const BACKGROUND_TO_POPUP_CONNECTION_NAME = 'popup' +// #endregion