-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
rpc.cljs
181 lines (161 loc) · 5.31 KB
/
rpc.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(ns portal.ui.rpc
(:refer-clojure :exclude [read type])
(:require [portal.async :as a]
[portal.runtime.cson :as cson]
[portal.ui.rpc.runtime :as rt]
[portal.ui.sci :as sci]
[portal.ui.state :as state]
[portal.ui.viewer.diff :as diff]))
(defn call [f & args]
(apply state/invoke f args))
(when (exists? js/BigInt)
(extend-type js/BigInt
IHash
(-hash [this]
(hash (.toString this)))
IPrintWithWriter
(-pr-writer [this writer _opts]
(-write writer (str this "N")))))
(extend-type default
cson/ToJson
(-to-json [value]
(cson/tag "remote" (pr-str value))))
(defn- read [string]
(cson/read
string
{:transform rt/transform
:default-handler
(fn [value]
(case (first value)
"ref" (rt/ref-> value)
"object" (rt/object-> call value)
(diff/diff-> value)))}))
(defn- write [value]
(cson/write
value
{:transform
(fn [value]
(if-let [id (-> value meta :portal.runtime/id)]
(rt/->runtime call {:id id})
value))}))
(defonce ^:private id (atom 0))
(defonce ^:private pending-requests (atom {}))
(defonce log
(atom
(with-meta
(list)
{:portal.viewer/default :portal.viewer/table})))
(defn- next-id [] (swap! id inc))
(declare send!)
(defn- ws-request [message]
(js/Promise.
(fn [resolve reject]
(let [id (:portal.rpc/id message)]
(swap! pending-requests assoc id [resolve reject])
(send! (assoc message :portal.rpc/id id))))))
(defn- web-request [message]
(js/Promise.
(fn [resolve reject]
(try
(-> (write message)
js/window.opener.portal.web.send_BANG_
(.then read)
resolve)
(catch :default e (reject e))))))
(defn request [message]
(a/let [message (assoc message :portal.rpc/id (next-id))
start (.now js/Date)
response ((if js/window.opener web-request ws-request) message)]
(when (and js/goog.DEBUG
(not= 'portal.runtime/ping (:f message)))
(swap! log
(fn [log]
(with-meta
(take 10
(conj log
(-> message
(dissoc :op :portal.rpc/id)
(assoc :return (:return response)
:time-ms (- (.now js/Date) start)))))
(meta log)))))
response))
(def ^:private ops
{:portal.rpc/response
(fn [message _send!]
(let [id (:portal.rpc/id message)
[resolve] (get @pending-requests id)]
(swap! pending-requests dissoc id)
(when (fn? resolve) (resolve message))))
:portal.rpc/eval-str
(fn [message send!]
(try
(send!
{:op :portal.rpc/response
:result (sci/eval-string (:code message))
:portal.rpc/id (:portal.rpc/id message)})
(catch :default e
(.error js/console e)
(send!
{:op :portal.rpc/response
:error true
:message (.-message e)
:result e
:portal.rpc/id (:portal.rpc/id message)}))))
:portal.rpc/invalidate
(fn [message send!]
(rt/deref (:atom message))
(send! {:op :portal.rpc/response
:portal.rpc/id (:portal.rpc/id message)}))
:portal.rpc/close
(fn [message send!]
(js/setTimeout
(fn []
(state/notify-parent {:type :close})
(js/window.close))
100)
(send! {:op :portal.rpc/response
:portal.rpc/id (:portal.rpc/id message)}))
:portal.rpc/clear
(fn [message send!]
(state/dispatch! state/state state/clear)
(reset! rt/current-values {})
(send! {:op :portal.rpc/response
:portal.rpc/id (:portal.rpc/id message)}))
:portal.rpc/push-state
(fn [message send!]
(state/dispatch! state/state state/history-push {:portal/value (:state message)})
(send!
{:op :portal.rpc/response
:portal.rpc/id (:portal.rpc/id message)}))})
(defn- dispatch [message send!]
;; (tap> (assoc message :type :response))
(when-let [f (get ops (:op message))] (f message send!)))
(defn ^:export ^:no-doc handler [request]
(write (dispatch (read request) identity)))
(defonce ^:private ws-promise (atom nil))
(defn- get-session []
(if (exists? js/PORTAL_SESSION)
js/PORTAL_SESSION
(subs js/window.location.search 1)))
(defn- get-host []
(if (exists? js/PORTAL_HOST) js/PORTAL_HOST js/location.host))
(defn- connect []
(if-let [ws @ws-promise]
ws
(reset!
ws-promise
(js/Promise.
(fn [resolve reject]
(when-let [chan (js/WebSocket.
(str "ws://" (get-host) "/rpc?" (get-session)))]
(set! (.-onmessage chan) #(dispatch (read (.-data %))
(fn [message]
(send! message))))
(set! (.-onerror chan) (fn [e]
(reject e)
(doseq [[_ [_ reject]] @pending-requests]
(reject e))))
(set! (.-onclose chan) #(reset! ws-promise nil))
(set! (.-onopen chan) #(resolve chan))))))))
(defn- send! [message]
(.then (connect) #(.send % (write message))))