Skip to content

Commit

Permalink
Move !sync-state to viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Oct 2, 2024
1 parent b74665e commit 922f2e7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
11 changes: 7 additions & 4 deletions notebooks/global_sync.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; # ⚡️ Global Sync
(ns global-sync
(:require [nextjournal.clerk :as clerk]))
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.viewer :as viewer]))

^::clerk/no-cache
(shuffle (range 100))
Expand All @@ -9,10 +10,12 @@
'(fn [_ opts]
[:div
[:button
{:on-click #(swap! nextjournal.clerk.render/!sync-state assoc :foo (rand-int 1000))}
{:on-click #(swap! nextjournal.clerk.viewer/!sync-state assoc :foo (rand-int 1000))}
"clickme"]
(pr-str @nextjournal.clerk.render/!sync-state)
[nextjournal.clerk.render/inspect @nextjournal.clerk.viewer/!sync-state]
#_(nextjournal.clerk.render/inspect @nextjournal.clerk.render/!sync-state)])}
{})

#_(swap! nextjournal.clerk.webserver/!sync-state assoc :foo (rand-int 1000))
@viewer/!sync-state

#_(swap! nextjournal.clerk.viewer/!sync-state assoc :foo (rand-int 1000))
6 changes: 2 additions & 4 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@
(r/atom {:eval-counter 0
:doc nil
:viewers viewer/!viewers
:panels {}
:sync {}}))
:panels {}}))

(defonce !eval-counter (r/cursor !state [:eval-counter]))
(defonce !doc (r/cursor !state [:doc]))
(defonce !sync-state (r/cursor !state [:sync]))
(defonce !viewers (r/cursor !state [:viewers]))
(defonce !panels (r/cursor !state [:panels]))

Expand Down Expand Up @@ -806,7 +804,7 @@

(defn ^:export init [{:as state :keys [render-router path->doc]}]
(setup-router! state)
(add-watch !sync-state 'nextjournal.clerk.webserver/!sync-state atom-changed)
(add-watch viewer/!sync-state `viewer/!sync-state atom-changed)
(when (contains? #{:bundle :serve} render-router)
(set-state! (case render-router
:bundle {:doc (get path->doc (or (path-from-url-hash (->URL (.-href js/location))) ""))}
Expand Down
7 changes: 5 additions & 2 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1265,11 +1265,14 @@
{:nextjournal/presented (present error)
:nextjournal/blob-id (str (gensym "error"))})

(defonce !sync-state
(#?(:clj atom :cljs ratom/atom) {}))

(defn sync-state []
(->viewer-eval
(list 'reset!
'nextjournal.clerk.render/!sync-state
@@(resolve 'nextjournal.clerk.webserver/!sync-state))))
`!sync-state
@!sync-state)))

(defn process-blocks [viewers {:as doc :keys [ns]}]
(-> doc
Expand Down
4 changes: 2 additions & 2 deletions src/nextjournal/clerk/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
(eval '(nextjournal.clerk/recompute!)))

(defonce !clients (atom #{}))
(defonce !sync-state (doto (atom {})
(add-watch `!sync-state sync-atom-changed)))
(defonce watch-sync
(add-watch v/!sync-state `v/!sync-state sync-atom-changed))
(defonce !doc (atom nil))
(defonce !last-sender-ch (atom nil))

Expand Down

0 comments on commit 922f2e7

Please sign in to comment.