Skip to content

Commit 74b0099

Browse files
committedAug 11, 2023
Improve runtime re-connection mechanism
1 parent d3b24e2 commit 74b0099

File tree

4 files changed

+94
-66
lines changed

4 files changed

+94
-66
lines changed
 

‎src/portal/ui/app.cljs

+65-49
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
[portal.ui.icons :as icons]
1010
[portal.ui.inspector :as ins]
1111
[portal.ui.options :as opts]
12+
[portal.ui.rpc :as rpc]
1213
[portal.ui.select :as select]
1314
[portal.ui.state :as state]
1415
[portal.ui.styled :as s]
@@ -87,10 +88,9 @@
8788
[s/div {:style {:grid-row "1"}} "]"]]
8889
[s/div {:style {:flex "1"}}]]))
8990

90-
(defn- runtime-info []
91+
(defn- use-runtime-info []
9192
(let [opts (opts/use-options)
9293
theme (theme/use-theme)
93-
connected? (status/use-status)
9494
header (if (= :dev (:mode opts))
9595
(::c/diff-add theme)
9696
(::c/background2 theme))]
@@ -106,70 +106,86 @@
106106
(str/join
107107
" - "
108108
[(:window-title opts name) platform version]))))
109-
#js [opts])
110-
(when-not connected?
111-
[s/div
112-
{:style {:color (::c/exception theme)}}
113-
"ERROR: Disconnected from runtime!"])))
109+
#js [opts])))
114110

115111
(defn inspect-footer []
116112
(let [theme (theme/use-theme)
117113
state (state/use-state)
114+
connected? (status/use-status)
118115
selected-context (state/get-all-selected-context @state)
119116
viewer (ins/get-viewer state (first selected-context))
120117
compatible-viewers (ins/get-compatible-viewers @ins/viewers selected-context)]
118+
(use-runtime-info)
121119
[s/div
122120
{:style
123121
{:display :flex
124122
:padding-top (:padding theme)
125123
:padding-bottom (:padding theme)
126124
:padding-right (* 1.5 (:padding theme))
127125
:padding-left (* 1.5 (:padding theme))
128-
:align-items :stretch
126+
:align-items :center
129127
:justify-content :space-between
130-
:background (::c/background2 theme)
128+
:background (if-not connected?
129+
(::c/exception theme)
130+
(::c/background2 theme))
131131
:box-sizing :border-box
132132
:border-top [1 :solid (::c/border theme)]}}
133-
[s/select
134-
{:title "Select a different viewer."
135-
:value (pr-str (:name viewer))
136-
:on-change
137-
(fn [e]
138-
(ins/set-viewer!
139-
state
140-
selected-context
141-
(keyword (.substr (.. e -target -value) 1))))
142-
:style
143-
{:background (::c/background theme)
144-
:padding (:padding theme)
145-
:box-sizing :border-box
146-
:font-family (:font-family theme)
147-
:font-size (:font-size theme)
148-
:color (::c/text theme)
149-
:border-radius (:border-radius theme)
150-
:border [1 :solid (::c/border theme)]}}
151-
(for [{:keys [name]} compatible-viewers]
152-
^{:key name}
153-
[s/option {:value (pr-str name)} (pr-str name)])]
154-
[runtime-info]
155-
[s/button
156-
{:title "Open command palette."
157-
:on-click #(commands/open-command-palette state)
158-
:style
159-
{:font-family (:font-family theme)
160-
:background (::c/background theme)
161-
:border-radius (:border-radius theme)
162-
:border [1 :solid (::c/border theme)]
163-
:box-sizing :border-box
164-
:padding-top (:padding theme)
165-
:padding-bottom (:padding theme)
166-
:padding-left (* 2.5 (:padding theme))
167-
:padding-right (* 2.5 (:padding theme))
168-
:color (::c/tag theme)
169-
:font-size (:font-size theme)
170-
:font-weight :bold
171-
:cursor :pointer}}
172-
[icons/terminal]]]))
133+
[s/div
134+
{:style {:flex "1"}}
135+
[s/select
136+
{:title "Select a different viewer."
137+
:value (pr-str (:name viewer))
138+
:on-change
139+
(fn [e]
140+
(ins/set-viewer!
141+
state
142+
selected-context
143+
(keyword (.substr (.. e -target -value) 1))))
144+
:style
145+
{:background (::c/background theme)
146+
:padding (:padding theme)
147+
:box-sizing :border-box
148+
:font-family (:font-family theme)
149+
:font-size (:font-size theme)
150+
:color (::c/text theme)
151+
:border-radius (:border-radius theme)
152+
:border [1 :solid (::c/border theme)]}}
153+
(for [{:keys [name]} compatible-viewers]
154+
^{:key name}
155+
[s/option {:value (pr-str name)} (pr-str name)])]]
156+
157+
(when-not connected?
158+
[s/div
159+
{:style {:text-align :center :flex "1" :color (::c/text theme)}}
160+
[s/div
161+
#_{:style {:display :flex :gap (:padding theme)}}
162+
[s/span {:style {:font-weight :bold}} "Error: "]
163+
"Connection lost"]
164+
[s/div
165+
"(portal.api/start "
166+
(pr-str (select-keys (rpc/resolve-connection) [:port]))
167+
")"]])
168+
169+
[s/div
170+
{:style {:flex "1" :text-align :right}}
171+
[s/button
172+
{:title "Open command palette."
173+
:on-click #(commands/open-command-palette state)
174+
:style
175+
{:font-family (:font-family theme)
176+
:background (::c/background theme)
177+
:border-radius (:border-radius theme)
178+
:border [1 :solid (::c/border theme)]
179+
:box-sizing :border-box
180+
:padding-top (:padding theme)
181+
:padding-bottom (:padding theme)
182+
:padding-left (* 3 (:padding theme))
183+
:padding-right (* 3 (:padding theme))
184+
:color (::c/tag theme)
185+
:font-size (:font-size theme)
186+
:font-weight :bold
187+
:cursor :pointer}}
188+
[icons/terminal]]]]))
173189

174190
(defn- search-input []
175191
(let [ref (react/useRef nil)

‎src/portal/ui/connection_status.cljs

+9-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
(let [[{:keys [errors connected?]} set-status!]
1717
(react/useState {:connected? true :errors 0})]
1818
(use-interval
19-
(if (> errors 5)
20-
(.error js/console "Unable to establish connection to runtime.")
21-
(fn []
22-
(-> (state/invoke 'portal.runtime/ping)
23-
(.then #(set-status! {:connected? true :errors 0}))
24-
(.catch #(set-status! {:connected? false :errors (inc errors)})))))
19+
(fn []
20+
(-> (state/invoke 'portal.runtime/ping)
21+
(.then (fn [_]
22+
;; reconnecting to runtime
23+
(when-not (zero? errors)
24+
(state/reset-value-cache!)
25+
(set-status! {:connected? true :errors 0}))))
26+
(.catch (fn [_]
27+
(set-status! {:connected? false :errors (inc errors)})))))
2528
5000)
2629
connected?))
2730

‎src/portal/ui/rpc.cljs

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns portal.ui.rpc
22
(:refer-clojure :exclude [read type])
3-
(:require [portal.async :as a]
3+
(:require [clojure.string :as str]
4+
[portal.async :as a]
45
[portal.runtime.cson :as cson]
56
[portal.ui.cljs :as cljs]
67
[portal.ui.rpc.runtime :as rt]
@@ -189,23 +190,27 @@
189190

190191
(defn- get-host []
191192
(if (exists? js/PORTAL_HOST)
192-
js/PORTAL_HOST
193+
(first (str/split js/PORTAL_HOST #":"))
193194
(.-hostname js/location)))
194195

195196
(defn- get-proto []
196197
(if (= (.-protocol js/location) "https:") "wss:" "ws:"))
197198

198199
(defn- get-port []
199-
(when-not (= (.-port js/location) "")
200-
(.-port js/location)))
200+
(if (exists? js/PORTAL_HOST)
201+
(js/parseInt (second (str/split js/PORTAL_HOST #":")))
202+
(when-not (= (.-port js/location) "")
203+
(js/parseInt (.-port js/location)))))
204+
205+
(defn resolve-connection []
206+
{:host (get-host)
207+
:port (get-port)
208+
:protocol (get-proto)
209+
:session (get-session)})
201210

202211
(defn connect
203212
([]
204-
(connect
205-
{:host (get-host)
206-
:port (get-port)
207-
:protocol (get-proto)
208-
:session (get-session)}))
213+
(connect (resolve-connection)))
209214
([{:keys [host port protocol session]}]
210215
(if-let [ws @ws-promise]
211216
ws
@@ -220,7 +225,6 @@
220225
(send! message))))
221226
(set! (.-onerror chan) (fn [e]
222227
(reject e)
223-
(.error js/console (pr-str e))
224228
(doseq [[_ [_ reject]] @pending-requests]
225229
(reject e))))
226230
(set! (.-onclose chan) #(reset! ws-promise nil))

‎src/portal/ui/state.cljs

+6-1
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,15 @@
279279

280280
(defonce value-cache (r/atom {}))
281281

282+
(defn reset-value-cache!
283+
"Useful when establishing or re-establishing a connection with the runtime."
284+
[]
285+
(reset! value-cache (with-meta {} {:key (.now js/Date)})))
286+
282287
(defn clear [state]
283288
(a/do
284289
(invoke 'portal.runtime/clear-values)
285-
(reset! value-cache (with-meta {} {:key (.now js/Date)}))
290+
(reset-value-cache!)
286291
(-> state
287292
(dissoc :portal/value
288293
:search-text

0 commit comments

Comments
 (0)
Please sign in to comment.