Skip to content

Commit

Permalink
Improve render performance by only evaluating each render-fn once
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed May 22, 2023
1 parent 4b959df commit 2b33638
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,15 @@
(let [re-eval (fn [{:keys [form]}] (viewer/->viewer-fn form))]
(w/postwalk (fn [x] (cond-> x (viewer/viewer-fn? x) re-eval)) doc)))

(defn replace-viewer-fns [doc]
(w/postwalk-replace (:hash->viewer doc) (dissoc doc :hash->viewer)))

(defn ^:export set-state! [{:as state :keys [doc]}]
(when (contains? state :doc)
(when (exists? js/window)
;; TODO: can we restore the scroll position when navigating back?
(.scrollTo js/window #js {:top 0}))
(reset! !doc doc))
(reset! !doc (replace-viewer-fns doc)))
;; (when (and error (contains? @!doc :status))
;; (swap! !doc dissoc :status))
(when (remount? doc)
Expand Down
18 changes: 14 additions & 4 deletions src/nextjournal/clerk/view.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
(ns nextjournal.clerk.view
(:require [nextjournal.clerk.viewer :as v]
[hiccup.page :as hiccup]
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as str]
[clojure.java.io :as io])
[clojure.walk :as walk]
[hiccup.page :as hiccup]
[nextjournal.clerk.viewer :as v])
(:import (java.net URI)))

(defn ^:private extract-hash->viewer [presentation]
(into {}
(map (juxt :hash identity))
(keep :nextjournal/viewer (tree-seq (some-fn map? vector?) #(cond-> % (map? %) vals) presentation))))

(defn doc->viewer
([doc] (doc->viewer {} doc))
([opts {:as doc :keys [ns file]}]
(binding [*ns* ns]
(-> (merge doc opts) v/notebook v/present))))
(let [presentation (-> (merge doc opts) v/notebook v/present)
hash->viewer (extract-hash->viewer presentation)]
(assoc (walk/postwalk-replace (set/map-invert hash->viewer) presentation)
:hash->viewer hash->viewer)))))

#_(doc->viewer (nextjournal.clerk/eval-file "notebooks/hello.clj"))
#_(nextjournal.clerk/show! "notebooks/test.clj")
Expand Down

0 comments on commit 2b33638

Please sign in to comment.