Browse Source

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 years ago
parent
commit
5a970d3ea9
3 changed files with 32 additions and 8 deletions
  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",
         "@emotion/styled": "^11.3.0",
         "@monaco-editor/react": "^4.1.3",
         "@monaco-editor/react": "^4.1.3",
         "framer-motion": "^4.1.17",
         "framer-motion": "^4.1.17",
+        "lodash.debounce": "^4.0.8",
         "react": "^17.0.2",
         "react": "^17.0.2",
         "react-dom": "^17.0.2",
         "react-dom": "^17.0.2",
         "react-icons": "^4.2.0",
         "react-icons": "^4.2.0",
@@ -20,6 +21,7 @@
         "use-local-storage-state": "^10.0.0"
         "use-local-storage-state": "^10.0.0"
       },
       },
       "devDependencies": {
       "devDependencies": {
+        "@types/lodash.debounce": "^4.0.6",
         "@types/react": "^17.0.8",
         "@types/react": "^17.0.8",
         "@types/react-dom": "^17.0.5",
         "@types/react-dom": "^17.0.5",
         "monaco-editor": "^0.23.0",
         "monaco-editor": "^0.23.0",
@@ -3863,6 +3865,15 @@
       "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.170.tgz",
       "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.170.tgz",
       "integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q=="
       "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": {
     "node_modules/@types/lodash.mergewith": {
       "version": "4.6.6",
       "version": "4.6.6",
       "resolved": "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz",
       "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",
       "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.170.tgz",
       "integrity": "sha512-bpcvu/MKHHeYX+qeEN8GE7DIravODWdACVA1ctevD8CN24RhPZIKMn9ntfAsrvLfSX3cR5RrBKAbYm9bGs0A+Q=="
       "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": {
     "@types/lodash.mergewith": {
       "version": "4.6.6",
       "version": "4.6.6",
       "resolved": "https://registry.npmjs.org/@types/lodash.mergewith/-/lodash.mergewith-4.6.6.tgz",
       "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",
     "@emotion/styled": "^11.3.0",
     "@monaco-editor/react": "^4.1.3",
     "@monaco-editor/react": "^4.1.3",
     "framer-motion": "^4.1.17",
     "framer-motion": "^4.1.17",
+    "lodash.debounce": "^4.0.8",
     "react": "^17.0.2",
     "react": "^17.0.2",
     "react-dom": "^17.0.2",
     "react-dom": "^17.0.2",
     "react-icons": "^4.2.0",
     "react-icons": "^4.2.0",
@@ -23,6 +24,7 @@
     "use-local-storage-state": "^10.0.0"
     "use-local-storage-state": "^10.0.0"
   },
   },
   "devDependencies": {
   "devDependencies": {
+    "@types/lodash.debounce": "^4.0.6",
     "@types/react": "^17.0.8",
     "@types/react": "^17.0.8",
     "@types/react-dom": "^17.0.5",
     "@types/react-dom": "^17.0.5",
     "monaco-editor": "^0.23.0",
     "monaco-editor": "^0.23.0",

+ 10 - 8
src/rustpad.ts

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