55 isContentToBackgroundMessage ,
66 isGetOpenSpotsMessage ,
77 isSwitchToTabMessage ,
8+ CLOSE_MESSAGE_PORT ,
9+ KEEP_PORT_OPEN ,
810} from '../lib/messages'
911
1012export interface Tab {
@@ -23,7 +25,7 @@ export interface CommentState {
2325
2426export 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
5558export 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
7986export 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} )
0 commit comments