Skip to content

Commit

Permalink
electron: add ipc layer
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Jan 20, 2021
1 parent abdad56 commit 8e5a589
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"codemirror": "^5.58.1",
"diff": "5.0.0",
"diff-match-patch": "^1.0.5",
"electron": "^11.2.0",
"fs": "^0.0.1-security",
"fuzzysort": "^1.1.4",
"gulp-cached": "^1.1.1",
Expand Down
7 changes: 1 addition & 6 deletions resources/dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ <h3>
</a>
</h3>
</div>
<script>
require('electron').ipcRenderer.on('hello', (e, v) => {
console.info('[hello] ', v)
})
</script>
</body>
</html>
</html>
7 changes: 7 additions & 0 deletions resources/js/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {ipcRenderer, contextBridge} = require('electron');

contextBridge.exposeInMainWorld('api', {
doAction: async (arg) => {
return await ipcRenderer.invoke('main', arg);
}
});
11 changes: 6 additions & 5 deletions src/electron/electron/core.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns electron.core
(:require [electron.init :refer [init-channel]]
[electron.handler :as handler]
(:require [electron.handler :as handler]
["fs" :as fs]
["path" :as path]
["electron" :refer [BrowserWindow app] :as electron]
Expand All @@ -25,8 +24,10 @@
(let [win-opts {:width 980
:height 700
:webPreferences
{:nodeIntegration true ;; FIXME
}}
{:nodeIntegration false
:nodeIntegrationInWorker false
:contextIsolation true
:preload (path/join js/__dirname "js/preload.js")}}
url MAIN_WINDOW_ENTRY
win (BrowserWindow. (clj->js win-opts))]
(.loadURL win url)
Expand Down Expand Up @@ -73,7 +74,7 @@
(setup-updater! nil)

;; init stuffs
(init-channel win)
(handler/set-ipc-handler! win)

;; main window events
(.on win "close" #(if (or @*quitting? win32?)
Expand Down
9 changes: 8 additions & 1 deletion src/electron/electron/handler.cljs
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
(ns electron.handler)
(ns electron.handler
(:require ["electron" :refer [ipcMain]]))

(defn set-ipc-handler! [window]
(.handle ipcMain "main"
(fn [event args-js]
(prn "receive event: " args-js)
args-js)))
9 changes: 0 additions & 9 deletions src/electron/electron/init.cljs

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/electron/ipc.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns electron.ipc
(:require [cljs-bean.core :as bean]
[promesa.core :as p]))

(defn ipc
[& args]
(js/window.api.doAction (bean/->js args)))
38 changes: 28 additions & 10 deletions src/main/frontend/fs/node.cljs
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
(ns frontend.fs.node
(:require [frontend.fs.protocol :as protocol]
[frontend.util :as util]
[clojure.string :as string]))
[clojure.string :as string]
[promesa.core :as p]
[electron.ipc :as ipc]))

;; (defonce fs (when (util/electron?)
;; (js/require "fs")))

;; (defonce path (when (util/electron?)
;; (js/require "path")))

;; (defn ls-dir [dir]
;; (->> (tree-seq
;; (fn [f] (.isDirectory (.statSync fs f) ()))
;; (fn [d] (map #(.join path d %) (.readdirSync fs d)))
;; dir)
;; (apply concat)
;; (doall)))

(defrecord Node []
protocol/Fs
(mkdir! [this dir]
nil)
(readdir [this dir]
nil)
(unlink! [this path opts]
nil)
(ipc/ipc "mkdir" {:path dir}))
(readdir [this dir] ; recursive
(ipc/ipc "readdir" {:dir dir}))
(unlink! [this path _opts]
(ipc/ipc "unlink" {:path path}))
(rmdir! [this dir]
nil)
(read-file [this dir path]
nil)
(ipc/ipc "readFile" {:path (str dir "/" path)}))
(write-file! [this repo dir path content opts]
nil)
(ipc/ipc "writeFile" {:path (str dir "/" path)
:content content}))
(rename! [this repo old-path new-path]
nil)
(ipc/ipc "rename" {:old-path old-path
:new-path new-path}))
(stat [this dir path]
nil))
(ipc/ipc "stat" {:path (str dir path)})))
Loading

0 comments on commit 8e5a589

Please sign in to comment.