Skip to content

Commit

Permalink
[UI] add warning box when ws is disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
thheller committed Apr 27, 2020
1 parent ae0a178 commit c78c5b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/shadow/cljs/ui/main.cljs
Expand Up @@ -14,11 +14,12 @@
))

(defc ui-root* []
[{::m/keys [current-page]}
[{::m/keys [current-page api-ws-connected] :as data}
(sg/query-root
[::m/current-page
;; load marker for suspense, ensures that all basic data is loaded
::m/init-complete?])
::m/init-complete?
::m/api-ws-connected])

nav-items
[{:pages #{:dashboard} :label "Dashboard" :path "/dashboard"}
Expand All @@ -32,6 +33,9 @@
"inline-block px-4 py-2"]

(<< [:div.flex.flex-col.h-full.bg-gray-100
(when-not api-ws-connected
(<< [:div.p-4.bg-red-700.text-white.text-lg.font-bold "UI WebSocket not connected! Reload page to reconnect."]))

[:div.bg-white.shadow-md.z-10
#_[:div.py-2.px-4 [:span.font-bold "shadow-cljs"]]
[:div
Expand Down
20 changes: 20 additions & 0 deletions src/main/shadow/cljs/ui/worker/api_ws.cljs
Expand Up @@ -41,6 +41,23 @@
;; (js/console.log ::api-ws op msg)
(handle-ws env msg)))

(sw/reg-event-fx env/app-ref ::m/api-ws-connect
[]
(fn [{:keys [db] :as env} _]
{:db (assoc db ::m/api-ws-connected true)}))

(sw/reg-event-fx env/app-ref ::m/api-ws-close
[]
(fn [{:keys [db] :as env} _]
{:db (assoc db ::m/api-ws-connected false)}
))

(sw/reg-event-fx env/app-ref ::m/api-ws-error
[]
(fn [{:keys [db] :as env} _]
{:db (assoc db ::m/api-ws-connected false)}))


(defn fx-to-ws
[env build-id]
{:api-ws
Expand Down Expand Up @@ -83,15 +100,18 @@
(.addEventListener socket "open"
(fn [e]
;; (js/console.log "api-open" e socket)
(sw/tx* @env/app-ref [::m/api-ws-connect])
(go (loop []
(when-some [msg (<! api-out)]
(.send socket (transit-str msg))
(recur)))))))

(.addEventListener socket "close"
(fn [e]
(sw/tx* @env/app-ref [::m/api-ws-close])
(js/console.log "api-close" e)))

(.addEventListener socket "error"
(fn [e]
(sw/tx* @env/app-ref [::m/api-ws-error])
(js/console.warn "api-error" e)))))

0 comments on commit c78c5b0

Please sign in to comment.