DEV: allows autocomplete to be debounced (#10166)

DEV: allows autocomplete to be debounced (#10166)

Usage:

$(".foo").autocomplete({debounced: true});
diff --git a/app/assets/javascripts/discourse/app/components/group-selector.js b/app/assets/javascripts/discourse/app/components/group-selector.js
index 4fb6d1b..e39aade 100644
--- a/app/assets/javascripts/discourse/app/components/group-selector.js
+++ b/app/assets/javascripts/discourse/app/components/group-selector.js
@@ -25,6 +25,7 @@ export default Component.extend({
     let groupNames = this.groupNames;
 
     $(this.element.querySelector("input")).autocomplete({
+      debounced: true,
       allowAny: false,
       items: _.isArray(groupNames)
         ? groupNames
diff --git a/app/assets/javascripts/discourse/app/lib/autocomplete.js b/app/assets/javascripts/discourse/app/lib/autocomplete.js
index a951683..1b2a0b6 100644
--- a/app/assets/javascripts/discourse/app/lib/autocomplete.js
+++ b/app/assets/javascripts/discourse/app/lib/autocomplete.js
@@ -1,4 +1,5 @@
-import { cancel, later } from "@ember/runloop";
+import { INPUT_DELAY } from "discourse-common/config/environment";
+import { debounce, cancel, later } from "@ember/runloop";
 import { iconHTML } from "discourse-common/lib/icon-library";
 import { setCaretPosition, caretPosition } from "discourse/lib/utilities";
 import Site from "discourse/models/site";
@@ -413,6 +414,14 @@ export default function(options) {
   }
 
   $(this).on("keyup.autocomplete", function(e) {
+    if (options.debounced) {
+      debounce(this, performAutocomplete, e, INPUT_DELAY);
+    } else {
+      performAutocomplete(e);
+    }
+  });
+
+  function performAutocomplete(e) {
     if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
 
     let cp = caretPosition(me[0]);
@@ -445,7 +454,7 @@ export default function(options) {
       let term = me.val().substring(completeStart + (options.key ? 1 : 0), cp);
       updateAutoComplete(dataSource(term, options));
     }
-  });
+  }
 
   $(this).on("keydown.autocomplete", function(e) {
     var c, i, initial, prev, prevIsGood, stopFound, term, total, userToComplete;

GitHub sha: 789a15a8

1 Like

This commit appears in #10166 which was merged by jjaffeux.