[bitbake-devel] [PATCH 11/12] bitbake: toastergui: libtoaster: typeahead resiliency for slow server

Ed Bartosh ed.bartosh at linux.intel.com
Tue Aug 4 19:46:39 UTC 2015


From: Michael Wood <michael.g.wood at intel.com>

When we have a slow request we don't want to see the typeahead results
changing in the middle of typing the item. To do this we make sure that
requests to the typeahead can only happen sequentially and that if
results have been retrieved but the input is now empty we don't bother
showing the results.

Signed-off-by: Michael Wood <michael.g.wood at intel.com>
Signed-off-by: Ed Bartosh <ed.bartosh at linux.intel.com>
---
 lib/toaster/toastergui/static/js/libtoaster.js | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/lib/toaster/toastergui/static/js/libtoaster.js b/lib/toaster/toastergui/static/js/libtoaster.js
index 96e28b0..f6979f6 100644
--- a/lib/toaster/toastergui/static/js/libtoaster.js
+++ b/lib/toaster/toastergui/static/js/libtoaster.js
@@ -18,15 +18,24 @@ var libtoaster = (function (){
     if (!xhrUrl || xhrUrl.length === 0)
       throw("No url to typeahead supplied");
 
+    var xhrReq;
+
     jQElement.typeahead({
         source: function(query, process){
           xhrParams.search = query;
-          $.getJSON(xhrUrl, this.options.xhrParams, function(data){
+
+          /* If we have a request in progress don't fire off another one*/
+          if (xhrReq)
+            xhrReq.abort();
+
+          xhrReq = $.getJSON(xhrUrl, this.options.xhrParams, function(data){
             if (data.error !== "ok") {
               console.log("Error getting data from server "+data.error);
               return;
             }
 
+            xhrReq = null;
+
             return process(data.results);
           });
         },
@@ -41,6 +50,9 @@ var libtoaster = (function (){
             return 0;
           }
 
+          if (this.$element.val().length === 0)
+            return 0;
+
           return 1;
         },
         highlighter: function (item) {
@@ -52,6 +64,7 @@ var libtoaster = (function (){
         sorter: function (items) { return items; },
         xhrUrl: xhrUrl,
         xhrParams: xhrParams,
+        xhrReq: xhrReq,
     });
 
 
@@ -530,6 +543,9 @@ $(document).ready(function() {
     });
 
     $(document).ajaxError(function(event, jqxhr, settings, errMsg){
+      if (errMsg === 'abort')
+        return;
+
       console.warn("Problem with xhr call");
       console.warn(errMsg);
       console.warn(jqxhr.responseText);
-- 
2.1.4




More information about the bitbake-devel mailing list