Procházet zdrojové kódy

fix(marketplace): download updates state when network issues

charlie před 3 roky
rodič
revize
53a8c8b96f

+ 8 - 6
src/main/frontend/components/plugins.cljs

@@ -413,11 +413,11 @@
                    {:title   [:span (ui/icon "file-code") "Open Preferences"]
                     :options {:on-click
                               #(p/let [root (plugin-handler/get-ls-dotdir-root)]
-                                 (js/apis.openPath (str root "/preferences.json")))}}
+                                      (js/apis.openPath (str root "/preferences.json")))}}
                    {:title   [:span (ui/icon "bug") "Open " [:code " ~/.logseq"]]
                     :options {:on-click
                               #(p/let [root (plugin-handler/get-ls-dotdir-root)]
-                                 (js/apis.openPath root))}}]))
+                                      (js/apis.openPath root))}}]))
         {})
 
       ;; developer
@@ -646,10 +646,12 @@
 
           :on-click
           #(when-not downloading?
-             (state/set-state! :plugin/updates-downloading? true)
-             (plugin-handler/check-or-update-marketplace-plugin
-               (assoc (state/get-next-selected-coming-update) :only-check false)
-               (fn [^js e] (notification/show! e :error))))
+             (plugin-handler/open-updates-downloading)
+             (if-let [n (state/get-next-selected-coming-update)]
+               (plugin-handler/check-or-update-marketplace-plugin
+                 (assoc n :only-check false)
+                 (fn [^js e] (notification/show! e :error)))
+               (plugin-handler/close-updates-downloading)))
 
           :disabled
           (or downloading?

+ 26 - 21
src/main/frontend/handler/events.cljs

@@ -240,27 +240,32 @@
     (util/make-el-into-viewport input)))
 
 (defmethod handle :plugin/consume-updates [[_ id pending? updated?]]
-  (when-let [coming (get-in @state/state [:plugin/updates-coming id])]
-    (notification/show!
-      (str "Checked: " (:title coming))
-      :success))
-
-  (if (and updated? (:plugin/updates-downloading? @state/state))
-    ;; try to start consume downloading item
-    (if-let [n (state/get-next-selected-coming-update)]
-      (plugin-handler/check-or-update-marketplace-plugin
-        (assoc n :only-check false)
-        (fn [^js e] (js/console.error "[Download Err]" n e)))
-      (state/set-state! :plugin/updates-downloading? false))
-
-    ;; try to start consume pending item
-    (if-let [n (second (first (:plugin/updates-pending @state/state)))]
-      (plugin-handler/check-or-update-marketplace-plugin
-        (assoc n :only-check true)
-        (fn [^js e] (js/console.error "[Check Err]" n e)))
-      ;; try to open waiting updates list
-      (when (and pending? (seq (state/all-available-coming-updates)))
-        (plugin/open-waiting-updates-modal!)))))
+  (let [downloading? (:plugin/updates-downloading? @state/state)]
+
+    (when-let [coming (and (not downloading?)
+                           (get-in @state/state [:plugin/updates-coming id]))]
+      (notification/show!
+        (str "Checked: " (:title coming))
+        :success))
+
+    (if (and updated? downloading?)
+      ;; try to start consume downloading item
+      (if-let [n (state/get-next-selected-coming-update)]
+        (plugin-handler/check-or-update-marketplace-plugin
+          (assoc n :only-check false :error-code nil)
+          (fn [^js e] (js/console.error "[Download Err]" n e)))
+        (plugin-handler/close-updates-downloading))
+
+      ;; try to start consume pending item
+      (if-let [n (second (first (:plugin/updates-pending @state/state)))]
+        (plugin-handler/check-or-update-marketplace-plugin
+          (assoc n :only-check true :error-code nil)
+          (fn [^js e]
+            (notification/show! (.toString e) :error)
+            (js/console.error "[Check Err]" n e)))
+        ;; try to open waiting updates list
+        (when (and pending? (seq (state/all-available-coming-updates)))
+          (plugin/open-waiting-updates-modal!))))))
 
 (defn run!
   []

+ 40 - 17
src/main/frontend/handler/plugin.cljs

@@ -96,12 +96,16 @@
     (p/catch
       (p/then
         (do (state/set-state! :plugin/installing pkg)
-            (load-marketplace-plugins false))
+            (p/catch
+              (load-marketplace-plugins false)
+              (fn [^js e]
+                (state/reset-all-updates-state)
+                (throw e))))
         (fn [mfts]
           (if-let [mft (some #(if (= (:id %) id) %) mfts)]
             (do
               (ipc/ipc "updateMarketPlugin" (merge (dissoc pkg :logger) mft)))
-            (throw (js/Error. (str ":central-not-matched " id))))
+            (throw (js/Error. (str ":not-found-in-marketplace" id))))
           true))
 
       (fn [^js e]
@@ -116,6 +120,23 @@
     (catch js/Error e
       nil)))
 
+(defn open-updates-downloading
+  []
+  (when (and (not (:plugin/updates-downloading? @state/state))
+             (seq (state/all-available-coming-updates)))
+    (->> (:plugin/updates-coming @state/state)
+         (map #(if (state/coming-update-new-version? (second %1))
+                 (update % 1 dissoc :error-code) %1))
+         (into {})
+         (state/set-state! :plugin/updates-coming))
+    (state/set-state! :plugin/updates-downloading? true)))
+
+(defn close-updates-downloading
+  []
+  (when (:plugin/updates-downloading? @state/state)
+    (state/set-state! :plugin/updates-downloading? false)))
+
+
 (defn setup-install-listener!
   [t]
   (let [channel (name :lsp-installed)
@@ -140,7 +161,7 @@
                                       (str (t :plugin/update) (t :plugins) ": " name " - " (.-version (.-options pl))) :success)
                                     (state/consume-updates-coming-plugin payload true))))
 
-                             (do                            ;; register new
+                             (do    ;; register new
                                (p/then
                                  (js/LSPluginCore.register (bean/->js {:key id :url dst}))
                                  (fn [] (if theme (js/setTimeout #(select-a-plugin-theme id) 300))))
@@ -155,16 +176,18 @@
                                           [(str (t :plugin/up-to-date) " :)") :success]
 
                                           [error-code :error])
-                             updates-pending? (seq (:plugin/updates-pending @state/state))]
+                             pending? (seq (:plugin/updates-pending @state/state))]
 
-                         (if (and only-check updates-pending?)
+                         (if (and only-check pending?)
                            (state/consume-updates-coming-plugin payload false)
 
-                           ;; TODO: consume failed download updates?
-                           (notifications/show!
-                             (str
-                               (if (= :error type) "[Install Error]" "")
-                               msg) type))
+                           (do
+                             ;; consume failed download updates
+                             (state/consume-updates-coming-plugin payload true)
+                             (notifications/show!
+                               (str
+                                 (if (= :error type) "[Install Error]" "")
+                                 msg) type)))
 
                          (js/console.error payload))
 
@@ -301,9 +324,9 @@
       ;; local
       (-> (p/let [content (invoke-exported-api "load_plugin_readme" url)
                   content (parse-user-md-content content item)]
-            (and (string/blank? (string/trim content)) (throw nil))
-            (state/set-state! :plugin/active-readme [content item])
-            (state/set-sub-modal! (fn [_] (display))))
+                 (and (string/blank? (string/trim content)) (throw nil))
+                 (state/set-state! :plugin/active-readme [content item])
+                 (state/set-sub-modal! (fn [_] (display))))
           (p/catch #(do (js/console.warn %)
                         (notifications/show! "No README content." :warn))))
       ;; market
@@ -313,8 +336,8 @@
   []
   (when util/electron?
     (p/let [path (ipc/ipc "openDialog")]
-      (when-not (:plugin/selected-unpacked-pkg @state/state)
-        (state/set-state! :plugin/selected-unpacked-pkg path)))))
+           (when-not (:plugin/selected-unpacked-pkg @state/state)
+             (state/set-state! :plugin/selected-unpacked-pkg path)))))
 
 (defn reset-unpacked-state
   []
@@ -356,7 +379,7 @@
   (p/catch
     (p/let [files ^js (ipc/ipc "getUserDefaultPlugins")
             files (js->clj files)]
-      (map #(hash-map :url %) files))
+           (map #(hash-map :url %) files))
     (fn [e]
       (js/console.error e))))
 
@@ -367,7 +390,7 @@
                             ;; TODO: too many requests may be limited by Github api
                             (seq (take 32 (state/get-enabled-installed-plugins theme?))))]
       (state/set-state! :plugin/updates-pending
-        (into {} (map (fn [v] [(keyword (:id v)) v]) plugins)))
+                        (into {} (map (fn [v] [(keyword (:id v)) v]) plugins)))
       (state/pub-event! [:plugin/consume-updates]))))
 
 ;; components

+ 17 - 10
src/main/frontend/state.cljs

@@ -1015,7 +1015,7 @@
                   (ipc/ipc "userAppCfgs")
                   (:electron/user-cfgs @state))
            cfgs (if (object? cfgs) (bean/->clj cfgs) cfgs)]
-     (set-state! :electron/user-cfgs cfgs))))
+          (set-state! :electron/user-cfgs cfgs))))
 
 (defn setup-electron-updater!
   []
@@ -1694,10 +1694,10 @@
 (defn get-enabled-installed-plugins
   [theme?]
   (filterv
-   #(and (:iir %)
-         (not (get-in % [:settings :disabled]))
-         (= (boolean theme?) (:theme %)))
-   (vals (:plugin/installed-plugins @state))))
+    #(and (:iir %)
+          (not (get-in % [:settings :disabled]))
+          (= (boolean theme?) (:theme %)))
+    (vals (:plugin/installed-plugins @state))))
 
 (defn lsp-enabled?-or-theme
   []
@@ -1709,7 +1709,9 @@
     (let [pending? (boolean (seq (:plugin/updates-pending @state)))]
       (swap! state update :plugin/updates-pending dissoc id)
       (if updated?
-        (swap! state update :plugin/updates-coming dissoc id)
+        (if-let [error (:error-code payload)]
+          (swap! state update-in [:plugin/updates-coming id] assoc :error-code error)
+          (swap! state update :plugin/updates-coming dissoc id))
         (swap! state update :plugin/updates-coming assoc id payload))
       (pub-event! [:plugin/consume-updates id pending? updated?]))))
 
@@ -1731,10 +1733,8 @@
   []
   (when-let [updates (all-available-coming-updates)]
     (let [unchecked (:plugin/updates-unchecked @state)]
-      (first
-       (if (seq unchecked)
-         (filter #(not (contains? unchecked (:id %))) updates)
-         updates)))))
+      (first (filter #(and (not (and (seq unchecked) (contains? unchecked (:id %))))
+                           (not (:error-code %))) updates)))))
 
 (defn set-unchecked-update
   [id unchecked?]
@@ -1744,6 +1744,13 @@
   []
   (swap! state assoc :plugin/updates-unchecked #{}))
 
+(defn reset-all-updates-state
+  []
+  (swap! state assoc
+         :plugin/updates-pending                {}
+         :plugin/updates-coming                 {}
+         :plugin/updates-downloading?           false))
+
 (defn sub-right-sidebar-blocks
   []
   (when-let [current-repo (get-current-repo)]