Browse Source

fix(iOS): don't pause if it's already resumed from background mode

Tienson Qin 2 years ago
parent
commit
6c8f6741f7
1 changed files with 37 additions and 29 deletions
  1. 37 29
      src/main/frontend/fs/sync.cljs

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

@@ -3135,36 +3135,44 @@
 
 (def finished-local->remote-chan (chan 1))
 
-(add-watch app-state-changed-cursor "sync"
-           (fn [_ _ _ {:keys [is-active?]}]
-             (cond
-               (mobile-util/native-android?)
-               ;; TODO: support background task on Android
-               (restart-if-stopped! is-active?)
-
-               (mobile-util/native-ios?)
-               (let [*task-id (atom nil)]
-                 (if is-active?
-                   (restart-if-stopped! is-active?)
-                   (when (state/get-current-file-sync-graph-uuid)
-                     (p/let [task-id (.beforeExit ^js BackgroundTask
-                                                  (fn []
-                                                    (go
-                                                      ;; Wait for file watcher events
-                                                      (<! (timeout 2000))
-                                                      (util/drain-chan finished-local->remote-chan)
-                                                      (<! (<sync-local->remote-now))
-                                                      ;; wait at most 20s
-                                                      (async/alts! [finished-local->remote-chan (timeout 20000)])
-                                                      (offer! pause-resume-chan is-active?)
-                                                      (<! (timeout 5000))
-                                                      (prn "finish task: " @*task-id)
-                                                      (let [opt #js {:taskId @*task-id}]
-                                                        (.finish ^js BackgroundTask opt)))))]
-                       (reset! *task-id task-id)))))
+(let [*resumed? (atom false)
+      *into-background? (atom false)]
+  (add-watch app-state-changed-cursor "sync"
+            (fn [_ _ _ {:keys [is-active?]}]
+              (cond
+                (mobile-util/native-android?)
+                ;; TODO: support background task on Android
+                (restart-if-stopped! is-active?)
 
-               :else
-               nil)))
+                (mobile-util/native-ios?)
+                (let [*task-id (atom nil)]
+                  (if is-active?
+                    (do
+                      (when @*into-background?
+                        (reset! *into-background? false)
+                        (reset! *resumed? true))
+                      (restart-if-stopped! is-active?))
+                    (when (state/get-current-file-sync-graph-uuid)
+                      (p/let [task-id (.beforeExit ^js BackgroundTask
+                                                   (fn []
+                                                     (reset! *resumed? false)
+                                                     (reset! *into-background? true)
+                                                     (go
+                                                       ;; Wait for file watcher events
+                                                       (<! (timeout 2000))
+                                                       (util/drain-chan finished-local->remote-chan)
+                                                       (<! (<sync-local->remote-now))
+                                                       ;; wait at most 20s
+                                                       (async/alts! [finished-local->remote-chan (timeout 20000)])
+                                                       (when-not @*resumed? (offer! pause-resume-chan is-active?))
+                                                       (<! (timeout 5000))
+                                                       (prn "finish task: " @*task-id)
+                                                       (let [opt #js {:taskId @*task-id}]
+                                                         (.finish ^js BackgroundTask opt)))))]
+                        (reset! *task-id task-id)))))
+
+                :else
+                nil))))
 
 ;;; ### some add-watches