Skip to content

Commit 8bdf3b8

Browse files
committed
Add types for the flag which determines if a response is going to be sent or not.
1 parent 2334895 commit 8bdf3b8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

browser-extension/src/entrypoints/background.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
isContentToBackgroundMessage,
66
isGetOpenSpotsMessage,
77
isSwitchToTabMessage,
8+
CLOSE_MESSAGE_PORT,
9+
KEEP_PORT_OPEN,
810
} from '../lib/messages'
911

1012
export interface Tab {
@@ -23,7 +25,7 @@ export interface CommentState {
2325

2426
export const openSpots = new JsonMap<TabAndSpot, CommentState>()
2527

26-
export function handleCommentEvent(message: CommentEvent, sender: any): void {
28+
export function handleCommentEvent(message: CommentEvent, sender: any): boolean {
2729
if (
2830
(message.type === 'ENHANCED' || message.type === 'DESTROYED') &&
2931
sender.tab?.id &&
@@ -33,12 +35,10 @@ export function handleCommentEvent(message: CommentEvent, sender: any): void {
3335
tabId: sender.tab.id,
3436
windowId: sender.tab.windowId,
3537
}
36-
3738
const tabAndSpot: TabAndSpot = {
3839
spot: message.spot,
3940
tab,
4041
}
41-
4242
if (message.type === 'ENHANCED') {
4343
const commentState: CommentState = {
4444
drafts: [],
@@ -48,22 +48,26 @@ export function handleCommentEvent(message: CommentEvent, sender: any): void {
4848
openSpots.set(tabAndSpot, commentState)
4949
} else if (message.type === 'DESTROYED') {
5050
openSpots.delete(tabAndSpot)
51+
} else {
52+
throw new Error(`Unhandled comment event type: ${message.type}`)
5153
}
5254
}
55+
return CLOSE_MESSAGE_PORT
5356
}
5457

5558
export function handlePopupMessage(
5659
message: any,
5760
_sender: any,
5861
sendResponse: (response: any) => void,
59-
): void {
62+
): typeof CLOSE_MESSAGE_PORT | typeof KEEP_PORT_OPEN {
6063
if (isGetOpenSpotsMessage(message)) {
6164
const spots: CommentState[] = []
6265
for (const [, commentState] of openSpots) {
6366
spots.push(commentState)
6467
}
6568
const response: GetOpenSpotsResponse = { spots }
6669
sendResponse(response)
70+
return KEEP_PORT_OPEN
6771
} else if (isSwitchToTabMessage(message)) {
6872
browser.windows
6973
.update(message.windowId, { focused: true })
@@ -73,17 +77,18 @@ export function handlePopupMessage(
7377
.catch((error) => {
7478
console.error('Error switching to tab:', error)
7579
})
80+
return CLOSE_MESSAGE_PORT
81+
} else {
82+
throw new Error(`Unhandled popup message type: ${message?.type || 'unknown'}`)
7683
}
7784
}
7885

7986
export default defineBackground(() => {
8087
browser.runtime.onMessage.addListener((message: ToBackgroundMessage, sender, sendResponse) => {
8188
if (isContentToBackgroundMessage(message)) {
82-
handleCommentEvent(message, sender)
83-
return false
89+
return handleCommentEvent(message, sender)
8490
} else {
85-
handlePopupMessage(message, sender, sendResponse)
86-
return true
91+
return handlePopupMessage(message, sender, sendResponse)
8792
}
8893
})
8994
})

browser-extension/src/lib/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { CommentDraft, CommentEvent, CommentSpot } from './enhancer'
22

3+
// Message handler response types
4+
export const CLOSE_MESSAGE_PORT = false as const // No response will be sent
5+
export const KEEP_PORT_OPEN = true as const // Response will be sent (possibly async)
6+
37
// Content -> Background messages (already well-typed as CommentEvent)
48
export type ContentToBackgroundMessage = CommentEvent
59

0 commit comments

Comments
 (0)