Skip to content

Commit

Permalink
Resolve circular dep and avoid requring-resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Jun 21, 2023
1 parent cc90f90 commit f9fd783
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
9 changes: 5 additions & 4 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[nextjournal.clerk.eval :as eval]
[nextjournal.clerk.parser :as parser]
[nextjournal.clerk.viewer :as v]
[nextjournal.clerk.webserver :as webserver]))
[nextjournal.clerk.webserver :as webserver]
[nextjournal.clerk.window :as window]))

(defonce ^:private !show-filter-fn (atom nil))
(defonce ^:private !last-file (atom nil))
Expand Down Expand Up @@ -78,9 +79,9 @@
#_(show! "https://raw.githubusercontent.com/nextjournal/clerk-demo/main/notebooks/rule_30.clj")
#_(show! (java.io.StringReader. ";; # In Memory Notebook 👋\n(+ 41 1)"))

(defn window! "todo" [& args] (apply (requiring-resolve 'nextjournal.clerk.window/open!) args))
(defn close-window! "todo" [id] ((requiring-resolve 'nextjournal.clerk.window/close!) id))
(defn close-all-windows! "todo" [] ((requiring-resolve 'nextjournal.clerk.window/close-all!)))
(defn window! [& args] (apply window/open! args))
(defn close-window! [id] (window/close! id))
(defn close-all-windows! [] (window/close-all!))

(defn recompute!
"Recomputes the currently visible doc, without parsing it."
Expand Down
34 changes: 16 additions & 18 deletions src/nextjournal/clerk/tap.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
;; # 🚰 Tap Inspector
(ns nextjournal.clerk.tap
{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(:require [clojure.core :as core]
[nextjournal.clerk :as clerk]
[nextjournal.clerk.viewer :as v])
(:require [nextjournal.clerk.viewer :as v])
(:import (java.time Instant LocalTime ZoneId)))

(defn inst->local-time-str [inst] (str (LocalTime/ofInstant inst (ZoneId/systemDefault))))
Expand All @@ -25,18 +23,18 @@
[:button.text-xs.rounded-full.px-3.py-1.border-2.font-sans.hover:bg-slate-100.cursor-pointer
{:on-click #(nextjournal.clerk.render/clerk-eval `(reset-taps!))} "Clear"]]))))

^{::clerk/sync true ::clerk/viewer switch-view ::clerk/visibility {:result :show}}
^{:nextjournal.clerk/sync true :nextjournal.clerk/viewer switch-view :nextjournal.clerk/visibility {:result :show}}
(defonce !view (atom :stream))

(defonce !taps (atom ()))

(defn reset-taps! []
(reset! !taps ())
(clerk/recompute!))
((resolve 'nextjournal.clerk/recompute!)))

(defn tapped [x]
(swap! !taps conj (record-tap x))
(clerk/recompute!))
((resolve 'nextjournal.clerk/recompute!)))

(defonce tap-setup
(add-tap (fn [x] ((resolve `tapped) x))))
Expand All @@ -58,25 +56,25 @@
(update-in [:nextjournal/value ::tapped-at] inst->local-time-str)))})


^{::clerk/visibility {:result :show}
::clerk/viewers (v/add-viewers [tap-viewer])}
(clerk/fragment (cond->> @!taps
(= :latest @!view) (take 1)))
^{:nextjournal.clerk/visibility {:result :show}
:nextjournal.clerk/viewers (v/add-viewers [tap-viewer])}
(v/fragment (cond->> @!taps
(= :latest @!view) (take 1)))

(comment
(last @!taps)
(dotimes [_i 5]
(tap> (rand-int 1000)))
(tap> (shuffle (range (+ 20 (rand-int 200)))))
(tap> (clerk/md "> The purpose of visualization is **insight**, not pictures."))
(tap> (v/md "> The purpose of visualization is **insight**, not pictures."))
(tap> (v/plotly {:data [{:z [[1 2 3] [3 2 1]] :type "surface"}]}))
(tap> (clerk/html {::clerk/width :full} [:h1.w-full.border-2.border-amber-500.bg-amber-500.h-10]))
(tap> (clerk/table {::clerk/width :full} [[1 2] [3 4]]))
(tap> (clerk/plotly {::clerk/width :full} {:data [{:y [3 1 2]}]}))
(tap> (clerk/image "trees.png"))
(tap> (v/html {:nextjournal.clerk/width :full} [:h1.w-full.border-2.border-amber-500.bg-amber-500.h-10]))
(tap> (v/table {:nextjournal.clerk/width :full} [[1 2] [3 4]]))
(tap> (v/plotly {:nextjournal.clerk/width :full} {:data [{:y [3 1 2]}]}))
(tap> (v/image "trees.png"))
(do (require 'rule-30)
(tap> (clerk/with-viewers (clerk/add-viewers rule-30/viewers) rule-30/rule-30)))
(tap> (clerk/with-viewers (clerk/add-viewers rule-30/viewers) rule-30/board))
(tap> (clerk/html [:h1 "Fin. 👋"]))
(tap> (v/with-viewers (v/add-viewers rule-30/viewers) rule-30/rule-30)))
(tap> (v/with-viewers (v/add-viewers rule-30/viewers) rule-30/board))
(tap> (v/html [:h1 "Fin. 👋"]))
(tap> (reduce (fn [acc _] (vector acc)) :fin (range 200)))
(reset-taps!))
11 changes: 5 additions & 6 deletions src/nextjournal/clerk/window.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
(ns nextjournal.clerk.window
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.tap :as tap]
(:require [nextjournal.clerk.tap :as tap]
[nextjournal.clerk.viewer :as v]
[nextjournal.clerk.webserver :as webserver]))

(declare open!)
(defonce !taps-view (atom :stream))
(defn set-view! [x] (reset! !taps-view x) (open! ::clerk/taps))
(defn set-view! [x] (reset! !taps-view x) (open! :nextjournal.clerk/taps))

(def taps-viewer
{:render-fn '(fn [taps {:as opts :keys [taps-view]}]
Expand Down Expand Up @@ -49,18 +48,18 @@
:nextjournal/fetch-opts {:blob-id (str id)}
:nextjournal/blob-id (str id)}))))

(add-watch tap/!taps ::tap-watcher (fn [_ _ _ _] (open! ::clerk/taps)))
(add-watch tap/!taps ::tap-watcher (fn [_ _ _ _] (open! :nextjournal.clerk/taps)))

(defn close! [id] (webserver/close-window! id))

(defn close-all! []
(doseq [w (keys @webserver/!windows)]
(close! w)))

#_(open! ::clerk/taps)
#_(open! :nextjournal.clerk/taps)
#_(doseq [f @@(resolve 'clojure.core/tapset)] (remove-tap f))
#_(tap> (range 30))
#_(close! ::clerk/taps)
#_(close! :nextjournal.clerk/taps)
#_(tap> (v/plotly {:data [{:y [1 2 3]}]}))
#_(tap> (v/table [[1 2] [3 4]]))
#_(open! ::my-window {:title "🔭 Rear Window"} (v/table [[1 2] [3 4]]))
Expand Down

0 comments on commit f9fd783

Please sign in to comment.