浏览代码

Optimization: debounce user cursor updates by 20ms

This noticeably improves performance on initial page load for documents
with large histories, as previously, the user sent one cursor update message
for every past edit to the document upon joining. Now, they only send a
single cursor update at the end of the update sequence, due to the
20ms debouncing operation.
Eric Zhang 3 年之前
父节点
当前提交
5a970d3ea9
共有 3 个文件被更改,包括 32 次插入8 次删除
  1. 20 0
      package-lock.json
  2. 2 0
      package.json
  3. 10 8
      src/rustpad.ts

+ 20 - 0
package-lock.json

@@ -12,6 +12,7 @@
         "@emotion/styled": "^11.3.0",
         "@monaco-editor/react": "^4.1.3",
         "framer-motion": "^4.1.17",
+        "lodash.debounce": "^4.0.8",
         "react": "^17.0.2",
         "react-dom": "^17.0.2",
         "react-icons": "^4.2.0",
@@ -20,6 +21,7 @@
         "use-local-storage-state": "^10.0.0"
       },
       "devDependencies": {
+        "@types/lodash.debounce": "^4.0.6",
         "@types/react": "^17.0.8",
         "@types/react-dom": "^17.0.5",
         "monaco-editor": "^0.23.0",
@@ -3863,6 +3865,15 @@
       "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.170.tgz",
       "integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q=="
     },
+    "node_modules/@types/lodash.debounce": {
+      "version": "4.0.6",
+      "resolved": "https://registry.npmjs.org/@types/lodash.debounce/-/lodash.debounce-4.0.6.tgz",
+      "integrity": "sha512-4WTmnnhCfDvvuLMaF3KV4Qfki93KebocUF45msxhYyjMttZDQYzHkO639ohhk8+oco2cluAFL3t5+Jn4mleylQ==",
+      "dev": true,
+      "dependencies": {
+        "@types/lodash": "*"
+      }
+    },
     "node_modules/@types/lodash.mergewith": {
       "version": "4.6.6",
       "resolved": "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz",
@@ -25608,6 +25619,15 @@
       "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.170.tgz",
       "integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q=="
     },
+    "@types/lodash.debounce": {
+      "version": "4.0.6",
+      "resolved": "https://registry.npmjs.org/@types/lodash.debounce/-/lodash.debounce-4.0.6.tgz",
+      "integrity": "sha512-4WTmnnhCfDvvuLMaF3KV4Qfki93KebocUF45msxhYyjMttZDQYzHkO639ohhk8+oco2cluAFL3t5+Jn4mleylQ==",
+      "dev": true,
+      "requires": {
+        "@types/lodash": "*"
+      }
+    },
     "@types/lodash.mergewith": {
       "version": "4.6.6",
       "resolved": "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz",

+ 2 - 0
package.json

@@ -15,6 +15,7 @@
     "@emotion/styled": "^11.3.0",
     "@monaco-editor/react": "^4.1.3",
     "framer-motion": "^4.1.17",
+    "lodash.debounce": "^4.0.8",
     "react": "^17.0.2",
     "react-dom": "^17.0.2",
     "react-icons": "^4.2.0",
@@ -23,6 +24,7 @@
     "use-local-storage-state": "^10.0.0"
   },
   "devDependencies": {
+    "@types/lodash.debounce": "^4.0.6",
     "@types/react": "^17.0.8",
     "@types/react-dom": "^17.0.5",
     "monaco-editor": "^0.23.0",

+ 10 - 8
src/rustpad.ts

@@ -4,6 +4,7 @@ import type {
   IDisposable,
   IPosition,
 } from "monaco-editor/esm/vs/editor/editor.api";
+import debounce from "lodash.debounce";
 
 /** Options passed in to the Rustpad constructor. */
 export type RustpadOptions = {
@@ -56,12 +57,15 @@ class Rustpad {
     this.onChangeHandle = options.editor.onDidChangeModelContent((e) =>
       this.onChange(e)
     );
-    this.onCursorHandle = options.editor.onDidChangeCursorPosition((e) =>
-      this.onCursor(e)
-    );
-    this.onSelectionHandle = options.editor.onDidChangeCursorSelection((e) =>
-      this.onSelection(e)
-    );
+    const cursorUpdate = debounce(() => this.sendCursorData(), 20);
+    this.onCursorHandle = options.editor.onDidChangeCursorPosition((e) => {
+      this.onCursor(e);
+      cursorUpdate();
+    });
+    this.onSelectionHandle = options.editor.onDidChangeCursorSelection((e) => {
+      this.onSelection(e);
+      cursorUpdate();
+    });
     this.beforeUnload = (event: BeforeUnloadEvent) => {
       if (this.outstanding) {
         event.preventDefault();
@@ -412,7 +416,6 @@ class Rustpad {
   private onCursor(event: editor.ICursorPositionChangedEvent) {
     const cursors = [event.position, ...event.secondaryPositions];
     this.cursorData.cursors = cursors.map((p) => unicodeOffset(this.model, p));
-    this.sendCursorData();
   }
 
   private onSelection(event: editor.ICursorSelectionChangedEvent) {
@@ -421,7 +424,6 @@ class Rustpad {
       unicodeOffset(this.model, s.getStartPosition()),
       unicodeOffset(this.model, s.getEndPosition()),
     ]);
-    this.sendCursorData();
   }
 }