aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-05-09 09:50:13 +0100
committerStephen Blott2015-05-09 09:50:13 +0100
commit75051d53536ddb9f247501b4509306cae1734184 (patch)
tree3d70d47a5b05d2ce506650f9d22516b2991eccfa
parent3bc555eb02b228f35647884575189eed40625c52 (diff)
downloadvimium-75051d53536ddb9f247501b4509306cae1734184.tar.bz2
Search completion; reintroduce vomnibar cache.
-rw-r--r--background_scripts/completion.coffee4
-rw-r--r--pages/vomnibar.coffee47
2 files changed, 37 insertions, 14 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee
index f800c818..8c73c658 100644
--- a/background_scripts/completion.coffee
+++ b/background_scripts/completion.coffee
@@ -547,7 +547,7 @@ class MultiCompleter
unless suggestions.length == 0 and shouldRunContinuations
onComplete
results: @prepareSuggestions queryTerms, suggestions
- expectMoreResults: shouldRunContinuations
+ mayCacheResult: continuations.length == 0
# Run any continuations, unless there's a pending query.
if shouldRunContinuations
@@ -560,7 +560,7 @@ class MultiCompleter
results: @prepareSuggestions queryTerms, suggestions
# FIXME(smblott) This currently assumes that there is at most one continuation. We
# should really be counting pending/completed continuations.
- expectMoreResults: false
+ mayCacheResult: true
# Admit subsequent queries, and launch any pending query.
@filterInProgress = false
diff --git a/pages/vomnibar.coffee b/pages/vomnibar.coffee
index bc773909..bb7720e9 100644
--- a/pages/vomnibar.coffee
+++ b/pages/vomnibar.coffee
@@ -244,12 +244,17 @@ class BackgroundCompleter
constructor: (@name) ->
@port = chrome.runtime.connect name: "completions"
@messageId = null
+ # @keywords and @cache are both reset in @reset().
+ # We only cache for the duration of a single vomnibar activation.
+ @keywords = []
+ @cache = {}
@reset()
@port.onMessage.addListener (msg) =>
switch msg.handler
when "customSearchEngineKeywords"
- @lastUI.setKeywords msg.keywords
+ @keywords = msg.keywords
+ @lastUI.setKeywords @keywords
when "completions"
# The result objects coming from the background page will be of the form:
# { html: "", type: "", url: "" }
@@ -261,26 +266,44 @@ class BackgroundCompleter
else
@completionActions.navigateToUrl.curry result.url
- # We ignore messages which arrive too late.
- if msg.id == @messageId
- @mostRecentCallback msg.results
+ # Cache the result -- if we have been told it's ok to do so (it could be that more results will be
+ # posted shortly). We cache the result even if it arrives late.
+ if msg.mayCacheResult
+ console.log "cache set:", "-#{msg.cacheKey}-" if @debug
+ @cache[msg.cacheKey] = msg.results
+ else
+ console.log "not setting cache:", "-#{msg.cacheKey}-" if @debug
+
+ # Handle the message, but only if it hasn't arrived too late.
+ @mostRecentCallback msg.results if msg.id == @messageId
filter: (query, @mostRecentCallback) ->
queryTerms = query.trim().split(/\s+/).filter (s) -> 0 < s.length
- @port.postMessage
- handler: "filter"
- name: @name
- id: @messageId = Utils.createUniqueId()
- queryTerms: queryTerms
- query: query
+ cacheKey = queryTerms.join " "
+ cacheKey += " " if 0 < queryTerms.length and queryTerms[0] in @keywords and /\s$/.test query
+
+ if cacheKey of @cache
+ console.log "cache hit:", "-#{cacheKey}-" if @debug
+ @mostRecentCallback @cache[cacheKey]
+ else
+ console.log "cache miss:", "-#{cacheKey}-" if @debug
+ @port.postMessage
+ handler: "filter"
+ name: @name
+ id: @messageId = Utils.createUniqueId()
+ queryTerms: queryTerms
+ query: query
+ cacheKey: cacheKey
+
+ reset: ->
+ @keywords = []
+ @cache = {}
refresh: (@lastUI) ->
@reset()
# Inform the background completer that we have a new vomnibar activation.
@port.postMessage name: @name, handler: "refresh"
- reset: ->
-
cancel: ->
# Inform the background completer that it may (should it choose to do so) abandon any pending query
# (because the user is typing, and there'll be another query along soon).