Skip to content

Commit

Permalink
use only first connected runtime for REPL when multiple are connected
Browse files Browse the repository at this point in the history
otherwise it errors out since it doesn't know which to eval in. not
a perfect behavior either but since no client knows how to pick a
runtime its a better default.
  • Loading branch information
thheller committed Jan 19, 2019
1 parent 0b7b228 commit 7e03bc1
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions src/main/shadow/cljs/devtools/server/worker/impl.clj
Expand Up @@ -313,18 +313,60 @@
(update :repl-sources repl-sources-as-client-resources build-state))})

;; (>!!output worker-state {:type :repl/runtime-connect :runtime-id runtime-id :runtime-info runtime-info})
(update worker-state :runtimes assoc runtime-id
{:runtime-id runtime-id
:runtime-out runtime-out
:runtime-info runtime-info
:init-sent true}))
(-> worker-state
(cond->
(zero? (count (:runtimes worker-state)))
(assoc :default-runtime-id runtime-id))
(update :runtimes assoc runtime-id
{:runtime-id runtime-id
:runtime-out runtime-out
:runtime-info runtime-info
:connected-since (System/currentTimeMillis)
:init-sent true})))

(defn maybe-pick-different-default-runtime [{:keys [runtimes default-runtime-id] :as worker-state} runtime-id]
(cond
(not= default-runtime-id runtime-id)
worker-state

(empty? runtimes)
(dissoc worker-state :default-runtime-id)

:else
(assoc worker-state :default-runtime-id
(->> (vals runtimes)
(sort-by :connected-since)
(first)
:runtime-id))))

(comment
;; when a runtime disconnects and it was the default
;; pick a new one if there are any
(maybe-pick-different-default-runtime
{:default-runtime-id 1
:runtimes {2 {:runtime-id 2 :connected-since 2}
3 {:runtime-id 3 :connected-since 3}}}
1)

;; if there aren't any remove the default
(maybe-pick-different-default-runtime
{:default-runtime-id 1
:runtimes {}}
1)

;; if it wasn't the default do nothing
(maybe-pick-different-default-runtime
{:default-runtime-id 2
:runtimes {}}
1))

(defmethod do-proc-control :runtime-disconnect
[worker-state {:keys [runtime-id]}]
(log/debug ::runtime-disconnect {:runtime-id runtime-id})
;; (>!!output worker-state {:type :repl/runtime-disconnect :runtime-id runtime-id})
(-> worker-state
(update :runtimes dissoc runtime-id)
(maybe-pick-different-default-runtime runtime-id)
;; clean all sessions for that runtime
(update :repl-sessions (fn [sessions]
(reduce-kv
Expand Down Expand Up @@ -655,10 +697,13 @@
worker-state)))

(defmethod do-proc-control :repl-eval
[{:keys [build-state runtimes] :as worker-state}
[{:keys [build-state runtimes default-runtime-id] :as worker-state}
{:keys [result-chan input session-id runtime-id] :as msg}]
(log/debug ::repl-eval {:session-id session-id :runtime-id runtime-id})
(let [runtime-count (count runtimes)]
(let [runtime-count (count runtimes)

runtime-id
(or runtime-id default-runtime-id)]

(cond
(nil? build-state)
Expand Down

0 comments on commit 7e03bc1

Please sign in to comment.