diff options
| author | Stephen Blott | 2014-11-22 15:17:13 +0000 | 
|---|---|---|
| committer | Stephen Blott | 2014-11-22 15:17:13 +0000 | 
| commit | 99498dec4c41c3602cd306d1356cb9abc300f7e5 (patch) | |
| tree | d84712064bb4cbe747f600246c63e146e7286c7b | |
| parent | df1be6e5098e8d20ae0b06b8c69adceea1056838 (diff) | |
| parent | 1c2730ca195fcc075fbfc8bc8993672ae978b246 (diff) | |
| download | vimium-99498dec4c41c3602cd306d1356cb9abc300f7e5.tar.bz2 | |
Merge branch 'mrmr1993-synchronous-tab-visit-marker'
| -rw-r--r-- | background_scripts/completion.coffee | 40 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 34 | 
2 files changed, 52 insertions, 22 deletions
| diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index b411bcba..dc5519d5 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -259,26 +259,44 @@ 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 # Milliseconds. -    chrome.tabs.onActivated.addListener (activeInfo) => @add activeInfo.tabId -    chrome.tabs.onRemoved.addListener (tabId) => @remove tabId +  constructor: -> +    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 + +  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 -  add: (tabId) -> @cache[tabId] = ++@timestamp -  remove: (tabId) -> delete @cache[tabId] +  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 @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..e4966016 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() -    @tabRecency.add 3 -    @tabRecency.add 2 -    @tabRecency.add 9 -    @tabRecency.add 1 -    @tabRecency.remove 9 -    @tabRecency.add 4 - -  should "have entries for active tabs", -> + +    fakeTimeDeltaElapsing = => +      if @tabRecency.lastVisitedTime? +        @tabRecency.lastVisitedTime = new Date(@tabRecency.lastVisitedTime - @tabRecency.timeDelta) + +    @tabRecency.register 3 +    fakeTimeDeltaElapsing() +    @tabRecency.register 2 +    fakeTimeDeltaElapsing() +    @tabRecency.register 9 +    fakeTimeDeltaElapsing() +    @tabRecency.register 1 +    @tabRecency.deregister 9 +    fakeTimeDeltaElapsing() +    @tabRecency.register 4 +    fakeTimeDeltaElapsing() + +  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] @@ -431,8 +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.add 4 # Making 3 the most recent tab which isn't the current tab. +    @tabRecency.register 3 +    fakeTimeDeltaElapsing() +    @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 | 
