diff options
| author | Stephen Blott | 2015-05-06 04:55:22 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-05-06 05:03:13 +0100 |
| commit | 7d11b1699454366bf99e8e5033ba39b127687fcb (patch) | |
| tree | 740e1234c6d5ce89058d1841bf22971098c38eee /pages | |
| parent | 3a4ca1615ec714e9036453bd44839fe056be63e7 (diff) | |
| download | vimium-7d11b1699454366bf99e8e5033ba39b127687fcb.tar.bz2 | |
Search completion; simplify messaging code.
This eliminates the need to repeatedly install and remove listeners for
@filterPort in the vomnibar. It also eleiminates the need for
"keepAlive" in reponses. All as suggested by @mrmr1993 in #1635.
Diffstat (limited to 'pages')
| -rw-r--r-- | pages/vomnibar.coffee | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/pages/vomnibar.coffee b/pages/vomnibar.coffee index 8a070df6..d9a86a3f 100644 --- a/pages/vomnibar.coffee +++ b/pages/vomnibar.coffee @@ -223,37 +223,35 @@ class VomnibarUI # Sends filter and refresh requests to a Vomnibox completer on the background page. # class BackgroundCompleter - # We increment this counter on each message sent, and ignore responses which arrive too late. - @messageId: 0 - # - name: The background page completer that you want to interface with. Either "omni", "tabs", or # "bookmarks". */ constructor: (@name) -> + @messageId = null @filterPort = chrome.runtime.connect name: "filterCompleter" + @filterPort.onMessage.addListener handler = @messageHandler + + messageHandler: (msg) => + # We ignore messages which arrive too late. + if msg.id == @messageId + # The result objects coming from the background page will be of the form: + # { html: "", type: "", url: "" } + # type will be one of [tab, bookmark, history, domain]. + results = msg.results.map (result) -> + functionToCall = if result.type == "tab" + BackgroundCompleter.completionActions.switchToTab.curry result.tabId + else + BackgroundCompleter.completionActions.navigateToUrl.curry result.url + result.performAction = functionToCall + result + @mostRecentCallback results + + filter: (query, @mostRecentCallback) -> + @messageId = Utils.createUniqueId() + @filterPort.postMessage id: @messageId, name: @name, query: query refresh: -> chrome.runtime.sendMessage handler: "refreshCompleter", name: @name - filter: (query, callback) -> - id = BackgroundCompleter.messageId += 1 - @filterPort.onMessage.addListener handler = (msg) => - if msg.id == id - @filterPort.onMessage.removeListener handler unless msg.keepAlive and id == BackgroundCompleter.messageId - if id == BackgroundCompleter.messageId - # The result objects coming from the background page will be of the form: - # { html: "", type: "", url: "" } - # type will be one of [tab, bookmark, history, domain]. - results = msg.results.map (result) -> - functionToCall = if (result.type == "tab") - BackgroundCompleter.completionActions.switchToTab.curry(result.tabId) - else - BackgroundCompleter.completionActions.navigateToUrl.curry(result.url) - result.performAction = functionToCall - result - callback(results) - - @filterPort.postMessage id: id, name: @name, query: query - userIsTyping: -> @filterPort.postMessage name: @name, userIsTyping: true |
