Răsfoiți Sursa

enhance(sync): save queued files now

rcmerci 3 ani în urmă
părinte
comite
aaf2357a95

+ 5 - 1
src/main/frontend/components/file_sync.cljs

@@ -88,7 +88,11 @@
                                         (state/pub-event!
                                          [:modal/remote-encryption-input-pw-dialog current-repo current-graph
                                           :input-pwd-remote (fn [] (fs-sync/restore-pwd! (:GraphUUID current-graph)))]))}})
-
+          (and graph-txid-exists? idle?)
+          (concat
+           [{:title [:strong "Save files now"]
+             :options {:on-click
+                       #(as/offer! fs-sync/immediately-local->remote-chan true)}}])
           graph-txid-exists?
           (concat
            (map (fn [f] {:title [:div.file-item f]

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

@@ -40,15 +40,15 @@
 ;;
 ;; sync strategy:
 ;; - when toggle file-sync on,
-;;   trigger remote->local first, then local->remote-full-sync
-;;   local->remote-full-sync will compare local-files with remote-files (by md5 & size),
+;;   trigger remote->local-full-sync first, then local->remote-full-sync
+;;   local->remote-full-sync will compare local-files with remote-files (by md5),
 ;;   and upload new-added-files to remote server.
 ;; - if local->remote sync(normal-sync or full-sync) return :need-sync-remote,
 ;;   then trigger a remote->local sync
 ;; - if remote->local sync return :need-remote->local-full-sync,
 ;;   then we need a remote->local-full-sync,
 ;;   which compare local-files with remote-files, sync diff-remote-files to local
-;; - local->remote-full-sync will be triggered after 20min of idle
+;; - local->remote-full-sync will be triggered after 20mins of idle
 ;; - every 20s, flush local changes, and sync to remote
 
 ;; TODO: use access-token instead of id-token
@@ -1140,6 +1140,9 @@
 (def stop-sync-chan (chan 1))
 (def remote->local-sync-chan (chan 1))
 (def remote->local-full-sync-chan (chan 1))
+(def immediately-local->remote-chan
+  "Immediately trigger upload of files in waiting queue"
+  (chan))
 
 (defn sync-state
   "create a new sync-state"
@@ -1349,8 +1352,6 @@
         (and (<! (local-file-exists? r-path basepath))
              (<! (file-changed? graph-uuid r-path basepath)))))))
 
-
-
 (defrecord ^:large-vars/cleanup-todo
     Local->RemoteSyncer [user-uuid graph-uuid base-path repo *sync-state remoteapi
                          ^:mutable rate *txid ^:mutable remote->local-syncer stop-chan *stopped]
@@ -1389,7 +1390,8 @@
                       v)))))
          :flush-fn #(swap! *sync-state sync-state-reset-queued-local->remote-files)
          :stop-ch stop-chan
-         :distinct-coll? true)))
+         :distinct-coll? true
+         :flush-now-ch immediately-local->remote-chan)))
 
     (sync-local->remote! [_ es]
       (if (empty? es)
@@ -1471,10 +1473,6 @@
                     (recur (next es-partitions))
                     (or need-sync-remote unknown) r)))))))))
 
-
-
-
-
 ;;; ### put all stuff together
 
 (defrecord ^:large-vars/cleanup-todo

+ 19 - 12
src/main/frontend/util.cljc

@@ -1073,6 +1073,13 @@
 
 (defn keyname [key] (str (namespace key) "/" (name key)))
 
+#?(:cljs
+   (defn drain-chan
+     "drop all stuffs in CH, and return all of them"
+     [ch]
+     (->> (repeatedly #(async/poll! ch))
+          (take-while identity))))
+
 #?(:cljs
    (defn ratelimit
      "return a channel CH,
@@ -1083,19 +1090,24 @@
   - :flush-fn exec flush-fn when time to flush, (flush-fn item-coll)
   - :stop-ch stop go-loop when stop-ch closed
   - :distinct-coll? distinct coll when put into CH
-  - :chan-buffer buffer of return CH, default use (async/chan 1000)"
-     [in-ch max-duration & {:keys [filter-fn flush-fn stop-ch distinct-coll? chan-buffer]}]
+  - :chan-buffer buffer of return CH, default use (async/chan 1000)
+  - :flush-now-ch flush the content in the queue immediately"
+     [in-ch max-duration & {:keys [filter-fn flush-fn stop-ch distinct-coll? chan-buffer flush-now-ch]}]
      (let [ch (if chan-buffer (async/chan chan-buffer) (async/chan 1000))
-           stop-ch* (or stop-ch (async/chan))]
+           stop-ch* (or stop-ch (async/chan))
+           flush-now-ch* (or flush-now-ch (async/chan))]
        (async/go-loop [timeout-ch (async/timeout max-duration) coll []]
-         (let [{:keys [timeout e stop]}
+         (let [{:keys [timeout e stop flush-now]}
                (async/alt! timeout-ch {:timeout true}
                            in-ch ([e] {:e e})
-                           stop-ch* {:stop true})]
+                           stop-ch* {:stop true}
+                           flush-now-ch* {:flush-now true})]
            (cond
-             timeout
+             (or flush-now timeout)
              (do (async/onto-chan! ch coll false)
                  (flush-fn coll)
+                 (drain-chan flush-now-ch*)
+                 (when (= max-duration 20000) (println :ratelimit flush-now timeout))
                  (recur (async/timeout max-duration) []))
 
              (some? e)
@@ -1114,12 +1126,7 @@
              (async/close! ch))))
        ch)))
 
-#?(:cljs
-   (defn drain-chan
-     "drop all stuffs in CH, and return all of them"
-     [ch]
-     (->> (repeatedly #(async/poll! ch))
-          (take-while identity))))
+
 
 #?(:cljs
    (defn trace!