diff options
| author | Stephen Blott | 2014-11-22 15:16:35 +0000 |
|---|---|---|
| committer | Stephen Blott | 2014-11-22 15:16:35 +0000 |
| commit | 1c2730ca195fcc075fbfc8bc8993672ae978b246 (patch) | |
| tree | d84712064bb4cbe747f600246c63e146e7286c7b | |
| parent | 0e4f78e5628eb3d33e4d7c441882ff00307d6e8f (diff) | |
| download | vimium-1c2730ca195fcc075fbfc8bc8993672ae978b246.tar.bz2 | |
Touch up tab recency.
| -rw-r--r-- | background_scripts/completion.coffee | 22 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 16 |
2 files changed, 18 insertions, 20 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index dc24ebb7..dc5519d5 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -265,31 +265,29 @@ class TabRecency timestamp: 1 current: -1 cache: {} - lastVisited: null lastVisitedTime: null - - timeDelta: 500 + timeDelta: 500 # Milliseconds. constructor: -> - chrome.tabs.onActivated.addListener (activeInfo) => @add activeInfo.tabId - chrome.tabs.onRemoved.addListener (tabId) => @remove tabId + chrome.tabs.onActivated.addListener (activeInfo) => @register activeInfo.tabId + chrome.tabs.onRemoved.addListener (tabId) => @deregister tabId chrome.tabs.onReplaced.addListener (addedTabId, removedTabId) => - @remove removedTabId - @add addedTabId + @deregister removedTabId + @register addedTabId - add: (tabId) -> + register: (tabId) -> currentTime = new Date() - # Register tabId if it has been visited for at least @timeDelta. Tabs which are visited only for a - # very-short time (e.g. those passed through with `5J`) shouldn't be registered as visited at all. - if @lastVisitedTime? and currentTime - @lastVisitedTime >= @timeDelta + # 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 - remove: (tabId) -> + deregister: (tabId) -> if tabId == @lastVisited # Ensure we don't register this tab, since it's going away. @lastVisited = @lastVisitedTime = null diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 755d681e..e4966016 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -409,16 +409,16 @@ context "TabRecency", if @tabRecency.lastVisitedTime? @tabRecency.lastVisitedTime = new Date(@tabRecency.lastVisitedTime - @tabRecency.timeDelta) - @tabRecency.add 3 + @tabRecency.register 3 fakeTimeDeltaElapsing() - @tabRecency.add 2 + @tabRecency.register 2 fakeTimeDeltaElapsing() - @tabRecency.add 9 + @tabRecency.register 9 fakeTimeDeltaElapsing() - @tabRecency.add 1 - @tabRecency.remove 9 + @tabRecency.register 1 + @tabRecency.deregister 9 fakeTimeDeltaElapsing() - @tabRecency.add 4 + @tabRecency.register 4 fakeTimeDeltaElapsing() should "have entries for recently active tabs", -> @@ -442,9 +442,9 @@ context "TabRecency", should "rank tabs by recency", -> assert.isTrue @tabRecency.recencyScore(3) < @tabRecency.recencyScore 2 assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 1 - @tabRecency.add 3 + @tabRecency.register 3 fakeTimeDeltaElapsing() - @tabRecency.add 4 # Making 3 the most recent tab which isn't the current tab. + @tabRecency.register 4 # Making 3 the most recent tab which isn't the current tab. assert.isTrue @tabRecency.recencyScore(1) < @tabRecency.recencyScore 3 assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 3 assert.isTrue @tabRecency.recencyScore(4) < @tabRecency.recencyScore 3 |
