Skip to content

Commit d3550b8

Browse files
committed
Unify runtime rpc handling between different platforms
1 parent 2c5cb8d commit d3550b8

File tree

4 files changed

+55
-79
lines changed

4 files changed

+55
-79
lines changed

src/portal/runtime/clr/server.clj

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
(:require [clojure.edn :as edn]
33
[clojure.string :as str]
44
[portal.runtime :as rt]
5-
[portal.runtime.clr.client :as c]
65
[portal.runtime.fs :as fs]
76
[portal.runtime.index :as index]
8-
[portal.runtime.json :as json])
7+
[portal.runtime.json :as json]
8+
[portal.runtime.rpc :as rpc])
99
(:import (clojure.lang RT)
1010
(System Environment Guid)
1111
(System.IO Path)
@@ -16,11 +16,6 @@
1616

1717
(defmulti route (juxt :request-method :uri))
1818

19-
(def ^:private ops (merge c/ops rt/ops))
20-
21-
(defn- not-found [_request done]
22-
(done {:status :not-found}))
23-
2419
(defmacro array-segment [& args]
2520
`(new ~(RT/classForName "System.ArraySegment`1[System.Byte]") ~@args))
2621

@@ -70,26 +65,15 @@
7065
ws (.WebSocket (.Result task))
7166
send! (fn [message]
7267
(send-message ws (rt/write message session)))]
73-
(swap! rt/connections assoc (:session-id session) send!)
74-
(when-let [f (get-in session [:options :on-load])]
75-
(f))
68+
(rpc/on-open session send!)
7669
(while (= (.State ws) WebSocketState/Open)
7770
(when-let [message (not-empty (recieve-messgae ws))]
78-
(let [body (rt/read message session)
79-
id (:portal.rpc/id body)
80-
op (get ops (:op body) not-found)]
81-
(binding [rt/*session* session]
82-
(op body (fn [response]
83-
(send!
84-
(assoc response
85-
:portal.rpc/id id
86-
:op :portal.rpc/response)))))))))
71+
(rpc/on-receive session message))))
8772
(catch Exception e
8873
(tap> (Throwable->map e)))
8974
(finally
9075
(close-debug debug)
91-
(rt/reset-session session)
92-
(swap! rt/connections dissoc (:session-id session)))))))
76+
(rpc/on-close session))))))
9377

9478
(defn- send-resource [content-type resource]
9579
{:status 200

src/portal/runtime/jvm/server.clj

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
[portal.runtime.fs :as fs]
1010
[portal.runtime.index :as index]
1111
[portal.runtime.json :as json]
12-
[portal.runtime.jvm.client :as c]
1312
[portal.runtime.npm :as npm]
14-
[portal.runtime.remote.socket :as socket])
13+
[portal.runtime.remote.socket :as socket]
14+
[portal.runtime.rpc :as rpc])
1515
(:import [java.io File PushbackReader]
1616
[java.util UUID]))
1717

@@ -25,9 +25,6 @@
2525

2626
(defmulti route (juxt :request-method :uri))
2727

28-
(defn- not-found [_request done]
29-
(done {:status :not-found}))
30-
3128
(defn- rpc-handler-remote [request]
3229
(let [conn (socket/open (:session request))]
3330
(server/as-channel
@@ -45,8 +42,6 @@
4542
(fn [_ch _status]
4643
(socket/close conn))})))
4744

48-
(def ^:private ops (merge c/ops rt/ops))
49-
5045
(defn- open-debug [{:keys [options] :as session}]
5146
(try
5247
(when (= :server (:debug options))
@@ -61,33 +56,14 @@
6156

6257
(defn- rpc-handler-local [request]
6358
(let [session (rt/open-session (:session request))
64-
send! (fn send! [ch message]
65-
(server/send! ch (rt/write message session)))
6659
debug (open-debug session)]
6760
(server/as-channel
6861
request
69-
{:on-receive
70-
(fn [ch message]
71-
(let [body (rt/read message session)
72-
id (:portal.rpc/id body)
73-
op (get ops (:op body) not-found)]
74-
(binding [rt/*session* session]
75-
(op body (fn [response]
76-
(send!
77-
ch
78-
(assoc response
79-
:portal.rpc/id id
80-
:op :portal.rpc/response)))))))
81-
:on-open
82-
(fn [ch]
83-
(swap! rt/connections assoc (:session-id session) (partial send! ch))
84-
(when-let [f (get-in session [:options :on-load])]
85-
(f)))
86-
:on-close
87-
(fn [_ch _status]
88-
(close-debug debug)
89-
(rt/reset-session session)
90-
(swap! rt/connections dissoc (:session-id session)))})))
62+
{:on-receive (fn [_ch message] (rpc/on-receive session message))
63+
:on-open (fn [ch] (rpc/on-open session #(server/send! ch %)))
64+
:on-close (fn [_ch _status]
65+
(close-debug debug)
66+
(rpc/on-close session))})))
9167

9268
(defmethod route [:get "/rpc"] [request]
9369
(if (get-in request [:session :options :runtime])

src/portal/runtime/node/server.cljs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
[portal.runtime.fs :as fs]
88
[portal.runtime.index :as index]
99
[portal.runtime.json :as json]
10-
[portal.runtime.node.client :as c]
10+
[portal.runtime.rpc :as rpc]
1111
[portal.runtime.transit :as transit]))
1212

1313
(defn- get-header [^js req k]
1414
(-> req .-headers (aget k)))
1515

16-
(defn- not-found [_request done]
17-
(done {:status :not-found}))
18-
1916
(defn- require-string [src file-name]
2017
(let [Module (js/require "module")
2118
^js m (Module. file-name (some-> js/module .-parent))]
@@ -27,8 +24,6 @@
2724

2825
(def Server (.-Server (require-string ws-code "portal/ws.js")))
2926

30-
(def ops (merge c/ops rt/ops))
31-
3227
(defn- get-session-id [^js req]
3328
(some->
3429
(or (second (str/split (.-url req) #"\?"))
@@ -54,27 +49,10 @@
5449
(.-socket req)
5550
(.-headers req)
5651
(fn [^js ws]
57-
(let [session (rt/open-session session)
58-
send!
59-
(fn send! [message]
60-
(.send ws (rt/write message session)))]
61-
(swap! rt/connections assoc (:session-id session) send!)
62-
(when-let [f (get-in session [:options :on-load])]
63-
(f))
64-
(.on ws "message"
65-
(fn [message]
66-
(a/let [req (rt/read message session)
67-
id (:portal.rpc/id req)
68-
op (get ops (get req :op) not-found)
69-
done #(send! (assoc %
70-
:portal.rpc/id id
71-
:op :portal.rpc/response))]
72-
(binding [rt/*session* session]
73-
(op req done)))))
74-
(.on ws "close"
75-
(fn []
76-
(rt/reset-session session)
77-
(swap! rt/connections dissoc (:session-id session)))))))))
52+
(let [session (rt/open-session session)]
53+
(rpc/on-open session #(.send ws %))
54+
(.on ws "message" (fn [message] (rpc/on-receive session message)))
55+
(.on ws "close" (fn [] (rpc/on-close session))))))))
7856

7957
(defn- send-resource [^js res content-type body]
8058
(-> res

src/portal/runtime/rpc.cljc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns ^:no-doc portal.runtime.rpc
2+
(:require #?(:clj [portal.runtime.jvm.client :as c]
3+
:cljs [portal.runtime.node.client :as c]
4+
:cljr [portal.runtime.clr.client :as c])
5+
[portal.runtime :as rt]))
6+
7+
(defn on-open [session send!]
8+
(swap! rt/connections
9+
assoc (:session-id session)
10+
(fn [message]
11+
(send! (rt/write message session))))
12+
(when-let [f (get-in session [:options :on-load])]
13+
(try
14+
(f)
15+
(catch #?(:cljs :default :default Exception) e
16+
(tap> e)))))
17+
18+
(def ^:private ops (merge c/ops rt/ops))
19+
20+
(defn- not-found [_request done]
21+
(done {:status :not-found}))
22+
23+
(defn on-receive [session message]
24+
(let [send! (get @rt/connections (:session-id session))
25+
body (rt/read message session)
26+
id (:portal.rpc/id body)
27+
op (get ops (:op body) not-found)
28+
done (fn on-done [response]
29+
(send!
30+
(assoc response
31+
:portal.rpc/id id
32+
:op :portal.rpc/response)))]
33+
(binding [rt/*session* session]
34+
(op body done))))
35+
36+
(defn on-close [session]
37+
(swap! rt/connections dissoc (:session-id session))
38+
(rt/reset-session session))

0 commit comments

Comments
 (0)