From a168c59863720bf5e641931ec52e87f73012c234 Mon Sep 17 00:00:00 2001
From: Yannick Li <yannick.li@concordant.io>
Date: Tue, 20 Jul 2021 13:58:08 +0200
Subject: [PATCH] Refactor get timeout

---
 src/Components/CMDEd.tsx | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/Components/CMDEd.tsx b/src/Components/CMDEd.tsx
index 14dea64..73d3a06 100644
--- a/src/Components/CMDEd.tsx
+++ b/src/Components/CMDEd.tsx
@@ -74,7 +74,9 @@ export default class CMDEditor extends Component<
    * Callback used when new update received.
    */
   private updateCallback() {
+    clearTimeout(this.timeoutGet);
     this.pullValue();
+    this.setGetTimeout();
   }
 
   /**
@@ -156,7 +158,6 @@ export default class CMDEditor extends Component<
       return;
     }
     this.updateRGA();
-    clearTimeout(this.timeoutGet);
     this.props.collection.pull(client.utils.ConsistencyLevel.None);
     let newValue = "";
     this.props.session.transaction(client.utils.ConsistencyLevel.None, () => {
@@ -168,7 +169,6 @@ export default class CMDEditor extends Component<
 
     if (diffs.length === 1 && diffs[0][0] === DiffMatchPatch.DIFF_EQUAL) {
       // Same value
-      this.setGetTimeout();
       return;
     }
 
@@ -189,7 +189,6 @@ export default class CMDEditor extends Component<
       [textarea.selectionStart, textarea.selectionEnd] =
         this.updateCursorPosition(diffs, cursorStart, cursorEnd);
     }
-    this.setGetTimeout();
   }
 
   /**
@@ -295,7 +294,7 @@ export default class CMDEditor extends Component<
   switchConnection(): void {
     this.setState({ isConnected: !this.state.isConnected }, () => {
       if (this.state.isConnected) {
-        this.pullValue();
+        this.updateCallback();
       } else {
         clearTimeout(this.timeoutPush);
         clearTimeout(this.timeoutGet);
-- 
GitLab