fix(electron): compatible ipc for js object

This commit is contained in:
charlie
2026-05-01 17:52:19 +08:00
parent e08a946404
commit 17a092d438
2 changed files with 23 additions and 4 deletions

View File

@@ -523,7 +523,9 @@
(p/let [message (decode-main-ipc-message args-js)
_ (vreset! message* message)
result (handle (or (utils/get-win-from-sender event) window) message)]
(sqlite-util/write-transit-str result))
(if (= (last message) "js-obj")
(bean/->js result)
(sqlite-util/write-transit-str result)))
(p/catch (fn [e]
(let [command (command-name @message*)]
(when-not (contains? #{"mkdir" "stat"} command)

View File

@@ -11,12 +11,29 @@
(sqlite-util/read-transit-str result)
result))
(defn- js-pure-object?
[x]
(or (array? x)
(and (some? x)
(= "[object Object]"
(.call (.-toString (.-prototype js/Object)) x))
(let [prototype (js/Object.getPrototypeOf x)]
(or (nil? prototype)
(nil? (js/Object.getPrototypeOf prototype)))))))
(defn- has-js-obj?
[args]
(some js-pure-object? args))
(defn ipc
[& args]
(when (util/electron?)
(p/let [payload (sqlite-util/write-transit-str (vec args))
maybe-result-str (js/window.apis.doAction payload)]
(maybe-read-transit maybe-result-str))))
(if-let [args (and (has-js-obj? args)
(conj (vec args) :js-obj))]
(js/window.apis.doAction (bean/->js args))
(p/let [payload (sqlite-util/write-transit-str args)
maybe-result-str (js/window.apis.doAction payload)]
(maybe-read-transit maybe-result-str)))))
(defn invoke
[channel & args]