Skip to content

Commit

Permalink
move from sendToPopup to shared/messages
Browse files Browse the repository at this point in the history
Also add some `#region` comments for organization (VS Code only?)
  • Loading branch information
sidvishnoi committed Jul 4, 2024
1 parent 16e895a commit 9c3d64c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
1 change: 1 addition & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typecheck
prettiercache
corepack
linkcode
endregion

# packages and 3rd party tools/libraries
awilix
Expand Down
24 changes: 7 additions & 17 deletions src/background/services/sendToPopup.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -39,9 +29,9 @@ export class SendToPopup {
return this.isConnected
}

async send<T extends keyof BackgroundToPopupMessages>(
async send<T extends keyof BackgroundToPopupMessagesMap>(
type: T,
data: BackgroundToPopupMessages[T]
data: BackgroundToPopupMessagesMap[T]
) {
if (!this.isConnected) {
return
Expand Down
4 changes: 2 additions & 2 deletions src/popup/lib/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
35 changes: 29 additions & 6 deletions src/shared/messages.ts
Original file line number Diff line number Diff line change
@@ -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<TPayload = undefined> {
success: true
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -154,6 +160,7 @@ export type BackgroundToContentBackgroundMessage = {
}[BackgroundToContentAction]

export type ToContentMessage = BackgroundToContentBackgroundMessage
// #endregion

export class MessageManager<TMessages> {
constructor(private browser: Browser) {}
Expand Down Expand Up @@ -184,3 +191,19 @@ export class MessageManager<TMessages> {
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

0 comments on commit 9c3d64c

Please sign in to comment.