aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts/completion.coffee
diff options
context:
space:
mode:
authorPhil Crosby2012-11-12 22:34:08 -0800
committerPhil Crosby2012-11-12 22:34:08 -0800
commit32e506dd94565d54ab4d81144eebf849a98be338 (patch)
treea581aa23cdb43cbbd52ad8eb490224cf1344a584 /background_scripts/completion.coffee
parenta3eb1cbee93c4c10505f629b8bc8bc00e37adb8a (diff)
parentf0347388264b0fb437489f4f021ba8c0dbcca8e3 (diff)
downloadvimium-32e506dd94565d54ab4d81144eebf849a98be338.tar.bz2
Merge pull request #715 from smblott-github/history--remove-entries
Remove entries from History/Domain completers when they're removed on chrome.
Diffstat (limited to 'background_scripts/completion.coffee')
-rw-r--r--background_scripts/completion.coffee42
1 files changed, 33 insertions, 9 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index 25f76c1e..28bb1afa 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -169,7 +169,11 @@ class HistoryCompleter
# The domain completer is designed to match a single-word query which looks like it is a domain. This supports
# the user experience where they quickly type a partial domain, hit tab -> enter, and expect to arrive there.
class DomainCompleter
- domains: null # A map of domain -> history
+ # A map of domain -> { entry: <historyEntry>, referenceCount: <count> }
+ # - `entry` is the most recently accessed page in the History within this domain.
+ # - `referenceCount` is a count of the number of History entries within this domain.
+ # If `referenceCount` goes to zero, the domain entry can and should be deleted.
+ domains: null
filter: (queryTerms, onComplete) ->
return onComplete([]) if queryTerms.length > 1
@@ -190,7 +194,7 @@ class DomainCompleter
sortDomainsByRelevancy: (queryTerms, domainCandidates) ->
results = []
for domain in domainCandidates
- recencyScore = RankingUtils.recencyScore(@domains[domain].lastVisitTime || 0)
+ recencyScore = RankingUtils.recencyScore(@domains[domain].entry.lastVisitTime || 0)
wordRelevancy = RankingUtils.wordRelevancy(queryTerms, domain, null)
score = (wordRelevancy + Math.max(recencyScore, wordRelevancy)) / 2
results.push([domain, score])
@@ -200,18 +204,27 @@ class DomainCompleter
populateDomains: (onComplete) ->
HistoryCache.use (history) =>
@domains = {}
- history.forEach (entry) =>
- # We want each key in our domains hash to point to the most recent History entry for that domain.
- domain = @parseDomain(entry.url)
- if domain
- previousEntry = @domains[domain]
- @domains[domain] = entry if !previousEntry || (previousEntry.lastVisitTime < entry.lastVisitTime)
+ history.forEach (entry) => @onPageVisited entry
chrome.history.onVisited.addListener(@onPageVisited.bind(this))
+ chrome.history.onVisitRemoved.addListener(@onVisitRemoved.bind(this))
onComplete()
onPageVisited: (newPage) ->
domain = @parseDomain(newPage.url)
- @domains[domain] = newPage if domain
+ if domain
+ slot = @domains[domain] ||= { entry: newPage, referenceCount: 0 }
+ # We want each entry in our domains hash to point to the most recent History entry for that domain.
+ slot.entry = newPage if slot.entry.lastVisitTime < newPage.lastVisitTime
+ slot.referenceCount += 1
+
+ onVisitRemoved: (toRemove) ->
+ if toRemove.allHistory
+ @domains = {}
+ else
+ toRemove.urls.forEach (url) =>
+ domain = @parseDomain(url)
+ if domain and @domains[domain] and ( @domains[domain].referenceCount -= 1 ) == 0
+ delete @domains[domain]
parseDomain: (url) -> url.split("/")[2] || ""
@@ -365,6 +378,7 @@ HistoryCache =
history.sort @compareHistoryByUrl
@history = history
chrome.history.onVisited.addListener(@onPageVisited.bind(this))
+ chrome.history.onVisitRemoved.addListener(@onVisitRemoved.bind(this))
callback(@history) for callback in @callbacks
@callbacks = null
@@ -383,6 +397,16 @@ HistoryCache =
else
@history.splice(i, 0, newPage)
+ # When a page is removed from the chrome history, remove it from the vimium history too.
+ onVisitRemoved: (toRemove) ->
+ if toRemove.allHistory
+ @history = []
+ else
+ toRemove.urls.forEach (url) =>
+ i = HistoryCache.binarySearch({url:url}, @history, @compareHistoryByUrl)
+ if i < @history.length and @history[i].url == url
+ @history.splice(i, 1)
+
# Returns the matching index or the closest matching index if the element is not found. That means you
# must check the element at the returned index to know whether the element was actually found.
# This method is used for quickly searching through our history cache.