Browse Source

fix: don't allow multiple instances at the moment

Resolved #1533
Tienson Qin 4 years ago
parent
commit
e5fcfa8d5e
2 changed files with 51 additions and 42 deletions
  1. 47 38
      src/electron/electron/core.cljs
  2. 4 4
      yarn.lock

+ 47 - 38
src/electron/electron/core.cljs

@@ -147,44 +147,53 @@
 
 
 (defn main
 (defn main
   []
   []
-  (.on app "window-all-closed" (fn [] (.quit app)))
-  (.on app "ready"
-       (fn []
-         (let [^js win (create-main-window)
-               _ (reset! *win win)
-               *quitting? (atom false)]
-           (.. logger (info (str "Logseq App(" (.getVersion app) ") Starting... ")))
-
-           (vreset! *setup-fn
-                    (fn []
-                      (let [t0 (setup-updater! win)
-                            t1 (setup-interceptor!)
-                            t2 (setup-app-manager! win)
-                            tt (handler/set-ipc-handler! win)]
-
-                        (vreset! *teardown-fn
-                                 #(doseq [f [t0 t1 t2 tt]]
-                                    (and f (f)))))))
-
-           ;; setup effects
-           (@*setup-fn)
-
-           ;; main window events
-           (.on win "close" (fn [e]
-                              (.preventDefault e)
-                              (let [web-contents (. win -webContents)]
-                                (.send web-contents "persistent-dbs"))
-                              (async/go
-                                ;; FIXME: What if persistence failed?
-                                (let [_ (async/<! state/persistent-dbs-chan)]
-                                  (if (or @*quitting? (not mac?))
-                                    (when-let [win @*win]
-                                      (destroy-window! win)
-                                      (reset! *win nil))
-                                    (do (.preventDefault ^js/Event e)
-                                        (.hide win)))))))
-           (.on app "before-quit" (fn [_e] (reset! *quitting? true)))
-           (.on app "activate" #(if @*win (.show win)))))))
+  (if-not (.requestSingleInstanceLock app)
+    (.quit app)
+    (do
+      (.on app "second-instance"
+           (fn [_event _commandLine _workingDirectory]
+             (when-let [win @*win]
+               (when (.isMinimized win)
+                 (.restore win))
+               (.focus win))))
+      (.on app "window-all-closed" (fn [] (.quit app)))
+      (.on app "ready"
+           (fn []
+             (let [^js win (create-main-window)
+                   _ (reset! *win win)
+                   *quitting? (atom false)]
+               (.. logger (info (str "Logseq App(" (.getVersion app) ") Starting... ")))
+
+               (vreset! *setup-fn
+                        (fn []
+                          (let [t0 (setup-updater! win)
+                                t1 (setup-interceptor!)
+                                t2 (setup-app-manager! win)
+                                tt (handler/set-ipc-handler! win)]
+
+                            (vreset! *teardown-fn
+                                     #(doseq [f [t0 t1 t2 tt]]
+                                        (and f (f)))))))
+
+               ;; setup effects
+               (@*setup-fn)
+
+               ;; main window events
+               (.on win "close" (fn [e]
+                                  (.preventDefault e)
+                                  (let [web-contents (. win -webContents)]
+                                    (.send web-contents "persistent-dbs"))
+                                  (async/go
+                                    ;; FIXME: What if persistence failed?
+                                    (let [_ (async/<! state/persistent-dbs-chan)]
+                                      (if (or @*quitting? (not mac?))
+                                        (when-let [win @*win]
+                                          (destroy-window! win)
+                                          (reset! *win nil))
+                                        (do (.preventDefault ^js/Event e)
+                                            (.hide win)))))))
+               (.on app "before-quit" (fn [_e] (reset! *quitting? true)))
+               (.on app "activate" #(if @*win (.show win)))))))))
 
 
 (defn start []
 (defn start []
   (js/console.log "Main - start")
   (js/console.log "Main - start")

+ 4 - 4
yarn.lock

@@ -3853,10 +3853,10 @@ mkdirp@^0.5.4, mkdirp@~0.5.1:
   dependencies:
   dependencies:
     minimist "^1.2.5"
     minimist "^1.2.5"
 
 
[email protected].5:
-  version "0.6.5"
-  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.5.tgz#51298589fcbb74ddef47fc8edc954d96e4e527e8"
-  integrity sha512-1NOunzlp+uGeFXXSaCstE+TfuvGAfuCiwLJnJXNklJhMqtRDzTr+wmMDplMAH6HVdWhxp9L9JY7g1YsoWJ0h5g==
[email protected].6:
+  version "0.6.6"
+  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.6.6.tgz#b38b64f83424514a2cb29e3a1f2cfc8cffaaca2c"
+  integrity sha512-oktdGC4KRDn5nSeAJY9Io55az+YlZg+k7om0Tuo0YeO3LU0hsxnKRB0h5dsy/lufDQn7Ay2EcSPlNs+oPBzi3A==
   dependencies:
   dependencies:
     yargs "^12.0.2"
     yargs "^12.0.2"