Skip to content

Commit

Permalink
rewrite obj-support some more
Browse files Browse the repository at this point in the history
  • Loading branch information
thheller committed Feb 25, 2023
1 parent ac8b33e commit c863f54
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 387 deletions.
2 changes: 1 addition & 1 deletion src/dev/repl.clj
Expand Up @@ -37,7 +37,7 @@
(defn start []
(server/start!)

;; (cljs/watch :ui {})
(cljs/watch :ui {})

;; until I can figure out a clean API for this
(reset! css-ref
Expand Down
93 changes: 60 additions & 33 deletions src/dev/shadow/insight/ui.cljs
Expand Up @@ -3,6 +3,7 @@
[cljs.pprint :refer (pprint)]
[shadow.grove :as sg :refer (defc <<)]
[shadow.grove.db :as db]
[shadow.grove.eql-query :as eql]
[shadow.insight :as-alias si]
[shadow.insight.runtime :as sir]
[shadow.insight.remote-ext :as insight-ext]
Expand All @@ -15,7 +16,12 @@
(def schema
{::plan
{:type :entity
:primary-key :plan-id}})
:primary-key :plan-id
:joins {:blocks [:many ::block]}}

::block
{:type :entity
:primary-key [:plan-id :idx]}})

(defonce data-ref
(-> {}
Expand All @@ -26,39 +32,50 @@
(-> {}
(sg/prepare data-ref :insight-driver)))

(defc ui-plan [ident]
(bind {::keys [runtime-id]}
(sg/query-root [::runtime-id]))
(defmethod eql/attr :is-local-result? [env db current query-part query-params]
(or (contains? (:result current) :value)
(= (::runtime-id db) (:runtime (:result current)))))

(bind {:keys [blocks exec-ctx] :as data}
(sg/query-ident ident))

(defc ui-block [ident]
(bind {:keys [idx result] :as block}
(sg/query-ident ident [:db/all :is-local-result?]))

(render
(<< [:pre
(with-out-str (pprint (dissoc exec-ctx :results)))]
(case (:type block)
:text
(<< [:div {:dom/inner-html (:html block)}])

:comment
nil

(sg/simple-seq blocks
(fn [block idx]
(case (:type block)
:text
(<< [:div {:dom/inner-html (:html block)}])
:expr
(cond
(not result)
(<< [:div
[:pre {:dom/inner-html (:source block)}]
[:pre "Waiting ..."]])

:comment
nil
(:hidden result)
nil ;; FIXME: render something to at least make it possible to reveal

:expr
(let [{:keys [hidden] :as res} (get-in exec-ctx [:results idx])]
(if hidden
nil ;; FIXME: render something to at least make it possible to reveal
(<< [:div
[:pre {:dom/inner-html (:source block)}]
[:pre (pr-str (get-in exec-ctx [:results idx]))]])))
:else
(<< [:div
[:pre {:dom/inner-html (:source block)}]
[:div (pr-str (:is-local-result? block))]
[:pre (pr-str result)]]))

:directives
(<< [:pre (:source block)])
:directives
(<< [:pre (:source block)])

(<< [:div (pr-str block)]))
)))))
(<< [:div (pr-str block)]))))

(defc ui-plan [ident]
(bind {:keys [blocks] :as data}
(sg/query-ident ident))

(render
(sg/simple-seq blocks ui-block)))

(defc ui-root []
(bind {::keys [active-plan]}
Expand Down Expand Up @@ -103,20 +120,31 @@
:file "foo"}
{::si/plan!
(fn [{:keys [plan] :as msg}]
(let [ident (db/make-ident ::plan (:plan-id plan))]

(let [self-id (srs/get-client-id runtime)
plan-id (:plan-id plan)
plan-ident (db/make-ident ::plan plan-id)]

(sg/run-tx! rt-ref
(fn [env]
(-> env
(assoc-in [:db ::runtime-id] (srs/get-client-id runtime))
(assoc-in [:db ident] (assoc plan :db/ident ident))
(assoc-in [:db ::active-plan] ident))))
(assoc-in [:db ::runtime-id] self-id)
(update :db db/add ::plan (update plan :blocks (fn [blocks] (mapv #(assoc %1 :plan-id plan-id) blocks)))
(fn [db ident]
(assoc db ::active-plan ident))))))

(insight-ext-cljs/add-listener insight-ext ::ui
(fn [exec-ctx]
(fn [{:keys [results] :as exec-ctx}]
(sg/run-tx! rt-ref
(fn [env]
(assoc-in env [:db ident :exec-ctx] exec-ctx)))))
(update env :db
(fn [db]
(reduce-kv
(fn [db idx result]
(let [block-ident (db/make-ident ::block [plan-id idx])]
(assoc-in db [block-ident :result] result)))
db
results)))))))

(insight-ext-cljs/plan-execute! insight-ext plan))
)}))
Expand All @@ -133,7 +161,6 @@
(fn [{:keys [runtime] :as svc}]
(p/del-extension runtime ::ui)))


(sg/reg-fx rt-ref :ws-send
(fn [{::keys [runtime] :as env} message]
(srs/relay-msg runtime message)))
Expand Down
11 changes: 4 additions & 7 deletions src/main/shadow/cljs/devtools/server/repl_impl.clj
Expand Up @@ -188,9 +188,8 @@
(let [{:keys [from ref-oid eval-ns]} msg]

(>!! to-relay
{:op :obj-request
{:op :obj-as-edn
:to from
:request-op :edn
:oid ref-oid})

(-> repl-state
Expand All @@ -199,7 +198,7 @@
:eval-result msg)
(recur)))

:obj-request-failed
:obj-as-edn-failed
(let [{:keys [from ex-oid]} msg]
(if (:print-failed repl-state)
(do (repl-stderr repl-state "The result failed to print and printing the exception also failed. No clue whats going on.")
Expand All @@ -210,9 +209,8 @@
(recur)))

(do (>!! to-relay
{:op :obj-request
{:op :obj-as-str
:to from
:request-op :str
:oid ex-oid})

(-> repl-state
Expand Down Expand Up @@ -269,9 +267,8 @@
:eval-runtime-error
(let [{:keys [from ex-oid]} msg]
(>!! to-relay
{:op :obj-request
{:op :obj-as-ex-str
:to from
:request-op :ex-str
:oid ex-oid})
(recur (assoc repl-state :stage :error)))

Expand Down
12 changes: 6 additions & 6 deletions src/main/shadow/cljs/ui/components/inspect.cljs
Expand Up @@ -276,11 +276,11 @@
[:div {:class
(css :px-2 :py-2 :shadow-lg :bg-white
["& > *:not(:last-child)" :mb-2])}
(when (contains? supports :fragment)
(when (contains? supports :obj-fragment)
(view-as-button display-type :browse "BROWSER" active?))
(when (contains? supports :pprint)
(when (contains? supports :obj-pprint)
(view-as-button display-type :pprint "PPRINT" active?))
(when (contains? supports :edn)
(when (contains? supports :obj-edn)
(view-as-button display-type :edn "EDN" active?))
(when (= :string data-type)
(view-as-button display-type :str "STR" active?))]]]
Expand Down Expand Up @@ -493,11 +493,11 @@

[:div {:class (css :flex :bg-white :py-2 :px-4 :font-mono :border-t-2)}
[:div "View as: "]
(when (contains? supports :fragment)
(when (contains? supports :obj-fragment)
(view-as-button display-type :browse "Browse" active?))
(when (contains? supports :pprint)
(when (contains? supports :obj-pprint)
(view-as-button display-type :pprint "Pretty-Print" active?))
(when (contains? supports :edn)
(when (contains? supports :obj-edn)
(view-as-button display-type :edn "EDN" active?))]]
))))

Expand Down
29 changes: 11 additions & 18 deletions src/main/shadow/cljs/ui/db/inspect.cljs
Expand Up @@ -77,7 +77,7 @@
(if (contains? supports pref)
pref
(cond
(contains? supports :fragment)
(contains? supports :obj-fragment)
:browse

(contains? #{:string :number :boolean} data-type)
Expand Down Expand Up @@ -173,10 +173,9 @@
;; leaving this as a hack for now until I can think of something cleaner
:hack
(do (relay-ws/call! env
{:op :obj-request
{:op :obj-edn-limit
:to runtime-id
:oid oid
:request-op :edn-limit
:limit 150}

{:e ::obj-preview-result})
Expand Down Expand Up @@ -226,10 +225,9 @@

:hack
(do (relay-ws/call! env
{:op :obj-request
{:op :obj-edn
:to runtime-id
:oid oid
:request-op :edn}
:oid oid}

{:e ::obj-as-result
:ident (:db/ident current)
Expand All @@ -246,10 +244,9 @@

:hack
(do (relay-ws/call! env
{:op :obj-request
{:op :obj-str
:to runtime-id
:oid oid
:request-op :str}
:oid oid}
{:e ::obj-as-result
:ident (:db/ident current)
:key :str})
Expand All @@ -265,10 +262,9 @@

:hack
(do (relay-ws/call! env
{:op :obj-request
{:op :obj-pprint
:to runtime-id
:oid oid
:request-op :pprint}
:oid oid}
{:e ::obj-as-result
:ident (:db/ident current)
:key :pprint})
Expand Down Expand Up @@ -310,12 +306,11 @@
;; FIXME: should be smarter about which elements to fetch
;; might already have some
(do (relay-ws/call! env
{:op :obj-request
{:op :obj-fragment
:to runtime-id
:oid oid
:start start-idx
:num num
:request-op :fragment
:key-limit 160
:val-limit 160}
{:e ::fragment-slice-loaded
Expand Down Expand Up @@ -387,12 +382,11 @@
:slice (persistent! slice)}

(do (relay-ws/call! env
{:op :obj-request
{:op :obj-lazy-chunk
:to runtime-id
:oid oid
:start start-idx
:num num
:request-op :chunk
:val-limit 100}

{:e ::lazy-seq-slice-loaded
Expand Down Expand Up @@ -448,10 +442,9 @@

;; FIXME: fx this
(relay-ws/call! env
{:op :obj-request
{:op :obj-nav
:to runtime-id
:oid oid
:request-op :nav
:idx idx
:summary true}

Expand Down
3 changes: 1 addition & 2 deletions src/main/shadow/remote/relay/local.clj
Expand Up @@ -361,9 +361,8 @@
:input {:code "(+ 1 2)"
:ns 'user}})

(>!! tool-in {:op :obj-request
(>!! tool-in {:op :obj-edn
:to 1
:request-op :edn
:oid "dbf3d3a5-aeed-4ba9-a1f7-b8f17eb32e12"})

(require '[shadow.remote.runtime.clj.local :as clj])
Expand Down

0 comments on commit c863f54

Please sign in to comment.