@@ -15,7 +15,6 @@ export interface Tab {
1515  windowId : number 
1616} 
1717export  interface  CommentStorage  { 
18-   tab : Tab 
1918  spot : CommentSpot 
2019  drafts : [ number ,  string ] [ ] 
2120  sentOn : number  |  null 
@@ -35,6 +34,7 @@ export interface CommentTableRow {
3534} 
3635
3736export  const  openSpots  =  new  Map < string ,  CommentStorage > ( ) 
37+ export  const  openTabs  =  new  Map < string ,  Array < Tab > > ( ) 
3838
3939export  function  handleCommentEvent ( message : CommentEvent ,  sender : any ) : boolean  { 
4040  logger . debug ( 'received comment event' ,  message ) 
@@ -46,17 +46,30 @@ export function handleCommentEvent(message: CommentEvent, sender: any): boolean
4646
4747  switch  ( message . type )  { 
4848    case  'ENHANCED' : { 
49-       const  commentState : CommentStorage  =  { 
50-         drafts : [ [ Date . now ( ) ,  message . draft  ||  '' ] ] , 
51-         sentOn : null , 
52-         spot : message . spot , 
53-         tab : { 
54-           tabId : sender . tab . id , 
55-           windowId : sender . tab . windowId , 
56-         } , 
57-         trashedOn : null , 
49+       // track the draft 
50+       const  existingState  =  openSpots . get ( message . spot . unique_key ) 
51+       if  ( existingState )  { 
52+         existingState . drafts . push ( [ Date . now ( ) ,  message . draft  ||  '' ] ) 
53+       }  else  { 
54+         const  commentState : CommentStorage  =  { 
55+           drafts : [ [ Date . now ( ) ,  message . draft  ||  '' ] ] , 
56+           sentOn : null , 
57+           spot : message . spot , 
58+           trashedOn : null , 
59+         } 
60+         openSpots . set ( message . spot . unique_key ,  commentState ) 
61+       } 
62+       // and track the tab (could be multiple) 
63+       const  eventTab : Tab  =  { 
64+         tabId : sender . tab . id , 
65+         windowId : sender . tab . windowId , 
66+       } 
67+       const  existingTabs  =  openTabs . get ( message . spot . unique_key ) 
68+       if  ( existingTabs )  { 
69+         existingTabs . push ( eventTab ) 
70+       }  else  { 
71+         openTabs . set ( message . spot . unique_key ,  [ eventTab ] ) 
5872      } 
59-       openSpots . set ( message . spot . unique_key ,  commentState ) 
6073      break 
6174    } 
6275    case  'DESTROYED' : { 
@@ -108,12 +121,12 @@ export function handlePopupMessage(
108121    return  KEEP_PORT_OPEN 
109122  }  else  if  ( isOpenOrFocusMessage ( message ) )  { 
110123    logger . debug ( 'received switch tab message' ,  message ) 
111-     const  storage  =  openSpots . get ( message . uniqueKey ) 
112-     if  ( storage )  { 
124+     const  tabs  =  openTabs . get ( message . uniqueKey ) 
125+     if  ( tabs )  { 
113126      browser . windows 
114-         . update ( storage . tab . windowId ,  {  focused : true  } ) 
127+         . update ( tabs [ 0 ] ! . windowId ,  {  focused : true  } ) 
115128        . then ( ( )  =>  { 
116-           return  browser . tabs . update ( storage . tab . tabId ,  {  active : true  } ) 
129+           return  browser . tabs . update ( tabs [ 0 ] ! . tabId ,  {  active : true  } ) 
117130        } ) 
118131        . catch ( ( error )  =>  { 
119132          console . error ( 'Error switching to tab:' ,  error ) 
@@ -141,10 +154,15 @@ export default defineBackground(() => {
141154    logger . debug ( 'tab removed' ,  {  tabId } ) 
142155
143156    // Clean up openSpots entries for the closed tab 
144-     for  ( const  [ key ,  value ]  of  openSpots )  { 
145-       if  ( tabId  ===  value . tab . tabId )  { 
157+     for  ( const  [ key ,  tabs ]  of  openTabs )  { 
158+       const  remainingTabs  =  tabs . filter ( ( tab )  =>  tab . tabId  !==  tabId ) 
159+       if  ( remainingTabs . length  ===  0 )  { 
160+         logger . debug ( 'closed every tab which contained spot' ,  key ) 
146161        openSpots . delete ( key ) 
147-         logger . debug ( 'closed tab which contained spot' ,  value . spot . unique_key ) 
162+         openTabs . delete ( key ) 
163+       }  else  if  ( remainingTabs . length  <  tabs . length )  { 
164+         logger . debug ( 'closed tab which contained spot, other tabs still open' ,  key ) 
165+         openTabs . set ( key ,  remainingTabs ) 
148166      } 
149167    } 
150168  } ) 
0 commit comments