Permalink
Browse files

Extend removing entries to the domain completer.

  • Loading branch information...
smblott-github committed Nov 11, 2012
1 parent 3dc8a03 commit 03823f97a526ed5e5104b724d4f2a0b582505a6c
Showing with 31 additions and 7 deletions.
  1. +29 −5 background_scripts/completion.coffee
  2. +2 −2 tests/unit_tests/completion_test.coffee
@@ -169,7 +169,7 @@ 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
+ domains: null # A map of domain -> { entry: <historyEntry>, referenceCount: <count> }
filter: (queryTerms, onComplete) ->
return onComplete([]) if queryTerms.length > 1
@@ -190,7 +190,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])
@@ -205,13 +205,37 @@ class DomainCompleter
domain = @parseDomain(entry.url)
if domain
previousEntry = @domains[domain]
- @domains[domain] = entry if !previousEntry || (previousEntry.lastVisitTime < entry.lastVisitTime)
+ if previousEntry
+ previousEntry.entry = entry if previousEntry.lastVisitTime < entry.lastVisitTime
+ previousEntry.referenceCount +=1
+ else
+ @domains[domain] = { entry: entry, referenceCount: 1 }
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
+ previousEntry = @domains[domain]
+ if previousEntry
+ previousEntry.entry = newPage
+ previousEntry.referenceCount += 1
+ else
+ @domains[domain] = { entry: newPage, referenceCount: 1 }
+
+ onVisitRemoved: (toRemove) ->
+ if toRemove.allHistory
+ @domains = {}
+ else
+ toRemove.urls.forEach (url) =>
+ domain = @parseDomain(url)
+ if domain
+ previousEntry = @domains[domain]
+ if previousEntry
+ previousEntry.referenceCount -= 1
+ if previousEntry.referenceCount == 0
+ delete @domains[domain]
parseDomain: (url) -> url.split("/")[2] || ""
@@ -389,7 +413,7 @@ HistoryCache =
if toRemove.allHistory
@history = []
else
- toRemove.urls.map (url) =>
+ toRemove.urls.forEach (url) =>
i = HistoryCache.binarySearch({url:url}, @history, @compareHistoryByUrl)
# TODO (smblott)
# The `i < @history.length` condition below should not be necessary. It can be removed when `binarySearch` is fixed.
@@ -137,7 +137,7 @@ context "domain completer",
@history2 = { title: "history2", url: "http://history2.com", lastVisitTime: hours(1) }
stub(HistoryCache, "use", (onComplete) => onComplete([@history1, @history2]))
- global.chrome.history = { onVisited: { addListener: -> }, onVisitRemoved: -> }
+ global.chrome.history = { onVisited: { addListener: -> }, onVisitRemoved: { addListener: -> } }
stub(Date, "now", returns(hours(24)))
@completer = new DomainCompleter()
@@ -147,7 +147,7 @@ context "domain completer",
assert.arrayEqual ["history1.com"], results.map (result) -> result.url
should "pick domains which are more recent", ->
- # This domains are the same except for their last visited time.
+ # These domains are the same except for their last visited time.
assert.equal "history1.com", filterCompleter(@completer, ["story"])[0].url
@history2.lastVisitTime = hours(3)
assert.equal "history2.com", filterCompleter(@completer, ["story"])[0].url

0 comments on commit 03823f9

Please sign in to comment.