diff options
| -rw-r--r-- | background_scripts/completion.coffee | 37 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 16 | ||||
| -rw-r--r-- | tests/unit_tests/test_chrome_stubs.coffee | 2 | 
3 files changed, 46 insertions, 9 deletions
| diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index b411bcba..8efd1687 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -259,12 +259,19 @@ class DomainCompleter    # Suggestions from the Domain completer have the maximum relevancy. They should be shown first in the list.    computeRelevancy: -> 1 -# TabRecency associates a logical timestamp with each tab id. +# 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 -  constructor: -> -    @timestamp = 1 -    @cache = {} +  timestamp: 1 +  current: -1 +  cache: {} + +  lastVisited: null +  lastVisitedTime: null +  timeDelta: 500 + +  constructor: ->      chrome.tabs.onActivated.addListener (activeInfo) => @add activeInfo.tabId      chrome.tabs.onRemoved.addListener (tabId) => @remove tabId @@ -272,13 +279,29 @@ class TabRecency        @remove removedTabId        @add addedTabId -  add: (tabId) -> @cache[tabId] = ++@timestamp -  remove: (tabId) -> delete @cache[tabId] +  add: (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 +      @cache[@lastVisited] = ++@timestamp + +    @current = tabId +    @lastVisited = tabId +    # If the tab we were previously on has gone away (or never existed if this is the first tab), then make +    # this one registers as soon as it's blurred. +    @lastVisitedTime = if @lastVisitedTime? then currentTime else new Date(currentTime - @timeDelta) + +  remove: (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 @cache[tabId] == @timestamp then 0.0 else @cache[tabId] / @timestamp +    if tabId == @current then 0.0 else @cache[tabId] / @timestamp  tabRecency = new TabRecency() diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 88f59b7e..755d681e 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -399,21 +399,32 @@ context "RegexpCache",    should "search for a string with a prefix/suffix (negative case)", ->      assert.isTrue "hound dog".search(RegexpCache.get("do", "\\b", "\\b")) == -1 +fakeTimeDeltaElapsing = -> +  context "TabRecency",    setup ->      @tabRecency = new TabRecency() + +    fakeTimeDeltaElapsing = => +      if @tabRecency.lastVisitedTime? +        @tabRecency.lastVisitedTime = new Date(@tabRecency.lastVisitedTime - @tabRecency.timeDelta) +      @tabRecency.add 3 +    fakeTimeDeltaElapsing()      @tabRecency.add 2 +    fakeTimeDeltaElapsing()      @tabRecency.add 9 +    fakeTimeDeltaElapsing()      @tabRecency.add 1      @tabRecency.remove 9 +    fakeTimeDeltaElapsing()      @tabRecency.add 4 +    fakeTimeDeltaElapsing() -  should "have entries for active tabs", -> +  should "have entries for recently active tabs", ->      assert.isTrue @tabRecency.cache[1]      assert.isTrue @tabRecency.cache[2]      assert.isTrue @tabRecency.cache[3] -    assert.isTrue @tabRecency.cache[4]    should "not have entries for removed tabs", ->      assert.isFalse @tabRecency.cache[9] @@ -432,6 +443,7 @@ context "TabRecency",      assert.isTrue @tabRecency.recencyScore(3) < @tabRecency.recencyScore 2      assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 1      @tabRecency.add 3 +    fakeTimeDeltaElapsing()      @tabRecency.add 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 diff --git a/tests/unit_tests/test_chrome_stubs.coffee b/tests/unit_tests/test_chrome_stubs.coffee index 80750337..396a2e55 100644 --- a/tests/unit_tests/test_chrome_stubs.coffee +++ b/tests/unit_tests/test_chrome_stubs.coffee @@ -95,3 +95,5 @@ exports.chrome =          callback() if callback          # Now, generate (supposedly asynchronous) notification for listeners.          global.chrome.storage.onChanged.callEmpty(key) + +exports.setTimeout = (callback,timeout) -> callback() | 
