Ver Fonte

feat(electron): add communication for updater

charlie há 4 anos atrás
pai
commit
3b62ed447c
3 ficheiros alterados com 74 adições e 38 exclusões
  1. 15 3
      resources/js/preload.js
  2. 32 35
      src/electron/electron/core.cljs
  3. 27 0
      src/electron/electron/updater.cljs

+ 15 - 3
resources/js/preload.js

@@ -1,7 +1,19 @@
-const {ipcRenderer, contextBridge} = require('electron');
+const { ipcRenderer, contextBridge } = require('electron')
 
 contextBridge.exposeInMainWorld('api', {
   doAction: async (arg) => {
-    return await ipcRenderer.invoke('main', arg);
+    return await ipcRenderer.invoke('main', arg)
+  },
+
+  checkForUpdates: async (...args) => {
+    await ipcRenderer.invoke('check-for-updates', ...args)
+  },
+
+  setUpdatesCallback (cb) {
+    if (typeof cb !== 'function') return
+
+    const channel = 'updates-callback'
+    ipcRenderer.removeAllListeners(channel)
+    ipcRenderer.on(channel, cb)
   }
-});
+})

+ 32 - 35
src/electron/electron/core.cljs

@@ -1,9 +1,9 @@
 (ns electron.core
   (:require [electron.handler :as handler]
+            [electron.updater :refer [init-updater]]
             ["fs" :as fs]
             ["path" :as path]
-            ["electron" :refer [BrowserWindow app] :as electron]
-            ["electron-updater" :refer [autoUpdater]]))
+            ["electron" :refer [BrowserWindow app] :as electron]))
 
 (defonce mac? (= (.-platform js/process) "darwin"))
 (defonce win32? (= (.-platform js/process) "win32"))
@@ -15,6 +15,10 @@
 (def ROOT_PATH (path/join js/__dirname ".."))
 (def MAIN_WINDOW_ENTRY (str "file://" (path/join js/__dirname (if dev? "dev.html" "index.html"))))
 
+(def ^:dynamic *setup-fn* nil)
+(def ^:dynamic *teardown-fn* nil)
+(def ^:dynamic *teardown-updater* nil)
+
 ;; Handle creating/removing shortcuts on Windows when installing/uninstalling.
 (when (js/require "electron-squirrel-startup") (.quit app))
 
@@ -24,42 +28,24 @@
   (let [win-opts {:width  980
                   :height 700
                   :webPreferences
-                  {:nodeIntegration false
+                  {:nodeIntegration         false
                    :nodeIntegrationInWorker false
-                   :contextIsolation true
-                   :preload (path/join js/__dirname "js/preload.js")}}
+                   :contextIsolation        true
+                   :preload                 (path/join js/__dirname "js/preload.js")}}
         url MAIN_WINDOW_ENTRY
         win (BrowserWindow. (clj->js win-opts))]
     (.loadURL win url)
     (when dev? (.. win -webContents (openDevTools)))
     win))
 
-(defn setup-updater! [notify-update-status]
-  ;; updater logging
-  (set! (.. autoUpdater -logger) log)
-  (set! (.. autoUpdater -logger -transports -file -level) "info")
-
+(defn setup-updater! [^js win]
   (.. log (info (str "Logseq App(" (.getVersion app) ") Starting... ")))
 
-  (let [init-updater (js/require "update-electron-app")]
-    (init-updater #js {:repo           "logseq/logseq"
-                       :updateInterval "1 hour"
-                       :logger         log}))
-  ;;; updater hooks
-  ;(doto autoUpdater
-  ;  (.on "checking-for-update" #(notify-update-status "checking for updating..."))
-  ;  (.on "update-not-available" #(notify-update-status "update not available"))
-  ;  (.on "error" #(notify-update-status %))
-  ;  (.on "download-progress"
-  ;       #(let [progress-clj (js->clj %)
-  ;              {:keys [percent transferred total]} progress-clj
-  ;              msg (str "Progress Downloaded " percent "%"
-  ;                       " (" transferred "/" total ")")]
-  ;          (notify-update-status msg)))
-  ;  (.on "update-downloaded" #(do (notify-update-status "update downloaded")
-  ;                                (.. autoUpdater quitAndInstall)))
-  ;  (.checkForUpdatesAndNotify))
-)
+  ;; manual updater
+  (set! *teardown-updater*
+        (init-updater {:repo   "logseq/logseq"
+                       :logger log
+                       :win    win})))
 
 (defn main
   []
@@ -70,11 +56,20 @@
                *win (atom win)
                *quitting? (atom false)]
 
-           ;; auto updater
-           (setup-updater! nil)
+           (set! *setup-fn*
+                 (fn []
+                   ;; updater
+                   (setup-updater! win)
+
+                   ;; handler
+                   (handler/set-ipc-handler! win)
+
+                   ;; teardown
+                   #(do
+                      (when *teardown-updater* (*teardown-updater*)))))
 
-           ;; init stuffs
-           (handler/set-ipc-handler! win)
+           ;; setup effects
+           (*setup-fn*)
 
            ;; main window events
            (.on win "close" #(if (or @*quitting? win32?)
@@ -85,7 +80,9 @@
            (.on app "activate" #(if @*win (.show win)))))))
 
 (defn start []
-  (js/console.log "Main - start"))
+  (js/console.log "Main - start")
+  (when *setup-fn* (*setup-fn*)))
 
 (defn stop []
-  (js/console.log "Main - stop"))
+  (js/console.log "Main - stop")
+  (when *teardown-fn* (*teardown-fn*)))

+ 27 - 0
src/electron/electron/updater.cljs

@@ -0,0 +1,27 @@
+(ns electron.updater
+  (:require ["electron" :refer [ipcMain]]
+            [promesa.core :as p]))
+
+(def *update-pending (atom nil))
+
+(defn check-for-updates
+  [{:keys [repo ^js logger ^js win]}]
+  (let [debug (partial (.-warn logger) "[updater]")
+        emit (fn [type payload]
+               (.. win -webContents
+                   (send "updates-callback" #js {:type type :payload payload})))]
+    (debug "check for updates #" repo)
+    (emit "update-ok" 1))
+  (p/resolved nil))
+
+(defn init-updater
+  [{:keys [repo logger] :as opts}]
+  (let [channel "check-for-updates"
+        listener (fn [e & args]
+                   (when-not @*update-pending
+                     (reset! *update-pending true)
+                     (p/finally
+                       (check-for-updates (merge opts {:args args}))
+                       #(reset! *update-pending nil))))]
+    (.handle ipcMain channel listener)
+    #(do (.removeHandler ipcMain channel) (reset! *update-pending nil))))