Skip to content

Commit 0411bbb

Browse files
committed
handle having a single unique_id open in multiple tabs
1 parent 8a0194a commit 0411bbb

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/entrypoints/background.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface Tab {
1515
windowId: number
1616
}
1717
export 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

3736
export const openSpots = new Map<string, CommentStorage>()
37+
export const openTabs = new Map<string, Array<Tab>>()
3838

3939
export 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
})

tests/background.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ describe('Background Event Handler', () => {
4646
"type": "TEST_SPOT",
4747
"unique_key": "test-key",
4848
},
49-
"tab": {
50-
"tabId": 123,
51-
"windowId": 456,
52-
},
5349
"trashedOn": null,
5450
},
5551
],

tests/playground/replica.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ const sampleSpots: CommentStorage[] = spots.map((spot) => {
2727
drafts: [[0, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.']],
2828
sentOn: null,
2929
spot,
30-
tab: {
31-
tabId: 123,
32-
windowId: 456,
33-
},
3430
trashedOn: null,
3531
}
3632
return state

0 commit comments

Comments
 (0)