Skip to content

Commit

Permalink
feat(sync): allow cancel all request
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf authored and tiensonqin committed Sep 29, 2022
1 parent 4e96468 commit b73829d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/electron/electron/file_sync_rsapi.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
(defn decrypt-with-passphrase [passphrase data]
(rsapi/ageDecryptWithPassphrase passphrase data))

(defn cancel-all-requests []
(rsapi/cancelAllRequests))

(defonce progress-notify-chan "file-sync-progress")
(set-progress-callback (fn [error progress-info]
(when-not error
Expand Down
3 changes: 3 additions & 0 deletions src/electron/electron/handler.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@
(defmethod handle :decrypt-with-passphrase [_ args]
(apply rsapi/decrypt-with-passphrase (rest args)))

(defmethod handle :cancel-all-requests [_ args]
(apply rsapi/cancel-all-requests (rest args)))

(defmethod handle :default [args]
(logger/error "Error: no ipc handler for:" args))

Expand Down
3 changes: 3 additions & 0 deletions src/main/frontend/components/file_sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@
;; options
{:outer-header
[:<>
(ui/button "stop syncing"
:on-click (fn []
(fs-sync/rsapi-cancel-all-requests)))
(when (util/electron?)
(indicator-progress-pane
sync-state sync-progress
Expand Down
47 changes: 28 additions & 19 deletions src/main/frontend/fs/sync.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@
(<update-remote-files [this graph-uuid base-path filepaths local-txid] "local -> remote, return err or txid")
(<delete-remote-files [this graph-uuid base-path filepaths local-txid] "return err or txid")
(<encrypt-fnames [this graph-uuid fnames])
(<decrypt-fnames [this graph-uuid fnames]))
(<decrypt-fnames [this graph-uuid fnames])
(<cancel-all-requests [this]))

(defprotocol IRemoteAPI
(<user-info [this] "user info")
Expand Down Expand Up @@ -794,10 +795,12 @@
#(p->c (ipc/ipc "delete-remote-files" graph-uuid base-path filepaths local-txid token)))))))
(<encrypt-fnames [_ graph-uuid fnames] (go (js->clj (<! (p->c (ipc/ipc "encrypt-fnames" graph-uuid fnames))))))
(<decrypt-fnames [_ graph-uuid fnames] (go
(let [r (<! (p->c (ipc/ipc "decrypt-fnames" graph-uuid fnames)))]
(if (instance? ExceptionInfo r)
(ex-info "decrypt-failed" {:fnames fnames} (ex-cause r))
(js->clj r))))))
(let [r (<! (p->c (ipc/ipc "decrypt-fnames" graph-uuid fnames)))]
(if (instance? ExceptionInfo r)
(ex-info "decrypt-failed" {:fnames fnames} (ex-cause r))
(js->clj r)))))
(<cancel-all-requests [_] (go
(<! (p->c (ipc/ipc "cancel-all-requests"))))))


(deftype ^:large-vars/cleanup-todo CapacitorAPI [^:mutable graph-uuid' ^:mutable private-key ^:mutable public-key']
Expand Down Expand Up @@ -873,10 +876,10 @@
(let [token (<! (<get-token this))
r (<! (<retry-rsapi
#(p->c (.updateLocalVersionFiles mobile-util/file-sync
(clj->js {:graphUUID graph-uuid
:basePath base-path
:filePaths filepaths
:token token})))))]
(clj->js {:graphUUID graph-uuid
:basePath base-path
:filePaths filepaths
:token token})))))]
r)))

(<delete-local-files [_ graph-uuid base-path filepaths]
Expand All @@ -903,15 +906,15 @@

(<delete-remote-files [this graph-uuid _base-path filepaths local-txid]
(go
(let [token (<! (<get-token this))
r (<! (p->c (.deleteRemoteFiles mobile-util/file-sync
(clj->js {:graphUUID graph-uuid
:filePaths filepaths
:txid local-txid
:token token}))))]
(if (instance? ExceptionInfo r)
r
(get (js->clj r) "txid")))))
(let [token (<! (<get-token this))
r (<! (p->c (.deleteRemoteFiles mobile-util/file-sync
(clj->js {:graphUUID graph-uuid
:filePaths filepaths
:txid local-txid
:token token}))))]
(if (instance? ExceptionInfo r)
r
(get (js->clj r) "txid")))))

(<encrypt-fnames [_ graph-uuid fnames]
(go
Expand All @@ -927,7 +930,9 @@
:filePaths fnames}))))]
(if (instance? ExceptionInfo r)
(ex-info "decrypt-failed" {:fnames fnames} (ex-cause r))
(get (js->clj r) "value"))))))
(get (js->clj r) "value")))))
(<cancel-all-requests [_]
(go (<! (p->c (.cancelAllRequest mobile-util/file-sync))))))

(def rsapi (cond
(util/electron?)
Expand All @@ -942,6 +947,10 @@
:else
nil))

(defn rsapi-cancel-all-requests []
(when rsapi
(<! (<cancel-all-requests rsapi))))

;;; ### remote & rs api exceptions
(defn sync-stop-when-api-flying?
[exp]
Expand Down

0 comments on commit b73829d

Please sign in to comment.