1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-24 10:02:27 +01:00

Perform search-suggest query asynchronously via async http request rather than background thread.

This commit is contained in:
Kris Maglione
2008-11-28 18:51:28 +00:00
parent 4c914e9907
commit 5fdf4bd6d6
3 changed files with 35 additions and 16 deletions

View File

@@ -503,7 +503,7 @@ function Bookmarks() //{{{
return searchEngines;
},
getSuggestions: function getSuggestions(engine, query)
getSuggestions: function getSuggestions(engine, query, callback)
{
let ss = Components.classes["@mozilla.org/browser/search-service;1"]
.getService(Components.interfaces.nsIBrowserSearchService);
@@ -515,16 +515,25 @@ function Bookmarks() //{{{
if (!queryURI)
return [];
let resp = util.httpGet(queryURI);
function process(resp)
{
let json = Components.classes["@mozilla.org/dom/json;1"]
.createInstance(Components.interfaces.nsIJSON);
let results = [];
try
{
let results = json.decode(resp.responseText)[1];
return [[item, ""] for ([k, item] in Iterator(results)) if (typeof item == "string")];
results = json.decode(resp.responseText)[1];
results = [[item, ""] for ([k, item] in Iterator(results)) if (typeof item == "string")];
}
catch (e) {}
return [];
if (!callback)
return results;
callback(results);
}
let resp = util.httpGet(queryURI, callback && process);
if (!callback)
return process(resp);
},
// TODO: add filtering

View File

@@ -1514,10 +1514,13 @@ function Completion() //{{{
let ctxt = context.fork(name, 0);
ctxt.title = [engine.description + " Suggestions"];
ctxt.background = true;
ctxt.compare = null;
ctxt.regenerate = true;
ctxt.generate = function () bookmarks.getSuggestions(name, this.filter);
ctxt.incomplete = true;
bookmarks.getSuggestions(name, ctxt.filter, function (compl) {
ctxt.incomplete = false;
ctxt.completions = compl;
liberator.dump(compl);
});
});
},

View File

@@ -285,18 +285,25 @@ const util = { //{{{
return ret;
},
httpGet: function httpGet(url)
httpGet: function httpGet(url, callback)
{
try
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, false);
let xmlhttp = new XMLHttpRequest();
if (callback)
{
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4)
callback(xmlhttp)
}
}
xmlhttp.open("GET", url, !!callback);
xmlhttp.send(null);
return xmlhttp;
}
catch (e)
{
liberator.log("Error opening " + url, 1);
liberator.log("Error opening " + url + ": " + e, 1);
}
},