diff options
| -rw-r--r-- | background_scripts/bg_utils.coffee | 45 | ||||
| -rw-r--r-- | background_scripts/completion.coffee | 44 | ||||
| -rw-r--r-- | manifest.json | 1 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 3 | 
4 files changed, 49 insertions, 44 deletions
| diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee new file mode 100644 index 00000000..9c1674bf --- /dev/null +++ b/background_scripts/bg_utils.coffee @@ -0,0 +1,45 @@ +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 + +BgUtils = +  tabRecency: new TabRecency() + +root.BgUtils = BgUtils diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 2427bad8..c880a26c 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -351,47 +351,6 @@ class DomainCompleter    parseDomainAndScheme: (url) ->        Utils.hasFullUrlPrefix(url) and not Utils.hasChromePrefix(url) and url.split("/",3).join "/" -# 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 - -tabRecency = new TabRecency() -  # Searches through all open tabs, matching on title and URL.  class TabCompleter    filter: ({ queryTerms }, onComplete) -> @@ -414,7 +373,7 @@ class TabCompleter      if suggestion.queryTerms.length        RankingUtils.wordRelevancy(suggestion.queryTerms, suggestion.url, suggestion.title)      else -      tabRecency.recencyScore(suggestion.tabId) +      BgUtils.tabRecency.recencyScore(suggestion.tabId)  class SearchEngineCompleter    @debug: false @@ -862,4 +821,3 @@ root.SearchEngineCompleter = SearchEngineCompleter  root.HistoryCache = HistoryCache  root.RankingUtils = RankingUtils  root.RegexpCache = RegexpCache -root.TabRecency = TabRecency diff --git a/manifest.json b/manifest.json index 7fe13b13..97599bbb 100644 --- a/manifest.json +++ b/manifest.json @@ -12,6 +12,7 @@        "lib/settings.js",        "background_scripts/commands.js",        "lib/clipboard.js", +      "background_scripts/bg_utils.js",        "background_scripts/exclusions.js",        "background_scripts/completion_engines.js",        "background_scripts/completion_search.js", diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index 9ce0a466..3bb187fd 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -1,5 +1,6 @@  require "./test_helper.js"  extend(global, require "../../lib/utils.js") +extend(global, require "../../background_scripts/bg_utils.js")  extend(global, require "../../background_scripts/completion_engines.js")  extend(global, require "../../background_scripts/completion.js")  extend global, require "./test_chrome_stubs.js" @@ -411,7 +412,7 @@ fakeTimeDeltaElapsing = ->  context "TabRecency",    setup -> -    @tabRecency = new TabRecency() +    @tabRecency = BgUtils.tabRecency      fakeTimeDeltaElapsing = =>        if @tabRecency.lastVisitedTime? | 
