diff --git a/src/main/frontend/components/file_sync.cljs b/src/main/frontend/components/file_sync.cljs index e6f9c2d33..ba1c6a768 100644 --- a/src/main/frontend/components/file_sync.cljs +++ b/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/c (persist-var/-load fs-sync/graphs-txid))) (cond @*beta-unavailable? (state/pub-event! [:file-sync/onboarding-tip :unavailable]) diff --git a/src/main/frontend/fs/sync.cljs b/src/main/frontend/fs/sync.cljs index 04bc2cce6..5cff918e7 100644 --- a/src/main/frontend/fs/sync.cljs +++ b/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 (c (persist-var/-load graphs-txid))) - (.start sm) + (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 (local-full-sync-chan true) - (offer! full-sync-chan true))))))))) + ;; 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