DEV: makes autocomplete debounced (#9941)

DEV: makes autocomplete debounced (#9941)

  • DEV: makes autocomplete debounced

  • better wording

diff --git a/app/assets/javascripts/discourse/app/lib/autocomplete.js b/app/assets/javascripts/discourse/app/lib/autocomplete.js
index eea9f8e..4b42b3b 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,9 +414,7 @@ export default function(options) {
     return options.triggerRule ? options.triggerRule(me[0], opts) : true;
   }
 
-  $(this).on("keyup.autocomplete", function(e) {
-    if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
-
+  function debouncedInput() {
     let cp = caretPosition(me[0]);
     const key = me[0].value[cp - 1];
 
@@ -426,7 +425,7 @@ export default function(options) {
           completeStart = cp - match[0].length;
           completeEnd = completeStart + match[0].length - 1;
           let term = match[0].substring(1, match[0].length);
-          updateAutoComplete(dataSource(term, options));
+          debouncedUpdateAutocomplete(term, options);
         }
       }
     }
@@ -439,15 +438,24 @@ export default function(options) {
           (!prevChar || allowedLettersRegex.test(prevChar))
         ) {
           completeStart = completeEnd = cp - 1;
-          updateAutoComplete(dataSource("", options));
+          debouncedUpdateAutocomplete("", options);
         }
       }
     } else if (completeStart !== null) {
       let term = me.val().substring(completeStart + (options.key ? 1 : 0), cp);
-      updateAutoComplete(dataSource(term, options));
+      debouncedUpdateAutocomplete(term, options);
     }
+  }
+
+  $(this).on("keyup.autocomplete", function(e) {
+    if ([keys.esc, keys.enter].indexOf(e.which) !== -1) return true;
+    debounce(this, debouncedInput, INPUT_DELAY);
   });
 
+  function debouncedUpdateAutocomplete(t, o) {
+    updateAutoComplete(dataSource(t, o));
+  }
+
   $(this).on("keydown.autocomplete", function(e) {
     var c, i, initial, prev, prevIsGood, stopFound, term, total, userToComplete;
     let cp;
@@ -496,7 +504,13 @@ export default function(options) {
           ) {
             completeStart = c;
             term = me[0].value.substring(c + 1, initial);
-            updateAutoComplete(dataSource(term, options));
+            debounce(
+              this,
+              debouncedUpdateAutocomplete,
+              term,
+              options,
+              INPUT_DELAY
+            );
             return true;
           }
         }
@@ -583,7 +597,13 @@ export default function(options) {
             closeAutocomplete();
           }
 
-          updateAutoComplete(dataSource(term, options));
+          debounce(
+            this,
+            debouncedUpdateAutocomplete,
+            term,
+            options,
+            INPUT_DELAY
+          );
           return true;
         default:
           completeEnd = cp;

GitHub sha: f9e71567

This commit appears in #9941 which was approved by eviltrout. It was merged by SamSaffron.