-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
rpc.cljs
230 lines (204 loc) · 6.45 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(ns portal.ui.rpc
(:refer-clojure :exclude [read type])
(:require [portal.async :as a]
[portal.runtime.cson :as cson]
[portal.ui.cljs :as cljs]
[portal.ui.rpc.runtime :as rt]
[portal.ui.state :as state]
[portal.ui.viewer.diff :as diff])
(:import [goog.math Long]))
(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")))))
(when (exists? js/Uint8Array)
(extend-type js/Uint8Array
IPrintWithWriter
(-pr-writer [this writer _opts]
(-write writer "#portal/bin \"")
(-write writer (cson/base64-encode this))
(-write writer "\""))))
(extend-type array
IHash
(-hash [this]
(hash (into [] this)))
IEquiv
(-equiv [^js this ^js o]
(let [n (.-length this)]
(and (array? o)
(= n (.-length o))
(loop [i 0]
(cond
(== i n) true
(not= (aget this i)
(aget this o)) false
:else (recur (inc i))))))))
(extend-type number
IEquiv
(-equiv [a b]
(or (== a b)
(and (.isNaN js/Number a)
(.isNaN js/Number b)))))
(extend-type Long
IPrintWithWriter
(-pr-writer [this writer _opts]
(-write writer (str this))))
(defmethod cson/tagged-str "remote" [{:keys [rep]}] rep)
(when-not js/goog.DEBUG
(extend-type default
cson/ToJson
(-to-json [value buffer]
(cson/-to-json
(with-meta
(cson/tagged-value "remote" (pr-str value))
(meta value))
buffer))))
(defn- read [string]
(cson/read
string
{:transform rt/transform
:default-handler
(fn [op value]
(or (case op
"ref" (rt/->value value)
"object" (rt/->object call value)
(diff/->diff op value))
(cson/tagged-value op value)))}))
(defn- write [value]
(cson/write
value
{:transform
(fn [value]
(if-let [id (-> value meta :portal.runtime/id)]
(cson/tagged-value "ref" id)
value))}))
(defonce ^:private id (atom 0))
(defonce ^:private pending-requests (atom {}))
(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]
((if js/window.opener web-request ws-request)
(assoc message :portal.rpc/id (next-id))))
(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!]
(let [return
(fn [msg]
(send!
(assoc msg
:op :portal.rpc/response
:portal.rpc/id (:portal.rpc/id message))))
error
(fn [e]
(return {:error e :message (.-message e)}))]
(try
(let [{:keys [value] :as response} (cljs/eval-string message)]
(if-not (:await message)
(return response)
(-> (.resolve js/Promise value)
(.then #(return (assoc response :value %)))
(.catch error))))
(catch :default e
(.error js/console e)
(error e)))))
: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!]
(a/do
(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
(.-hostname js/location)))
(defn- get-proto []
(if (= (.-protocol js/location) "https:") "wss:" "ws:"))
(defn- get-port []
(when-not (= (.-port js/location) "")
(.-port js/location)))
(defn connect
([]
(connect
{:host (get-host)
:port (get-port)
:protocol (get-proto)
:session (get-session)}))
([{:keys [host port protocol session]}]
(if-let [ws @ws-promise]
ws
(reset!
ws-promise
(js/Promise.
(fn [resolve reject]
(when-let [chan (js/WebSocket.
(str protocol "//" host (when port (str ":" port)) "/rpc?" session))]
(set! (.-onmessage chan) #(dispatch (read (.-data %))
(fn [message]
(send! message))))
(set! (.-onerror chan) (fn [e]
(reject e)
(.error js/console (pr-str 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))))