File tree 4 files changed +46
-8
lines changed 4 files changed +46
-8
lines changed Original file line number Diff line number Diff line change
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! )
Original file line number Diff line number Diff line change 687
687
(true ? (some #(= % :nextjournal.clerk/remount ) (tree-seq coll? seq doc-or-patch))))
688
688
689
689
(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 ))
690
693
(let [re-eval (fn [{:keys [form]}] (viewer/->render-fn form))]
691
694
(w/postwalk (fn [x] (cond-> x (and (viewer/render-fn? x) (not (:eval x))) re-eval)) doc)))
692
695
693
696
(defn eval-cljs-evals [doc]
694
697
(reset! !render-errors [])
698
+ (intern-atoms! (-> doc :nextjournal/value :atom-var-name->state ))
695
699
(w/postwalk (fn [x]
696
700
(if (viewer/render-eval? x)
697
701
(try (deref (:f x))
Original file line number Diff line number Diff line change 1183
1183
blocks))
1184
1184
1185
1185
(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)))
1191
1190
1192
1191
(defn home? [{:keys [nav-path]}]
1193
1192
(contains? #{" src/nextjournal/home.clj" " 'nextjournal.clerk.home" } nav-path))
Original file line number Diff line number Diff line change 348
348
(is (not-empty (-> (view/doc->viewer (eval/eval-string " (ns nextjournal.clerk.test.sync-vars (:require [nextjournal.clerk :as clerk]))
349
349
^::clerk/sync (def sync-me (atom {:a ['b 'c 3]}))" ))
350
350
:nextjournal/value
351
- :atom-var-name->state
352
- :form
353
- second)))
351
+ :atom-var-name->state )))
354
352
355
353
(is (thrown-with-msg?
356
354
clojure.lang.ExceptionInfo
You can’t perform that action at this time.
0 commit comments