From a1e95718b9713c60d8c7a84e8b7c86590e3ffc7b Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 18 Nov 2014 16:55:27 +0000 Subject: Tabs recency; always choose correct current tab. --- background_scripts/completion.coffee | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index d750850f..0f6f4340 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -264,6 +264,7 @@ class DomainCompleter class TabRecency constructor: -> @timestamp = 1 + @current = -1 @cache = {} chrome.tabs.onActivated.addListener (activeInfo) => @add activeInfo.tabId @@ -273,24 +274,28 @@ class TabRecency @remove removedTabId @add addedTabId - add: (tabId) -> @soon => @cache[tabId] = ++@timestamp - remove: (tabId) -> delete @cache[tabId] + add: (tabId) -> + @current = tabId + @registerTabSoon tabId + + remove: (tabId) -> + delete @cache[tabId] # Call callback in 750ms time; unless we're pre-empted by another call before then. The idea is that tabs # which are visited only for a very-short time (e.g. with `3J`) shouldn't register as visited at all. - soon: do -> + registerTabSoon: do -> timer = null - (callback) -> + (tabId) -> clearTimeout timer if timer - timer = setTimeout (-> + timer = setTimeout (=> timer = null - callback()) + @cache[tabId] = ++@timestamp) , 750 # 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() -- cgit v1.2.3