Skip to content

Commit

Permalink
Make lazy loading work for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Jul 11, 2023
1 parent bb1ccf1 commit ac6379c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
20 changes: 12 additions & 8 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,20 @@
(defn read-string [s]
(js/nextjournal.clerk.sci_env.read-string s))

(declare !doc)

(defn fetch! [{:keys [blob-id]} opts]
#_(js/console.log :fetch! blob-id opts)
(-> (js/fetch (str "/_blob/" blob-id (when (seq opts)
(str "?" (opts->query opts)))))
(.then #(.text %))
(.then #(try (read-string %)
(catch js/Error e
(js/console.error #js {:message "sci read error" :blob-id blob-id :code-string % :error e})
(render-unreadable-edn %))))))
(let [session (:session (:nextjournal/value @!doc))
opts+session (cond-> opts
session (assoc :session (pr-str session)))]
#_(js/console.log :fetch! blob-id opts session)
(-> (js/fetch (str "/_blob/" blob-id (when (seq opts+session)
(str "?" (opts->query opts+session)))))
(.then #(.text %))
(.then #(try (read-string %)
(catch js/Error e
(js/console.error #js {:message "sci read error" :blob-id blob-id :code-string % :error e})
(render-unreadable-edn %)))))))

(defn ->expanded-at [auto-expand? presented]
(cond-> presented
Expand Down
5 changes: 3 additions & 2 deletions src/nextjournal/clerk/session.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(ns nextjournal.clerk.session)
(ns nextjournal.clerk.session
(:require [nextjournal.clerk.analyzer :as analyzer]))

(defn in-session-ns [{:keys [ns session-ns]} var]
(if (and var session-ns)
Expand All @@ -10,7 +11,7 @@
"nextjournal.clerk.synthetic-session.")

(defn session-ns-name [{:keys [ns session]}]
(symbol (str session-ns-prefix (ns-name ns) ".session=" (nextjournal.clerk.analyzer/valuehash session))))
(symbol (str session-ns-prefix (ns-name ns) ".session=" (analyzer/valuehash session))))

#_(session-ns-name {:ns (create-ns 'scratch) :session 'foo})

Expand Down
1 change: 1 addition & 0 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@
:file
:open-graph
:ns
:session
:title
:toc
:toc-visibility
Expand Down
12 changes: 6 additions & 6 deletions src/nextjournal/clerk/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@
{:status 404}))

(defn extract-blob-opts [{:as _req :keys [uri query-string]}]
{:blob-id (str/replace uri "/_blob/" "")
:fetch-opts (get-fetch-opts query-string)})
(let [{:as fetch-opts :keys [session]} (get-fetch-opts query-string)]
(cond-> {:blob-id (str/replace uri "/_blob/" "")
:fetch-opts (dissoc fetch-opts :session)}
session (assoc :session (edn/read-string session)))))

(defn serve-file [uri path]
(let [file (when (fs/exists? path)
Expand Down Expand Up @@ -143,7 +145,6 @@
presented (view/doc->viewer doc)
sync-vars-old (v/extract-sync-atom-vars @!doc)
sync-vars (v/extract-sync-atom-vars doc)]
(prn :present+reset! *session*)
(doseq [sync-var (if recreate-all-watches?
sync-vars-old
(set/difference sync-vars-old sync-vars))]
Expand Down Expand Up @@ -192,7 +193,6 @@
(swap! !clients disj ch))
:on-receive (fn [sender-ch edn-string]
(binding [*session* (get-session (@!ch->req sender-ch))]
(prn :on-receive *session*)
(create-session-doc!)
(let [!doc (get-doc! *session*)
{:as msg :keys [type recompute?]} (read-msg edn-string)]
Expand Down Expand Up @@ -262,7 +262,6 @@
((resolve 'nextjournal.clerk/show!) (assoc opts :session *session*) file-or-ns))

(defn navigate! [{:as opts :keys [nav-path]}]
(prn :navigate! *session* opts)
(show! opts (->file-or-ns (maybe-add-extension nav-path))))

(defn prefetch-request? [req] (= "prefetch" (-> req :headers (get "purpose"))))
Expand Down Expand Up @@ -301,7 +300,8 @@
(httpkit/as-channel req ws-handlers))
(try
(case (get (re-matches #"/([^/]*).*" uri) 1)
"_blob" (serve-blob @(get-doc!) (extract-blob-opts req))
"_blob" (let [{:as blob-opts :keys [session]} (extract-blob-opts req)]
(serve-blob @(get-doc! session) blob-opts))
("build" "js" "css") (serve-file uri (str "public" uri))
("_fs") (serve-file uri (str/replace uri "/_fs/" ""))
"_ws" {:status 200 :body "upgrading..."}
Expand Down

0 comments on commit ac6379c

Please sign in to comment.