Skip to content

Commit

Permalink
refactor: Move IHost to elin.protocol.host.rpc/IRpc
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Mar 17, 2024
1 parent f1e0122 commit ac9a98f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
20 changes: 11 additions & 9 deletions src/elin/component/lazy_host.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[clojure.core.async :as async]
[com.stuartsierra.component :as component]
[elin.protocol.host.rpc :as e.p.h.rpc]
[elin.protocol.rpc :as e.p.rpc]
[taoensso.timbre :as timbre]))

Expand All @@ -17,11 +18,12 @@
(let [[type & args] (async/<! ch)]
(case type
::request! (let [[ch & args] args
res (async/<! (apply e.p.rpc/request! host args))]
res (async/<! (apply e.p.h.rpc/request! host args))]
(async/put! ch res))
::notify! (apply e.p.rpc/notify! host args)
::response! (apply e.p.rpc/response! host args)
::flush! (e.p.rpc/flush! host)
::notify! (apply e.p.h.rpc/notify! host args)
::response! (apply e.p.h.rpc/response! host args)
::flush! (e.p.h.rpc/flush! host)

::call-function (let [[ch & args] args
res (async/<! (apply e.p.rpc/call-function host args))]
(async/put! ch res))
Expand All @@ -47,24 +49,24 @@
(set-host! [_ host]
(reset! host-store host))

e.p.rpc/IHost
e.p.h.rpc/IRpc
(request! [_ content]
(if-let [host @host-store]
(e.p.rpc/request! host content)
(e.p.h.rpc/request! host content)
(let [ch (async/promise-chan)]
(async/put! host-channel [::request! ch content])
ch)))
(notify! [_ content]
(if-let [host @host-store]
(e.p.rpc/notify! host content)
(e.p.h.rpc/notify! host content)
(async/put! host-channel [::notify! content])))
(response! [_ id error result]
(if-let [host @host-store]
(e.p.rpc/response! host id error result)
(e.p.h.rpc/response! host id error result)
(async/put! host-channel [::response! id error result])))
(flush! [_]
(if-let [host @host-store]
(e.p.rpc/flush! host)
(e.p.h.rpc/flush! host)
(async/put! host-channel [::flush!])))

e.p.rpc/IFunction
Expand Down
8 changes: 4 additions & 4 deletions src/elin/component/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
(catch Exception ex
[nil (ex-message ex)]))]
(when (e.p.h.rpc/request? message)
(e.p.rpc/response! host
(:id (e.p.h.rpc/parse-message message))
err res)
(e.p.rpc/flush! host))))))
(e.p.h.rpc/response! host
(:id (e.p.h.rpc/parse-message message))
err res)
(e.p.h.rpc/flush! host))))))

(defrecord Server
[;; COMPONENTS
Expand Down
12 changes: 6 additions & 6 deletions src/elin/component/server/nvim.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

(defrecord NvimHost
[output-stream response-manager]
e.p.rpc/IHost
e.p.h.rpc/IRpc
(request! [_ content]
(let [id (e.u.id/next-id)
ch (async/promise-chan)]
Expand All @@ -79,20 +79,20 @@

e.p.rpc/IFunction
(call-function [this method params]
(e.p.rpc/request! this ["nvim_call_function" [method params]]))
(e.p.h.rpc/request! this ["nvim_call_function" [method params]]))

(notify-function [this method params]
(e.p.rpc/notify! this ["nvim_call_function" [method params]]))
(e.p.h.rpc/notify! this ["nvim_call_function" [method params]]))

(echo-text [this text]
(e.p.rpc/notify! this ["nvim_echo" [[[text "Normal"]] false {}]]))
(e.p.h.rpc/notify! this ["nvim_echo" [[[text "Normal"]] false {}]]))
(echo-text [this text highlight]
(e.p.rpc/notify! this ["nvim_echo" [[[text highlight]] false {}]]))
(e.p.h.rpc/notify! this ["nvim_echo" [[[text highlight]] false {}]]))

(echo-message [this text]
(e.p.rpc/echo-message this text "Normal"))
(echo-message [this text highlight]
(e.p.rpc/notify! this ["nvim_echo" [[[text highlight]] true {}]])))
(e.p.h.rpc/notify! this ["nvim_echo" [[[text highlight]] true {}]])))

(defn start-server
[{:keys [host server-socket on-accept stop-signal]}]
Expand Down
10 changes: 5 additions & 5 deletions src/elin/component/server/vim.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

(defrecord VimHost
[output-stream response-manager]
e.p.rpc/IHost
e.p.h.rpc/IRpc
(request! [_ [method :as content]]
(let [id (cond
(= "call" method) (nth content 3)
Expand All @@ -76,20 +76,20 @@

e.p.rpc/IFunction
(call-function [this method params]
(e.p.rpc/request! this ["call" method params (e.u.id/next-id)]))
(e.p.h.rpc/request! this ["call" method params (e.u.id/next-id)]))

(notify-function [this method params]
(e.p.rpc/notify! this ["call" method params]))
(e.p.h.rpc/notify! this ["call" method params]))

(echo-text [this text]
(e.p.rpc/echo-text this text "Normal"))
(echo-text [this text highlight]
(e.p.rpc/notify! this ["call" "elin#internal#echo" [text highlight]]))
(e.p.h.rpc/notify! this ["call" "elin#internal#echo" [text highlight]]))

(echo-message [this text]
(e.p.rpc/echo-message this text "Normal"))
(echo-message [this text highlight]
(e.p.rpc/notify! this ["call" "elin#internal#echom" [text highlight]])))
(e.p.h.rpc/notify! this ["call" "elin#internal#echom" [text highlight]])))

(defn start-server
[{:keys [host server-socket on-accept stop-signal]}]
Expand Down
6 changes: 6 additions & 0 deletions src/elin/protocol/host/rpc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
(request? [this])
(response? [this])
(parse-message [this]))

(defprotocol IRpc
(request! [this content])
(notify! [this content])
(response! [this id error result])
(flush! [this]))
6 changes: 0 additions & 6 deletions src/elin/protocol/rpc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
(defprotocol ILazyHost
(set-host! [this host]))

(defprotocol IHost
(request! [this content])
(notify! [this content])
(response! [this id error result])
(flush! [this]))

(defprotocol IFunction
(call-function [this method params])
(notify-function [this method params])
Expand Down
5 changes: 3 additions & 2 deletions test/elin/component/lazy_host_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[clojure.core.async :as async]
[clojure.test :as t]
[com.stuartsierra.component :as component]
[elin.protocol.host.rpc :as e.p.h.rpc]
[elin.protocol.rpc :as e.p.rpc]
[elin.system :as e.system]
[elin.test-helper :as h]))
Expand All @@ -17,9 +18,9 @@
host (h/test-host {:handler #(do (swap! wrote conj %)
"OK")})]
(try
(e.p.rpc/notify! lazy-host ["before"])
(e.p.h.rpc/notify! lazy-host ["before"])
(e.p.rpc/set-host! lazy-host host)
(e.p.rpc/notify! lazy-host ["after"])
(e.p.h.rpc/notify! lazy-host ["after"])

(t/is (= [[2 "after"]] @wrote))

Expand Down
5 changes: 3 additions & 2 deletions test/elin/test_helper/host.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns elin.test-helper.host
(:require
[clojure.core.async :as async]
[elin.protocol.host.rpc :as e.p.h.rpc]
[elin.protocol.rpc :as e.p.rpc]
[elin.schema.server :as e.s.server]
[elin.test-helper.message :as h.message]
Expand All @@ -9,7 +10,7 @@

(defrecord TestHost ; {{{
[host-store outputs option]
e.p.rpc/IHost
e.p.h.rpc/IRpc
(request! [_ content]
(let [id (e.u.id/next-id)
{:keys [handler]} option
Expand All @@ -32,7 +33,7 @@

e.p.rpc/IFunction
(call-function [this method params]
(e.p.rpc/request! this ["test_call_function" [method params]]))
(e.p.h.rpc/request! this ["test_call_function" [method params]]))

(echo-text [_ text]
(swap! outputs conj text))
Expand Down

0 comments on commit ac9a98f

Please sign in to comment.