Skip to content

Commit baaa3b1

Browse files
committed
feat(BrowserController): Listen to tab events
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 4587fd7 commit baaa3b1

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/lib/browser/BrowserController.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ export default class BrowserController {
9999
browser.bookmarks.onCreated.addListener((localId, details) =>
100100
this.onchange(localId, details)
101101
)
102+
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
103+
this.onTabsChanged(tabId)
104+
)
105+
browser.tabs.onRemoved.addListener((tabId, changeInfo, tab) =>
106+
this.onTabsChanged(tabId)
107+
)
108+
browser.tabs.onMoved.addListener((tabId, changeInfo, tab) =>
109+
this.onTabsChanged(tabId)
110+
)
111+
browser.tabs.onDetached.addListener((tabId, changeInfo, tab) =>
112+
this.onTabsChanged(tabId)
113+
)
114+
if (typeof browser.tabGroups !== 'undefined') {
115+
browser.tabGroups.onCreated.addListener((tabId, changeInfo, tab) =>
116+
this.onTabsChanged(tabId)
117+
)
118+
browser.tabGroups.onMoved.addListener((tabId, changeInfo, tab) =>
119+
this.onTabsChanged(tabId)
120+
)
121+
browser.tabGroups.onRemoved.addListener((tabId, changeInfo, tab) =>
122+
this.onTabsChanged(tabId)
123+
)
124+
browser.tabGroups.onUpdated.addListener((tabId, changeInfo, tab) =>
125+
this.onTabsChanged(tabId)
126+
)
127+
}
102128

103129
browser.permissions.contains({permissions: ['history']}).then((historyAllowed) => {
104130
if (historyAllowed) {
@@ -314,6 +340,34 @@ export default class BrowserController {
314340
this.setEnabled(true)
315341
}
316342

343+
async onTabsChanged() {
344+
if (!this.enabled) {
345+
return
346+
}
347+
// Debounce this function
348+
this.setEnabled(false)
349+
350+
const allAccounts = await BrowserAccount.getAllAccounts()
351+
352+
// Check which accounts contain the bookmark and which used to contain (track) it
353+
const tabAccounts = allAccounts.filter(account => {
354+
return account.getData().localRoot === 'tabs'
355+
})
356+
357+
const accountsToSync = tabAccounts
358+
// Filter out accounts that are not enabled
359+
.filter(account => account.getData().enabled)
360+
// Filter out accounts that are syncing, because the event may stem from the sync run
361+
.filter(account => !account.getData().syncing)
362+
363+
// schedule a new sync for all accounts involved
364+
accountsToSync.forEach(account => {
365+
this.scheduleSync(account.id, true)
366+
})
367+
368+
this.setEnabled(true)
369+
}
370+
317371
async scheduleSync(accountId, wait) {
318372
if (wait) {
319373
if (this.schedule[accountId]) {

0 commit comments

Comments
 (0)