Browse Source

feat(sync): trigger a remote->local when app awake from sleep

rcmerci 2 years ago
parent
commit
d36b06b171
3 changed files with 8 additions and 5 deletions
  1. 7 3
      src/main/frontend/fs/sync.cljs
  2. 1 0
      src/main/frontend/handler.cljs
  3. 0 2
      src/main/frontend/util.cljc

+ 7 - 3
src/main/frontend/fs/sync.cljs

@@ -2725,7 +2725,7 @@
                ^:mutable ratelimit-local-changes-chan
                *txid *txid-for-get-deletion-log
                ^:mutable state ^:mutable remote-change-chan ^:mutable *ws *stopped? *paused?
-               ^:mutable ops-chan
+               ^:mutable ops-chan ^:mutable app-awake-from-sleep-chan
                ;; control chans
                private-full-sync-chan private-remote->local-sync-chan
                private-remote->local-full-sync-chan private-pause-resume-chan]
@@ -2757,6 +2757,7 @@
 
   (start [this]
     (set! ops-chan (chan (async/dropping-buffer 10)))
+    (set! app-awake-from-sleep-chan (chan (async/sliding-buffer 1)))
     (set! *ws (atom nil))
     (set! remote-change-chan (ws-listen! graph-uuid *ws))
     (set! ratelimit-local-changes-chan (<ratelimit local->remote-syncer local-changes-revised-chan))
@@ -2765,6 +2766,7 @@
     (async/tap remote->local-sync-mult private-remote->local-sync-chan)
     (async/tap remote->local-full-sync-mult private-remote->local-full-sync-chan)
     (async/tap pause-resume-mult private-pause-resume-chan)
+    (async/tap util/app-wake-up-from-sleep-mult app-awake-from-sleep-chan)
     (go-loop []
       (let [{:keys [remote->local remote->local-full-sync local->remote-full-sync local->remote resume pause stop]}
             (async/alt!
@@ -2780,8 +2782,9 @@
                                                     vs     (cons v rest-v)]
                                                 (println "local changes:" vs)
                                                 {:local->remote vs})))
+              app-awake-from-sleep-chan {:remote->local true}
               (timeout (* 20 60 1000)) {:local->remote-full-sync true}
-              (timeout (* 5 60 1000)) {:remote->local true}
+              (timeout (* 10 60 1000)) {:remote->local true}
               :priority true)]
         (cond
           stop
@@ -3071,6 +3074,7 @@
         (async/untap remote->local-sync-mult private-remote->local-sync-chan)
         (async/untap remote->local-full-sync-mult private-remote->local-full-sync-chan)
         (async/untap pause-resume-mult private-pause-resume-chan)
+        (async/untap util/app-wake-up-from-sleep-mult app-awake-from-sleep-chan)
         (when ops-chan (async/close! ops-chan))
         (stop-local->remote! local->remote-syncer)
         (stop-remote->local! remote->local-syncer)
@@ -3104,7 +3108,7 @@
     (.set-local->remote-syncer! remote->local-syncer local->remote-syncer)
     (swap! *sync-state sync-state--update-current-syncing-graph-uuid graph-uuid)
     (->SyncManager user-uuid graph-uuid base-path *sync-state local->remote-syncer remote->local-syncer remoteapi-with-stop
-                   nil *txid *txid-for-get-deletion-log nil nil nil *stopped? *paused? nil (chan 1) (chan 1) (chan 1) (chan 1))))
+                   nil *txid *txid-for-get-deletion-log nil nil nil *stopped? *paused? nil nil (chan 1) (chan 1) (chan 1) (chan 1))))
 
 (defn sync-manager-singleton
   [user-uuid graph-uuid base-path repo txid *sync-state]

+ 1 - 0
src/main/frontend/handler.cljs

@@ -228,6 +228,7 @@
 
   (db/run-batch-txs!)
   (file/<ratelimit-file-writes!)
+  (util/<app-wake-up-from-sleep-loop (atom false))
 
   (when config/dev?
     (enable-datalog-console))

+ 0 - 2
src/main/frontend/util.cljc

@@ -1460,9 +1460,7 @@ Arg *stop: atom, reset to true to stop the loop"
            (if @*stop
              (println :<app-wake-up-from-sleep-loop :stop)
              (let [now-epoch (tc/to-epoch (t/now))]
-               (println :now (t/now))
                (when (< @*last-activated-at (- now-epoch 10))
-                 (println :wake {:last-activated-at @*last-activated-at :now now-epoch})
                  (async/>! app-wake-up-from-sleep-chan {:last-activated-at @*last-activated-at :now now-epoch}))
                (vreset! *last-activated-at now-epoch)
                (async/<! (async/timeout 5000))