diff options
| author | Stephen Blott | 2016-02-12 06:39:14 +0000 |
|---|---|---|
| committer | Stephen Blott | 2016-02-12 06:39:14 +0000 |
| commit | 973e5eebf2fdec5225c9332e5783dc90f5c720ce (patch) | |
| tree | 0531e1998d0925d679956e0a7da9d5fb0e2fb01f /background_scripts/bg_utils.coffee | |
| parent | aabd2068e4ba497c9ad7d237a727121fd573b837 (diff) | |
| parent | 1d809afe18d8638b899a1016d9cca0ccc4a32253 (diff) | |
| download | vimium-973e5eebf2fdec5225c9332e5783dc90f5c720ce.tar.bz2 | |
Merge pull request #1984 from smblott-github/previous-tab
New command: visit previous tab.
Diffstat (limited to 'background_scripts/bg_utils.coffee')
| -rw-r--r-- | background_scripts/bg_utils.coffee | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee new file mode 100644 index 00000000..96c1282a --- /dev/null +++ b/background_scripts/bg_utils.coffee @@ -0,0 +1,51 @@ +root = exports ? window + +# TabRecency associates a logical timestamp with each tab id. These are used to provide an initial +# recency-based ordering in the tabs vomnibar (which allows jumping quickly between recently-visited tabs). +class TabRecency + timestamp: 1 + current: -1 + cache: {} + lastVisited: null + lastVisitedTime: null + timeDelta: 500 # Milliseconds. + + constructor: -> + chrome.tabs.onActivated.addListener (activeInfo) => @register activeInfo.tabId + chrome.tabs.onRemoved.addListener (tabId) => @deregister tabId + + chrome.tabs.onReplaced.addListener (addedTabId, removedTabId) => + @deregister removedTabId + @register addedTabId + + register: (tabId) -> + currentTime = new Date() + # Register tabId if it has been visited for at least @timeDelta ms. Tabs which are visited only for a + # very-short time (e.g. those passed through with `5J`) aren't registered as visited at all. + if @lastVisitedTime? and @timeDelta <= currentTime - @lastVisitedTime + @cache[@lastVisited] = ++@timestamp + + @current = @lastVisited = tabId + @lastVisitedTime = currentTime + + deregister: (tabId) -> + if tabId == @lastVisited + # Ensure we don't register this tab, since it's going away. + @lastVisited = @lastVisitedTime = null + delete @cache[tabId] + + # Recently-visited tabs get a higher score (except the current tab, which gets a low score). + recencyScore: (tabId) -> + @cache[tabId] ||= 1 + if tabId == @current then 0.0 else @cache[tabId] / @timestamp + + # Returns a list of tab Ids sorted by recency, most recent tab first. + getTabsByRecency: -> + tabIds = (tId for own tId of @cache) + tabIds.sort (a,b) => @cache[b] - @cache[a] + tabIds.map (tId) -> parseInt tId + +BgUtils = + tabRecency: new TabRecency() + +root.BgUtils = BgUtils |
