Browse Source

fix: deleted remote graph

This can happens when switching between different graphs,
`frontend.fs.sync/graphs-txid` might not be loaded before starting
sync.
Tienson Qin 3 years ago
parent
commit
00c325ff35
2 changed files with 32 additions and 25 deletions
  1. 3 0
      src/main/frontend/components/file_sync.cljs
  2. 29 25
      src/main/frontend/fs/sync.cljs

+ 3 - 0
src/main/frontend/components/file_sync.cljs

@@ -1,5 +1,7 @@
 (ns frontend.components.file-sync
   (:require [cljs.core.async :as async]
+            [cljs.core.async.interop :refer [p->c]]
+            [frontend.util.persist-var :as persist-var]
             [clojure.string :as string]
             [electron.ipc :as ipc]
             [frontend.components.lazy-editor :as lazy-editor]
@@ -197,6 +199,7 @@
 
                                     (state/set-modal! confirm-fn {:center? true :close-btn? false})))
         turn-on                #(async/go
+                                  (async/<! (p->c (persist-var/-load fs-sync/graphs-txid)))
                                   (cond
                                     @*beta-unavailable?
                                     (state/pub-event! [:file-sync/onboarding-tip :unavailable])

+ 29 - 25
src/main/frontend/fs/sync.cljs

@@ -173,6 +173,7 @@
 
 (def ws-addr config/WS-URL)
 
+;; Warning: make sure to `persist-var/-load` graphs-txid before using it.
 (def graphs-txid (persist-var/persist-var nil "graphs-txid"))
 
 (declare assert-local-txid<=remote-txid)
@@ -2732,36 +2733,39 @@
       result)))
 
 (defn sync-start []
-  (let [[user-uuid graph-uuid txid] @graphs-txid
-        *sync-state                 (atom (sync-state))
+  (let [*sync-state                 (atom (sync-state))
         current-user-uuid           (user/user-uuid)
         repo                        (state/get-current-repo)]
     (go
       ;; stop previous sync
       (<! (<sync-stop))
-      (when (and user-uuid graph-uuid txid
-                 (user/logged-in?)
-                 repo
-                 (not (config/demo-graph? repo)))
-        (when-some [sm (sync-manager-singleton current-user-uuid graph-uuid
-                                               (config/get-repo-dir repo) repo
-                                               txid *sync-state)]
-          (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
-            (if-not (<! (<check-remote-graph-exists graph-uuid)) ; remote graph has been deleted
-              (clear-graphs-txid! repo)
-              (do
-                (state/set-file-sync-state repo @*sync-state)
-                (state/set-file-sync-manager sm)
-
-                ;; update global state when *sync-state changes
-                (add-watch *sync-state ::update-global-state
-                           (fn [_ _ _ n]
-                             (state/set-file-sync-state repo n)))
-
-                (.start sm)
-
-                (offer! remote->local-full-sync-chan true)
-                (offer! full-sync-chan true)))))))))
+
+      (<! (p->c (persist-var/-load graphs-txid)))
+
+      (let [[user-uuid graph-uuid txid] @graphs-txid]
+        (when (and user-uuid graph-uuid txid
+                   (user/logged-in?)
+                   repo
+                   (not (config/demo-graph? repo)))
+          (when-some [sm (sync-manager-singleton current-user-uuid graph-uuid
+                                                 (config/get-repo-dir repo) repo
+                                                 txid *sync-state)]
+            (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
+              (if-not (<! (<check-remote-graph-exists graph-uuid)) ; remote graph has been deleted
+                (clear-graphs-txid! repo)
+                (do
+                  (state/set-file-sync-state repo @*sync-state)
+                  (state/set-file-sync-manager sm)
+
+                  ;; update global state when *sync-state changes
+                  (add-watch *sync-state ::update-global-state
+                             (fn [_ _ _ n]
+                               (state/set-file-sync-state repo n)))
+
+                  (.start sm)
+
+                  (offer! remote->local-full-sync-chan true)
+                  (offer! full-sync-chan true))))))))))
 
 ;;; ### some add-watches