Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
debounce tab update events
Browse files Browse the repository at this point in the history
auditors @bbondy
  • Loading branch information
bridiver committed Mar 1, 2017
1 parent 669781b commit fe02ce3
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ var tabs = {}

const TAB_ID_NONE = -1

function debounce (fn, bufferInterval, ...args) {
let timeout
return (...args2) => {
clearTimeout(timeout)
let a = args || []
if (args2 && args2.constructor === Array) {
a = a.concat(args2)
}
timeout = setTimeout(fn.apply.bind(fn, this, a), bufferInterval)
}
}

var getResourceURL = function (extensionId, path) {
path = String(path)
if (!path.length || path[0] != '/')
Expand Down Expand Up @@ -350,45 +362,56 @@ app.on('web-contents-created', function (event, tab) {
if (tabId === -1)
return

const updateTabDebounce = debounce(chromeTabsUpdated, 5)

tab.on('page-title-updated', function () {
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('did-fail-load', function () {
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('did-fail-provisional-load', function () {
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('did-attach', function () {
createTabValue(tab)
chromeTabsUpdated(tabId)
chromeTabsUpdated(tabId) // immediate
})
tab.on('did-detach', () => {
chromeTabsUpdated(tabId) // immediate
})
tab.on('did-start-loading', function () {
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('did-stop-loading', function () {
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('navigation-entry-commited', function (evt, url) {
createTabValue(tab)
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('did-navigate', function (evt, url) {
createTabValue(tab)
chromeTabsUpdated(tabId)
chromeTabsUpdated(tabId) // immediate
})
tab.on('did-navigate-in-page', function (evt, url, isMainFrame) {
updateTabDebounce(tabId)
})
tab.on('load-start', function (evt, url, isMainFrame, isErrorPage) {
if (isMainFrame) {
createTabValue(tab)
chromeTabsUpdated(tabId)
chromeTabsUpdated(tabId) // immediate
}
})
tab.on('did-finish-load', function () {
createTabValue(tab)
chromeTabsUpdated(tabId)
updateTabDebounce(tabId)
})
tab.on('set-active', function (evt, active) {
chromeTabsUpdated(tabId)
chromeTabsUpdated(tabId) // immediate
})
tab.on('set-tab-index', function (evt, index) {
updateTabDebounce(tabId)
})
tab.on('destroyed', function () {
chromeTabsRemoved(tabId)
Expand Down

0 comments on commit fe02ce3

Please sign in to comment.