FIX: autocomplete username failing to pop up

Janky autocomplete behavior due to debounce not being accounted for
correctly when dealing with promises

This also fixes a promise leak
This commit is contained in:
Sam 2014-09-01 01:48:17 +10:00
parent 9aac004c0a
commit 5c244c6f8f
1 changed files with 13 additions and 12 deletions

View File

@ -10,7 +10,7 @@ function performSearch(term, topicId, includeGroups, resultsFn) {
var cached = cache[term]; var cached = cache[term];
if (cached) { if (cached) {
resultsFn(cached); resultsFn(cached);
return true; return;
} }
// need to be able to cancel this // need to be able to cancel this
@ -20,20 +20,20 @@ function performSearch(term, topicId, includeGroups, resultsFn) {
include_groups: includeGroups } include_groups: includeGroups }
}); });
var returnVal = CANCELLED_STATUS;
oldSearch.then(function (r) { oldSearch.then(function (r) {
cache[term] = r; cache[term] = r;
cacheTime = new Date(); cacheTime = new Date();
// If there is a newer search term, return null // If there is a newer search term, return null
if (term !== currentTerm) { r = CANCELLED_STATUS; } if (term === currentTerm) { returnVal = r; }
resultsFn(r);
}).always(function(){ }).always(function(){
oldSearch = null; oldSearch = null;
resultsFn(returnVal);
}); });
return true;
} }
var debouncedSearch = _.debounce(performSearch, 300); var debouncedSearch = _.debounce(performSearch, 300);
function organizeResults(r, options) { function organizeResults(r, options) {
@ -96,14 +96,15 @@ export default function userSearch(options) {
} }
cacheTopicId = topicId; cacheTopicId = topicId;
var executed = debouncedSearch(term, topicId, includeGroups, function(r) {
var clearPromise = setTimeout(function(){
resolve(CANCELLED_STATUS);
}, 5000);
debouncedSearch(term, topicId, includeGroups, function(r) {
clearTimeout(clearPromise);
resolve(organizeResults(r, options)); resolve(organizeResults(r, options));
}); });
// TODO: This doesn't cancel all debounced promises, we should figure out
// a way to handle that.
if (!executed) {
resolve(CANCELLED_STATUS);
}
}); });
} }