Browse Source

remove :network/unstable event

rcmerci 2 years ago
parent
commit
994b311d42

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

@@ -287,10 +287,9 @@
    (go
      (if (and *stop @*stop (contains? stoppable-apis api-name))
        :stop
-       (let [resp (<! (<request-once api-name body token))]
-         (if (= 0 (get-in resp [:resp :status]))
-           (state/pub-event! [:network/unstable true])
-           (state/pub-event! [:network/unstable false]))
+       (let [resp             (<! (<request-once api-name body token))
+             network-unstable (= 0 (get-in resp [:resp :status]))]
+         (state/set-network-unstable! network-unstable)
          (if (and
               (= 401 (get-in resp [:resp :status]))
               (= "Unauthorized" (:message (get-json-body (get-in resp [:resp :body])))))

+ 0 - 4
src/main/frontend/handler/events.cljs

@@ -889,10 +889,6 @@
                        [:p "Don't forget to re-index your graph when all the conflicts are resolved."]]
                       :status :error}]))
 
-(defmethod handle :network/unstable [[_ value]]
-  (state/set-network-unstable! value))
-
-
 (defn run!
   []
   (let [chan (state/get-events-chan)]

+ 12 - 6
src/main/frontend/handler/user.cljs

@@ -104,14 +104,21 @@
    (state/set-auth-refresh-token refresh-token)
    (set-token-to-localstorage! id-token access-token refresh-token)))
 
+(defn- <httpget-wrapper
+  [url]
+  (go
+    (let [resp (<! (http/get url {:with-credentials? false}))
+          network-unstable (= 0 (:status resp))]
+      (state/set-network-unstable! network-unstable)
+      resp)))
 
 (defn <refresh-id-token&access-token
   "Refresh id-token and access-token"
   []
   (go
     (when-let [refresh-token (state/get-auth-refresh-token)]
-      (let [resp (<! (http/get (str "https://" config/API-DOMAIN "/auth_refresh_token?refresh_token=" refresh-token)
-                               {:with-credentials? false}))]
+      (let [resp (<! (<httpget-wrapper
+                      (str "https://" config/API-DOMAIN "/auth_refresh_token?refresh_token=" refresh-token)))]
         (cond
           (and (<= 400 (:status resp))
                (> 500 (:status resp)))
@@ -129,8 +136,8 @@
           (clear-tokens true)
 
           :else                         ; ok
-        (when (and (:id_token (:body resp)) (:access_token (:body resp)))
-          (set-tokens! (:id_token (:body resp)) (:access_token (:body resp)))))))))
+          (when (and (:id_token (:body resp)) (:access_token (:body resp)))
+            (set-tokens! (:id_token (:body resp)) (:access_token (:body resp)))))))))
 
 (defn restore-tokens-from-localstorage
   "Refresh id-token&access-token, pull latest repos, returns nil when tokens are not available."
@@ -146,8 +153,7 @@
 (defn login-callback [code]
   (state/set-state! [:ui/loading? :login] true)
   (go
-    (let [resp (<! (http/get (str "https://" config/API-DOMAIN "/auth_callback?code=" code)
-                             {:with-credentials? false}))]
+    (let [resp (<! (<httpget-wrapper (str "https://" config/API-DOMAIN "/auth_callback?code=" code)))]
       (if (= 200 (:status resp))
         (-> resp
             :body