Skip to content

Commit 7886bdc

Browse files
committed
include image caching
1 parent b8cf49e commit 7886bdc

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/nextjournal/clerk.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@
596596
* a symbol representing the var name (qualified or not)
597597
* the form of an anonymous expression"
598598
([]
599-
(swap! webserver/!doc dissoc :blob->result)
599+
(swap! webserver/!doc dissoc :blob->result ::webserver/presentation-cache)
600600
(if (fs/exists? config/cache-dir)
601601
(do (fs/delete-tree config/cache-dir)
602602
(prn :cache-dir/deleted config/cache-dir))

src/nextjournal/clerk/viewer.cljc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@
552552
{:as to-present :nextjournal/keys [auto-expand-results?]} (merge (dissoc (->opts wrapped-value) :!budget :nextjournal/budget)
553553
(dissoc cell :result ::doc) ;; TODO: reintroduce doc once we know why it OOMs the static build on CI (some walk issue probably)
554554
opts-from-block
555-
(ensure-wrapped-with-viewers (or viewers (get-viewers (get-*ns*))) value))
555+
(ensure-wrapped-with-viewers (or viewers (get-viewers (get-*ns*))) value)
556+
(when blob-id {:nextjournal/blob-id blob-id}))
556557
presented-result (-> (present to-present)
557558
(update :nextjournal/render-opts
558559
(fn [{:as opts existing-id :id}]
@@ -939,14 +940,14 @@
939940

940941
(defn ->opts [wrapped-value]
941942
(select-keys wrapped-value [:nextjournal/budget :nextjournal/css-class :nextjournal/width :nextjournal/render-opts
942-
:nextjournal/render-evaluator
943-
:!budget :store!-wrapped-value :store!-viewer
943+
:nextjournal/render-evaluator :nextjournal/blob-id
944+
:!budget :store!-wrapped-value :store!-viewer :presentation-cache
944945
:present-elision-fn :path :offset]))
945946

946947
(defn inherit-opts [{:as wrapped-value :nextjournal/keys [viewers]} value path-segment]
947948
(-> (ensure-wrapped-with-viewers viewers value)
948-
(merge (select-keys (->opts wrapped-value) [:!budget :store!-wrapped-value :store!-viewer
949-
:present-elision-fn :nextjournal/budget :path]))
949+
(merge (select-keys (->opts wrapped-value) [:!budget :store!-wrapped-value :store!-viewer :presentation-cache
950+
:present-elision-fn :nextjournal/budget :path :nextjournal/blob-id]))
950951
(update :path (fnil conj []) path-segment)))
951952

952953
(defn present-ex-data [parent throwable-map]
@@ -996,11 +997,21 @@
996997
(def image-viewer
997998
{#?@(:bb []
998999
:clj [:pred #(instance? BufferedImage %)
999-
:transform-fn (fn [{image :nextjournal/value}]
1000-
(-> {:nextjournal/value (buffered-image->bytes image)
1001-
:nextjournal/content-type "image/png"
1002-
:nextjournal/width (image-width image)}
1003-
mark-presented))])
1000+
:transform-fn (fn [{image :nextjournal/value
1001+
blob-id :nextjournal/blob-id
1002+
:keys [presentation-cache]
1003+
:as wrapped-value}]
1004+
(let [cache-path [blob-id (:path wrapped-value)]
1005+
bytes (or (when presentation-cache
1006+
(get-in @presentation-cache cache-path))
1007+
(let [b (buffered-image->bytes image)]
1008+
(when (and presentation-cache blob-id)
1009+
(swap! presentation-cache assoc-in cache-path b))
1010+
b))]
1011+
(-> {:nextjournal/value bytes
1012+
:nextjournal/content-type "image/png"
1013+
:nextjournal/width (image-width image)}
1014+
mark-presented)))])
10041015
:name `image-viewer
10051016
:render-fn '(fn [blob-or-url] [:div.flex.flex-col.items-center.not-prose
10061017
[:img {:src #?(:clj (nextjournal.clerk.render/url-for blob-or-url)

src/nextjournal/clerk/webserver.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,17 @@
146146
(future-cancel scheduled-send-status-future)))
147147

148148
(defn present+reset! [doc]
149-
(let [presented (view/doc->viewer doc)
149+
(let [!presentation-cache (or (::presentation-cache @!doc) (atom {}))
150+
presented (view/doc->viewer {:presentation-cache !presentation-cache} doc)
150151
sync-vars-old (v/extract-sync-atom-vars @!doc)
151152
sync-vars (v/extract-sync-atom-vars doc)]
153+
(swap! !presentation-cache select-keys (keys (:blob->result doc)))
152154
(doseq [sync-var (set/difference sync-vars sync-vars-old)]
153155
(add-watch @sync-var (symbol sync-var) sync-atom-changed))
154156
(doseq [sync-var (set/difference sync-vars-old sync-vars)]
155157
(remove-watch @sync-var (symbol sync-var)))
156158
(maybe-cancel-send-status-future @!doc)
157-
(reset! !doc (with-meta doc presented))
159+
(reset! !doc (with-meta (assoc doc ::presentation-cache !presentation-cache) presented))
158160
presented))
159161

160162
(defn update-doc! [{:as doc :keys [nav-path fragment skip-history?]}]
@@ -327,7 +329,7 @@
327329
nil)))
328330
{:status 200
329331
:headers {"Content-Type" "text/html" "Cache-Control" "no-store"}
330-
:body (view/->html {:doc (view/doc->viewer @!doc)
332+
:body (view/->html {:doc (view/doc->viewer {:presentation-cache (::presentation-cache @!doc)} @!doc)
331333
:resource->url @config/!resource->url
332334
:render-router :serve
333335
:conn-ws? true})})

0 commit comments

Comments
 (0)