Permalink
Browse files

revision of awesomebar search behaviour

  • Loading branch information...
1 parent f5e3435 commit 37925d1dff31272a4733e8c0628c3c7fdb18ed1a @mackers mackers committed Jan 17, 2012
Showing with 127 additions and 68 deletions.
  1. +8 −4 components/facebook.js
  2. +119 −64 components/fbRemoteAutoComplete.js
View
@@ -19,8 +19,8 @@
*/
const BASE_CHECK_INTERVAL = 5*60*1000; // 5 minutes
-const DEBUG = true;
-const VERBOSITY = 2; // 0: no dumping, 1: normal dumping, 2: massive dumping
+const DEBUG = false;
+const VERBOSITY = 0; // 0: no dumping, 1: normal dumping, 2: massive dumping
var debug = ( VERBOSITY < 1 )
? function() {}
@@ -538,12 +538,15 @@ facebookService.prototype = {
{
if (response.id)
{
- //debug("SAVING ACCESS TOKEN: " + accessToken);
+ debug("XX SAVING ACCESS TOKEN: " + accessToken);
+ //debug("XX SAVING UID: " + response.id);
fbSvc._uid = response.id;
fbSvc._loggedIn = true;
fbSvc._prefService.setCharPref('extensions.facebook.access_token', accessToken)
fbSvc._prefService.setCharPref('extensions.facebook.uid', response.id)
+
+ debug("XX ACCESS TOKEN PREF NOW: " + fbSvc._prefService.getCharPref('extensions.facebook.access_token'));
}
else
{
@@ -581,7 +584,8 @@ facebookService.prototype = {
if( !saved ) {
// persist API sessions across the Firefox shutdown
// by saving them in the password store
- this.savePref( 'extensions.facebook.uid', this._uid );
+ if (this._uid)
+ this.savePref( 'extensions.facebook.uid', this._uid );
var hostname = PASSWORD_URL;
var formSubmitURL = PASSWORD_URL;
@@ -239,6 +239,8 @@ FacebookRemoteAutoCompleteSearch.prototype = {
bootstrap: function()
{
+ debug('bootstrapping');
+
this.sid = Math.random();
this.resultCache = {};
this.bootstrapped = false;
@@ -247,7 +249,11 @@ FacebookRemoteAutoCompleteSearch.prototype = {
this.fbSvc = Cc['@facebook.com/facebook-service;1'].getService(Ci.fbIFacebookService);
if (!this.uid || this.uid == "" || !this.fbSvc.loggedIn)
+ {
+ debug('no user id');
+ //this.fbSvc.sessionEnd();
return;
+ }
var opts = [
//{filter: ['event'], no_cache: 1},
@@ -273,9 +279,9 @@ FacebookRemoteAutoCompleteSearch.prototype = {
if (res)
{
- for (var i=0; i<res.length; i++)
+ for (var id in res)
{
- self.resultCache[res[i].uid] = res[i];
+ self.resultCache[id] = res[id];
}
self.bootstrapped = true;
@@ -349,8 +355,12 @@ FacebookRemoteAutoCompleteSearch.prototype = {
{
var queryResults = self.parsePayload(self.queryRequest.responseText.substr(9));
+ //debug("XX finished ajax query for '" + searchString + "'");
+
if (queryResults)
{
+ //debug("XX some results from that ");
+
if (cachedResults && cachedResults.matchCount > 0)
{
newResult = cachedResults;
@@ -360,28 +370,42 @@ FacebookRemoteAutoCompleteSearch.prototype = {
newResult = new FacebookRemoteAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 0, "", [], null, null);
}
- for (var i=0; i<cachedResults.length; i++)
+ //queryResults = queryResults.sort(function(a, b) { return a.index - b.index; });
+
+ for (var id in queryResults)
{
- newResult.appendMatch(
- cachedResults[i].path,
- cachedResults[i].text,
- cachedResults[i].photo);
- }
+ var existing = false;
- queryResults = queryResults.sort(function(a, b) { return a.index - b.index; });
+ for (var j=0; j<cachedResults.matchCount; j++)
+ {
+ if (cachedResults.getValueAt(j) == queryResults[id].path)
+ {
+ existing = true;
+ break;
+ }
+ }
- for (var i=0; i<queryResults.length; i++)
- {
- newResult.appendMatch(
- queryResults[i].path,
- queryResults[i].text,
- queryResults[i].photo);
- }
+ if (existing)
+ {
+ //self.resultCache[queryResults[i].uid].query = searchString.toLowerCase();
+ continue;
+ }
+ else
+ {
+ newResult.appendMatch(
+ queryResults[id].path,
+ queryResults[id].text,
+ queryResults[id].photo);
+ //queryResults[i].query = searchString.toLowerCase();
+ //self.resultCache[queryResults[i].uid] = queryResults[i];
+ }
- self._lastResult = newResult;
- listener.onSearchResult(self, newResult);
+ }
self.queryCache[searchString.toLowerCase()] = queryResults;
+
+ //self._lastResult = newResult;
+ listener.onSearchResult(self, newResult);
}
}
}
@@ -393,14 +417,20 @@ FacebookRemoteAutoCompleteSearch.prototype = {
parsePayload: function(pltext)
{
//debug("XX Payload: " + pltext);
- var queryResults = [];
+
+ var queryResults = {};
try
{
var pl = JSON.parse(pltext);
if (pl.error)
{
+ if (pl.error == 1357001)
+ {
+ this.fbSvc.sessionEnd();
+ }
+
debug("Error in payload: " + pl.error);
return null;
}
@@ -421,11 +451,11 @@ FacebookRemoteAutoCompleteSearch.prototype = {
continue;
}
- if (this.resultCache[entry.uid])
+ /*if (this.resultCache[entry.uid])
{
debug("XX hit cache on " + entry.uid);
continue;
- }
+ }*/
var path = (entry.path.toString().substring(0,1) == "/"?
"https://www.facebook.com" + entry.path.toString():
@@ -445,7 +475,8 @@ FacebookRemoteAutoCompleteSearch.prototype = {
};
//this.resultCache[entry.uid] = result;
- queryResults.push(result);
+ //queryResults.push(result);
+ queryResults[entry.uid] = result;
}
}
catch (e)
@@ -461,50 +492,71 @@ FacebookRemoteAutoCompleteSearch.prototype = {
{
var tmpResults = [];
var search_lc = searchString.toLowerCase();
+ var newResult = new FacebookRemoteAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 0, "", [], null, null);
- for (var id in this.resultCache)
+ function searchACache(q, c)
{
- if (this.resultCache[id]._text_lc.indexOf(search_lc) > -1
- || (this.resultCache[id].alias && this.resultCache[id].alias.indexOf(search_lc) > -1)
- )
- {
- tmpResults.push(this.resultCache[id]);
- }
- else if (this.resultCache[id].tokens)
+ var r = [];
+
+ for (var id in c)
{
- for (var i=0; i<this.resultCache[id].tokens.length; i++)
+ if (c[id]._text_lc.indexOf(q) > -1
+ || (c[id].alias && c[id].alias.indexOf(q) > -1)
+ )
{
- if (this.resultCache[id].tokens[i].indexOf(search_lc) > -1)
+ r.push(c[id]);
+ //debug("XX searchString '" + q + "' found in cache");
+ }
+ else if (c[id].tokens)
+ {
+ for (var i=0; i<c[id].tokens.length; i++)
{
- tmpResults.push(this.resultCache[id]);
- break;
+ if (c[id].tokens[i].indexOf(q) > -1)
+ {
+ r.push(c[id]);
+ //debug("XX searchString '" + q + "' found in cache");
+ break;
+ }
}
}
}
+
+ return r;
}
- var foundCachedSearchString = "";
+ var tmpResults = searchACache(search_lc, this.resultCache);
+ tmpResults = tmpResults.sort(function(a, b) { return a.index - b.index; });
+
+ //debug("XX have " + tmpResults.length + " result from bootstrap cache matches");
- for (var cachedSearchString in this.queryCache)
+ if (this.queryCache[search_lc])
{
- if (cachedSearchString.indexOf(search_lc) > -1 && cachedSearchString.length > foundCachedSearchString.length)
+ //debug("XX found cached query for '" + search_lc + "'");
+ newResult.hitQueryCache = true;
+
+ //tmpResults = tmpResults.concat(searchACache(search_lc, this.queryCache[search_lc]));
+ for (var id in this.queryCache[search_lc])
{
- foundCachedSearchString = cachedSearchString;
- }
- }
+ var existing = false;
- if (foundCachedSearchString != "")
- {
- var foundQueryCache = this.queryCache[foundCachedSearchString];
+ for (var i=0; i<tmpResults.length; i++)
+ {
+ if (tmpResults[i].uid == id)
+ {
+ existing = true;
+ break;
+ }
+ }
- tmpResults = tmpResults.concat(foundQueryCache);
- }
+ if (!existing)
+ tmpResults.push(this.queryCache[search_lc][id]);
+ }
- var newResult = new FacebookRemoteAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 0, "", [], null, null);
+ //debug("XX have " + tmpResults.length + " results from bootstrap cache matches + query cache matches");
+ }
if (tmpResults.length > 0)
{
- tmpResults = tmpResults.sort(function(a, b) { return a.index - b.index; });
for (var i=0; i<tmpResults.length; i++)
{
newResult.appendMatch(
@@ -514,9 +566,6 @@ FacebookRemoteAutoCompleteSearch.prototype = {
}
}
- if (foundCachedSearchString != "")
- newResult.hitQueryCache = true;
-
return newResult;
},
@@ -534,6 +583,8 @@ FacebookRemoteAutoCompleteSearch.prototype = {
var self = this;
var newResult = new FacebookRemoteAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_NOMATCH, 0, "", [], null, null);
+ debug("in startSearch with '" + searchString + "'");
+
if (!this.fbSvc.loggedIn)
{
newResult._searchResult = Ci.nsIAutoCompleteResult.RESULT_IGNORED;
@@ -557,38 +608,42 @@ FacebookRemoteAutoCompleteSearch.prototype = {
return listener.onSearchResult(this, newResult);
}
- oldResult = this._lastResult;
+ //oldResult = this._lastResult;
// If the user has just added a space, just give them the old results
+ /*
if (oldResult && oldResult._searchString == searchString.trim())
{
oldResult._searchString = searchString;
this._lastResult = oldResult;
return listener.onSearchResult(this, oldResult);
}
+ */
- this._lastResult = newResult;
- this._last = searchString;
+ //this._lastResult = newResult;
+ //this._last = searchString;
var res = this.searchResultCache(searchString);
+ debug("results found in cache = " + res.matchCount);
+
+ if (self.queryTimeout)
+ {
+ try
+ {
+ self.queryTimeout.cancel();
+ } catch(e) {}
+
+ self.queryTimeout = null;
+ }
+
if (res.matchCount > 5 || res.hitQueryCache)
{
- this._lastResult = res;
+ //this._lastResult = res;
return listener.onSearchResult(this, res);
}
else
{
- if (self.queryTimeout)
- {
- try
- {
- self.queryTimeout.cancel();
- } catch(e) {}
-
- self.queryTimeout = null;
- }
-
var event = {
notify: function(timer) {
self.query(searchString, res, listener);

0 comments on commit 37925d1

Please sign in to comment.