Pārlūkot izejas kodu

[editor] Throttle the predict typeahead and cancel any running predicts on change

Johan Åhlén 4 gadi atpakaļ
vecāks
revīzija
ae0f2e4b43

+ 46 - 40
desktop/core/src/desktop/js/apps/editor/components/aceEditor/acePredict.ts

@@ -63,47 +63,31 @@ export const attachPredictTypeahead = (
   let lastPrediction: string | undefined;
 
   const updatePredictTypeahead = () => {
-    if (activePredictPromise) {
-      try {
-        activePredictPromise.cancel();
-      } catch {}
-    }
-    defer(() => {
-      const editorText = editor.getValue();
-      if (
-        activePredict &&
-        (!editorText.length ||
-          activePredict.text === editorText ||
-          activePredict.text.indexOf(editorText) !== 0)
-      ) {
-        removeActivePredict();
-      }
-      try {
-        if (editorText.length && !activePredict) {
-          sqlAnalyzer
-            .predict({
-              beforeCursor: editor.getTextBeforeCursor(),
-              afterCursor: editor.getTextAfterCursor()
-            })
-            .then(({ prediction }) => {
-              if (prediction !== lastPrediction) {
-                const beforeCursor = editor.getTextBeforeCursor();
-                if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
-                  setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
-                } else {
-                  removeActivePredict();
-                }
+    const editorText = editor.getValue();
+
+    try {
+      if (editorText.length && !activePredict) {
+        activePredictPromise = sqlAnalyzer.predict({
+          beforeCursor: editor.getTextBeforeCursor(),
+          afterCursor: editor.getTextAfterCursor()
+        });
+        activePredictPromise
+          .then(({ prediction }) => {
+            if (prediction !== lastPrediction) {
+              const beforeCursor = editor.getTextBeforeCursor();
+              if (prediction && prediction.toLowerCase().startsWith(beforeCursor.toLowerCase())) {
+                setActivePredict(beforeCursor + prediction.slice(beforeCursor.length));
+              } else {
+                removeActivePredict();
               }
-              lastPrediction = prediction;
-            })
-            .catch(() => {
-              removeActivePredict();
-            });
-        }
-      } catch {
-        removeActivePredict();
+            }
+            lastPrediction = prediction;
+          })
+          .catch(removeActivePredict);
       }
-    });
+    } catch {
+      removeActivePredict();
+    }
   };
 
   editor.commands.addCommand({
@@ -140,8 +124,30 @@ export const attachPredictTypeahead = (
     }
   });
 
+  let predictThrottle = -1;
+
   const predictOnInput = () => {
-    updatePredictTypeahead();
+    if (activePredictPromise) {
+      try {
+        activePredictPromise.cancel();
+        activePredictPromise = undefined;
+      } catch {}
+    }
+    defer(() => {
+      window.clearTimeout(predictThrottle);
+
+      const editorText = editor.getValue();
+      if (
+        activePredict &&
+        (!editorText.length ||
+          activePredict.text === editorText ||
+          activePredict.text.indexOf(editorText) !== 0)
+      ) {
+        removeActivePredict();
+      }
+
+      predictThrottle = window.setTimeout(updatePredictTypeahead, 300);
+    });
   };
 
   editor.on('input', predictOnInput);

+ 2 - 3
desktop/core/src/desktop/js/catalog/analyzer/sqlAnalyzerRepository.ts

@@ -14,11 +14,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import NoopSqlAnalyzer from './NoopSqlAnalyzer';
 import CombinedSqlAnalyser from './CombinedSqlAnalyser';
+import NoopSqlAnalyzer from './NoopSqlAnalyzer';
+import { SqlAnalyzer, SqlAnalyzerProvider } from './types';
 import { Connector } from 'config/types';
-import { hueWindow } from 'types/types';
-import { SqlAnalyzer, SqlAnalyzerProvider, SqlAnalyzerMode } from './types';
 
 const sqlAnalyzerInstances: { [connectorId: string]: SqlAnalyzer | undefined } = {};