Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch sync atoms and recompute! when they're changed #354

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/nextjournal/clerk/tap.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
#_(inst->local-time-str (Instant/now))

(def tap-viewer
{:name :tapped-value
:render-fn '(fn [{:keys [val tapped-at key]} opts]
(with-meta
[:div.border-t.relative.py-3
[:span.absolute.rounded-full.px-2.bg-gray-300.font-mono.top-0
{:class "left-1/2 -translate-x-1/2 -translate-y-1/2 py-[1px] text-[9px]"} (:nextjournal/value tapped-at)]
[:div.overflow-x-auto [v/inspect-presented val]]]
{:key (:nextjournal/value key)}))
:transform-fn (comp clerk/mark-preserve-keys
(clerk/update-val #(update % :tapped-at inst->local-time-str)))})
{:name :tapped-value
:render-fn '(fn [{:keys [val tapped-at key]} opts]
(with-meta
[:div.border-t.relative.py-3
[:span.absolute.rounded-full.px-2.bg-gray-300.font-mono.top-0
{:class "left-1/2 -translate-x-1/2 -translate-y-1/2 py-[1px] text-[9px]"} (:nextjournal/value tapped-at)]
[:div.overflow-x-auto [v/inspect-presented val]]]
{:key (:nextjournal/value key)}))
:transform-fn (comp clerk/mark-preserve-keys
(clerk/update-val #(update % :tapped-at inst->local-time-str)))})

(clerk/add-viewers! [tap-viewer])

Expand Down Expand Up @@ -95,4 +95,5 @@

(tap> (clerk/html [:h1 "Fin. 👋"]))

(reset-taps!)
)
24 changes: 15 additions & 9 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -915,19 +915,25 @@
{:var var :value @var}
ex))))))

(defn extract-clerk-atom-vars [{:as _doc :keys [blocks]}]
(into {}
(comp (keep (fn [{:keys [result form]}]
(when-let [var (-> result :nextjournal/value (get-safe :nextjournal.clerk/var-from-def))]
(when (contains? (meta form) :nextjournal.clerk/sync)
#?(:clj (throw-if-sync-var-is-invalid var))
var))))
(map (juxt #(list 'quote (symbol %)) #(->> % deref deref (list 'quote)))))
(defn extract-sync-atom-vars [{:as _doc :keys [blocks]}]
(into #{}
(keep (fn [{:keys [result form]}]
(when-let [var (-> result :nextjournal/value (get-safe :nextjournal.clerk/var-from-def))]
(when (contains? (meta form) :nextjournal.clerk/sync)
#?(:clj (throw-if-sync-var-is-invalid var))
var))))
blocks))

(defn atom-var-name->state [doc]
(->viewer-eval
(list 'nextjournal.clerk.render/intern-atoms!
(into {}
(map (juxt #(list 'quote (symbol %)) #(->> % deref deref (list 'quote))))
(extract-sync-atom-vars doc)))))

(defn process-blocks [viewers {:as doc :keys [ns]}]
(-> doc
(assoc :atom-var-name->state (->viewer-eval (list 'nextjournal.clerk.render/intern-atoms! (extract-clerk-atom-vars doc))))
(assoc :atom-var-name->state (atom-var-name->state doc))
(update :blocks (partial into [] (comp (mapcat (partial with-block-viewer doc))
(map (comp process-wrapped-value
apply-viewers*
Expand Down
15 changes: 12 additions & 3 deletions src/nextjournal/clerk/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [babashka.fs :as fs]
[clojure.edn :as edn]
[clojure.pprint :as pprint]
[clojure.set :as set]
[clojure.string :as str]
[editscript.core :as editscript]
[nextjournal.clerk.view :as view]
Expand Down Expand Up @@ -136,9 +137,8 @@
(eval '(nextjournal.clerk/recompute!)))
:swap! (when-let [var (resolve (:var-name msg))]
(try
(apply swap! @var (eval (:args msg)))
(binding [*sender-ch* sender-ch]
(eval '(nextjournal.clerk/recompute!)))
(apply swap! @var (eval (:args msg))))
(catch Exception ex
(throw (doto (ex-info (str "Clerk cannot `swap!` synced var `" (:var-name msg) "`.") msg ex) show-error!)))))))))})

Expand All @@ -163,8 +163,17 @@

#_(nextjournal.clerk/serve! {})

(defn sync-atom-changed [key atom old-state new-state]
(eval '(nextjournal.clerk/recompute!)))

(defn present+reset! [doc]
(let [presented (view/doc->viewer doc)]
(let [presented (view/doc->viewer doc)
sync-vars-old (v/extract-sync-atom-vars @!doc)
sync-vars (v/extract-sync-atom-vars doc)]
(doseq [sync-var (set/difference sync-vars sync-vars-old)]
(add-watch @sync-var (symbol sync-var) sync-atom-changed))
(doseq [sync-var (set/difference sync-vars-old sync-vars)]
(remove-watch @sync-var (symbol sync-var)))
(reset! !doc (with-meta doc presented))
presented))

Expand Down