Browse Source

fix: sync status not refreshed

Tienson Qin 3 years ago
parent
commit
cd26853499

+ 36 - 35
src/main/frontend/components/file_sync.cljs

@@ -327,7 +327,7 @@
         current-repo            (state/get-current-repo)
         creating-remote-graph?  (state/sub [:ui/loading? :graph/create-remote?])
         current-graph-id        (state/get-current-file-sync-graph-uuid)
-        sync-state              (state/get-file-sync-state current-graph-id)
+        sync-state              (state/sub-file-sync-state current-graph-id)
         sync-progress           (state/sub [:file-sync/graph-state
                                             current-graph-id
                                             :file-sync/progress])
@@ -360,40 +360,41 @@
                                  (fn []
                                    (when-not (file-sync-handler/current-graph-sync-on?)
                                      (async/go
-                                       (async/<! (p->c (persist-var/-load fs-sync/graphs-txid)))
-                                       (cond
-                                         @*beta-unavailable?
-                                         (state/pub-event! [:file-sync/onboarding-tip :unavailable])
-
-                                         ;; current graph belong to other user, do nothing
-                                         (and (first @fs-sync/graphs-txid)
-                                              (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid)
-                                                                                               (first @fs-sync/graphs-txid))))
-                                         nil
-
-                                         (and synced-file-graph?
-                                              (second @fs-sync/graphs-txid)
-                                              (fs-sync/graph-sync-off? (second @fs-sync/graphs-txid))
-                                              (async/<! (fs-sync/<check-remote-graph-exists (second @fs-sync/graphs-txid))))
-                                         (do
-                                           (prn "sync start")
-                                           (fs-sync/sync-start))
-
-                                         ;; remote graph already has been deleted, clear repos first, then create-remote-graph
-                                         synced-file-graph?  ; <check-remote-graph-exists -> false
-                                         (do (state/set-repos!
-                                              (map (fn [r]
-                                                     (if (= (:url r) current-repo)
-                                                       (dissoc r :GraphUUID :GraphName :remote?)
-                                                       r))
-                                                (state/get-repos)))
-                                             (create-remote-graph-fn))
-
-                                         (second @fs-sync/graphs-txid) ; sync not started yet
-                                         nil
-
-                                         :else
-                                         (create-remote-graph-fn)))))
+                                       (let [graphs-txid fs-sync/graphs-txid]
+                                         (async/<! (p->c (persist-var/-load graphs-txid)))
+                                         (cond
+                                           @*beta-unavailable?
+                                           (state/pub-event! [:file-sync/onboarding-tip :unavailable])
+
+                                           ;; current graph belong to other user, do nothing
+                                           (and (first @graphs-txid)
+                                                (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid)
+                                                                                                 (first @graphs-txid))))
+                                           nil
+
+                                           (and synced-file-graph?
+                                                (second @graphs-txid)
+                                                (fs-sync/graph-sync-off? (second @graphs-txid))
+                                                (async/<! (fs-sync/<check-remote-graph-exists (second @graphs-txid))))
+                                           (do
+                                             (prn "sync start")
+                                             (fs-sync/sync-start))
+
+                                           ;; remote graph already has been deleted, clear repos first, then create-remote-graph
+                                           synced-file-graph?  ; <check-remote-graph-exists -> false
+                                           (do (state/set-repos!
+                                                (map (fn [r]
+                                                       (if (= (:url r) current-repo)
+                                                         (dissoc r :GraphUUID :GraphName :remote?)
+                                                         r))
+                                                  (state/get-repos)))
+                                               (create-remote-graph-fn))
+
+                                           (second @graphs-txid) ; sync not started yet
+                                           nil
+
+                                           :else
+                                           (create-remote-graph-fn))))))
                                  (debounce 1500))]
     (if creating-remote-graph?
       (ui/loading "")

+ 8 - 1
src/main/frontend/fs/sync.cljs

@@ -184,6 +184,12 @@
 ;; Warning: make sure to `persist-var/-load` graphs-txid before using it.
 (def graphs-txid (persist-var/persist-var nil "graphs-txid"))
 
+(defn get-graphs-txid
+  [graph-uuid]
+  ;; (when graph-uuid
+  ;;   (get @state/state [:file-sync/graph-state graph-uuid ]))
+  )
+
 (declare assert-local-txid<=remote-txid)
 (defn <update-graphs-txid!
   [latest-txid graph-uuid user-uuid repo]
@@ -264,6 +270,7 @@
        :api-name api-name
        :body body})))
 
+;; For debug
 (def *on-flying-request
   "requests not finished"
   (atom #{}))
@@ -2956,7 +2963,7 @@
                         ;; update global state when *sync-state changes
                         (add-watch *sync-state ::update-global-state
                                    (fn [_ _ _ n]
-                                     (state/set-file-sync-state repo n)))
+                                     (state/set-file-sync-state graph-uuid n)))
 
                         (state/set-state! [:file-sync/graph-state :current-graph-uuid] graph-uuid)
 

+ 6 - 5
src/main/frontend/handler/file_sync.cljs

@@ -20,7 +20,8 @@
 (def refresh-file-sync-component (atom false))
 
 
-(defn get-current-graph-uuid [] (second @sync/graphs-txid))
+(defn get-current-graph-uuid []
+  (state/get-current-file-sync-graph-uuid))
 
 (defn enable-sync?
   []
@@ -65,14 +66,14 @@
 
 (defn <delete-graph
   [graph-uuid]
-  (go
-    (when (= graph-uuid (get-current-graph-uuid))
+  (let [same-graph? (= graph-uuid (get-current-graph-uuid))]
+    (when same-graph?
       (<! (sync/<sync-stop)))
     (let [r (<! (sync/<delete-graph sync/remoteapi graph-uuid))]
       (if (instance? ExceptionInfo r)
         (notification/show! (str "Delete graph failed: " graph-uuid) :warning)
-        (let [[_ local-graph-uuid _] @sync/graphs-txid]
-          (when (= graph-uuid local-graph-uuid)
+        (do
+          (when same-graph?
             (sync/clear-graphs-txid! (state/get-current-repo))
             (swap! refresh-file-sync-component not))
           (notification/show! (str "Graph deleted") :success))))))

+ 9 - 7
src/main/frontend/state.cljs

@@ -238,14 +238,16 @@
      :file-sync/remote-graphs               {:loading false :graphs nil}
      :file-sync/set-remote-graph-password-result {}
 
-     ;; graph-uuid -> [:current-graph-uuid :file-sync/sync-manager :file-sync/sync-state]
+     ;; graph-uuid -> {:graphs-txid {}
+     ;;                :file-sync/sync-manager {}
+     ;;                :file-sync/sync-state {}
+     ;;                ;; {file-path -> payload}
+     ;;                :file-sync/progress {}
+     ;;                :file-sync/start-time {}
+     ;;                :file-sync/last-synced-at {}}
      :file-sync/graph-state                 {:current-graph-uuid nil
-                                             :file-sync/sync-manager nil
-                                             :file-sync/sync-state nil
-                                             ;; {file-path -> payload}
-                                             :file-sync/progress nil
-                                             :file-sync/start-time nil
-                                             :file-sync/last-synced-at nil}
+                                             ;; graph-uuid -> ...
+                                             }
 
      :encryption/graph-parsing?             false