Skip to content

Commit 748f73e

Browse files
authored
Move sync state out of viewer eval to speed up recomputation (#722)
1 parent bf89e07 commit 748f73e

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

notebooks/large_sync.clj

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns large-atom-sync
2+
(:require [nextjournal.clerk :as clerk]))
3+
4+
;; # Clerk synced atoms and large data
5+
6+
;; **1.** Define a Clerk synced atom called `!state`.
7+
8+
(def el
9+
{:hi :this
10+
:is :a
11+
:large :sync})
12+
13+
^::clerk/sync
14+
(defonce !state
15+
(atom (vec (repeat 20000 el))))
16+
17+
;; **2.** Verify that `!state` is present on both sides.
18+
(clerk/eval-cljs '!state)
19+
20+
;; **3.** Make a button to cram a lot of data into `!state` on the SCI side.
21+
22+
^{::clerk/visibility {:code :hide :result :hide}}
23+
(def large-sync-button-viewer
24+
{:render-fn '(fn [_]
25+
[:button.bg-sky-500.text-white.rounded-xl.px-2.py-1
26+
27+
{:on-click (fn [_]
28+
(swap! large-atom-sync/!state conj {:rand-int (rand-int 42000)}))}
29+
"Click for large sync!"])})
30+
31+
^{::clerk/viewer large-sync-button-viewer}
32+
{}
33+
34+
(peek @!state)
35+
36+
;; **4.** Click the button to see what happens 🙂
37+
#_(clerk/recompute!)

src/nextjournal/clerk/render.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,15 @@
687687
(true? (some #(= % :nextjournal.clerk/remount) (tree-seq coll? seq doc-or-patch))))
688688

689689
(defn re-eval-render-fns [doc]
690+
;; TODO: `intern-atoms!` is currently called twice in case of a
691+
;; remount patch-state! event
692+
(intern-atoms! (-> doc :nextjournal/value :atom-var-name->state))
690693
(let [re-eval (fn [{:keys [form]}] (viewer/->render-fn form))]
691694
(w/postwalk (fn [x] (cond-> x (and (viewer/render-fn? x) (not (:eval x))) re-eval)) doc)))
692695

693696
(defn eval-cljs-evals [doc]
694697
(reset! !render-errors [])
698+
(intern-atoms! (-> doc :nextjournal/value :atom-var-name->state))
695699
(w/postwalk (fn [x]
696700
(if (viewer/render-eval? x)
697701
(try (deref (:f x))

src/nextjournal/clerk/viewer.cljc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,11 +1183,10 @@
11831183
blocks))
11841184

11851185
(defn atom-var-name->state [doc]
1186-
(->render-eval
1187-
(list 'nextjournal.clerk.render/intern-atoms!
1188-
(into {}
1189-
(map (juxt #(list 'quote (symbol %)) #(->> % deref deref (list 'quote))))
1190-
(extract-sync-atom-vars doc)))))
1186+
(into {}
1187+
(map (fn [sync-var]
1188+
[(symbol sync-var) @@sync-var]))
1189+
(extract-sync-atom-vars doc)))
11911190

11921191
(defn home? [{:keys [nav-path]}]
11931192
(contains? #{"src/nextjournal/home.clj" "'nextjournal.clerk.home"} nav-path))

test/nextjournal/clerk/viewer_test.clj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@
348348
(is (not-empty (-> (view/doc->viewer (eval/eval-string "(ns nextjournal.clerk.test.sync-vars (:require [nextjournal.clerk :as clerk]))
349349
^::clerk/sync (def sync-me (atom {:a ['b 'c 3]}))"))
350350
:nextjournal/value
351-
:atom-var-name->state
352-
:form
353-
second)))
351+
:atom-var-name->state)))
354352

355353
(is (thrown-with-msg?
356354
clojure.lang.ExceptionInfo

0 commit comments

Comments
 (0)