Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
update for IC compat
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurC committed Dec 5, 2023
1 parent c7ff3c4 commit 01fbd49
Show file tree
Hide file tree
Showing 19 changed files with 357 additions and 334 deletions.
16 changes: 8 additions & 8 deletions src-dev/dev.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
; What if we hardcode this dev entrypoint for the fiddles we're working on today?
; thus can share models, for example

(e/defn MergedFiddleMain []
(e/defn MergedFiddleMain [ring-req]
(e/server
(let [[conn db] (model/init-datomic)]
(bindx [datomic-browser.domain/conn (check conn)
(bindx [e/http-request ring-req
datomic-browser.domain/conn (check conn)
datomic-browser.domain/db (check db)
datomic-browser.domain/schema (check (new (dx/schema> datomic-browser.domain/db)))]
(e/client
Expand Down Expand Up @@ -81,17 +82,16 @@
(@shadow-watch :dev)
; todo block until finished?
(comment (@shadow-stop!))
(def server (start-server! config))
(def server (start-server! (fn [ring-req] (e/boot-server {} MergedFiddleMain ring-req)) config))
(comment (.stop server))
(rcf/enable!))))

#?(:cljs
(do
(def electric-entrypoint
(e/boot
; in dev, we setup a merged fiddle config,
; fiddles must all opt in to the shared routing strategy
(MergedFiddleMain.)))
; in dev, we setup a merged fiddle config,
; fiddles must all opt in to the shared routing strategy
(e/boot-client {} MergedFiddleMain nil))

(defonce reactor nil)

Expand All @@ -118,4 +118,4 @@
(prof/serve-files 8082)
;; Navigate to http://localhost:8082
(prof/start {:framebuf 10000000})
(prof/stop))
(prof/stop))
313 changes: 161 additions & 152 deletions src/datomic_browser/datomic_browser.cljc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/dustingetz/scratch.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@


(e/defn Scratch []
(dom/pre (dom/text "yo")))
(e/client (dom/pre (dom/text "yo"))))
21 changes: 11 additions & 10 deletions src/dustingetz/y2023/bug_unmount.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
(e/on-unmount #(println "unmounting Foo" v visible)))

(e/defn Bug-unmount []
(let [!visible (atom [1])
visible (e/watch !visible)]
(ui/button (e/fn [] (swap! !visible (fn [v] (conj v (inc (last v))))))
(dom/text "append"))
(ui/button (e/fn [] (swap! !visible (fn [v] (into [] (butlast v)))))
(dom/text "remove"))
(e/for-by identity [v visible]
(dom/div (dom/text v))
(e/on-unmount #(println "unmounting branch")) ; no closure
(Foo. v visible))))
(e/client
(let [!visible (atom [1])
visible (e/watch !visible)]
(ui/button (e/fn [] (swap! !visible (fn [v] (conj v (inc (last v))))))
(dom/text "append"))
(ui/button (e/fn [] (swap! !visible (fn [v] (into [] (butlast v)))))
(dom/text "remove"))
(e/for-by identity [v visible]
(dom/div (dom/text v))
(e/on-unmount #(println "unmounting branch")) ; no closure
(Foo. v visible)))))
42 changes: 22 additions & 20 deletions src/dustingetz/y_dir.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@
(new (F. F) x))))))

(e/defn Dir-tree [Recur]
(e/fn [[h s]]
(cond
(file-is-dir h)
(e/client
(dom/li (dom/text (e/server (file-get-name h)))
(dom/ul
(e/server
(e/for [x (file-list-files h)]
(Recur. [x s])))))) ; recur
(e/server
(e/fn [[h s]]
(cond
(file-is-dir h)
(e/client
(dom/li (dom/text (e/server (file-get-name h)))
(dom/ul
(e/server
(e/for [x (file-list-files h)]
(Recur. [x s])))))) ; recur

(file-is-file h)
(when (includes-str? (file-get-name h) s)
(let [name_ (e/server (file-get-name h))]
(e/client (dom/li (dom/text name_))))))))
(file-is-file h)
(when (includes-str? (file-get-name h) s)
(let [name_ (e/server (file-get-name h))]
(e/client (dom/li (dom/text name_)))))))))

(e/defn Y-dir []
(dom/div
(let [!s (atom "") s (e/watch !s)]
(ui/input s (e/fn [v] (reset! !s v)))
(dom/ul
(e/server
(let [h (clojure.java.io/file (file-absolute-path "./src"))]
(new (Y. Dir-tree) [h s])))))))
(e/client
(dom/div
(let [!s (atom "") s (e/watch !s)]
(ui/input s (e/fn [v] (reset! !s v)))
(dom/ul
(e/server
(let [h (clojure.java.io/file (file-absolute-path "./src"))]
(new (Y. Dir-tree) [h s]))))))))
2 changes: 1 addition & 1 deletion src/dustingetz/y_fib.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(new (F. F) x))))))

(e/defn Trace [x]
(dom/div (dom/text x))
(e/client (dom/div (dom/text x)))
x)

(e/defn Fib [Recur]
Expand Down
11 changes: 6 additions & 5 deletions src/electric_fiddle/essay.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

(e/defn Essay [& [?essay]]
#_(e/client (dom/div #_(dom/props {:class ""}))) ; fix css grid next
(let [essay-filename (get essays ?essay)]
(cond
(nil? ?essay) (binding [hf/pages essays] (Index.))
(nil? essay-filename) (dom/h1 (dom/text "Essay not found: " history/route))
() (Custom-markdown. extensions essay-filename))))
(e/client
(let [essay-filename (get essays ?essay)]
(cond
(nil? ?essay) (binding [hf/pages essays] (Index.))
(nil? essay-filename) (dom/h1 (dom/text "Essay not found: " history/route))
() (Custom-markdown. extensions essay-filename)))))
41 changes: 21 additions & 20 deletions src/electric_fiddle/fiddle.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,27 @@

(e/defn Fiddle-impl [target ?wrap src]
#_(dom/pre (dom/text target " " ?wrap " " src))
(dom/div (dom/props {:class "user-examples"})
(dom/fieldset
(dom/props {:class "user-examples-code"})
(dom/legend (dom/text "Code"))
(CodeMirror. {:parent dom/node :readonly true} identity identity src))
(dom/fieldset
(dom/props {:class ["user-examples-target" (some-> target name)]})
(dom/legend (dom/text "Result"))
#_(dom/pre (dom/text (pr-str history/route)))
#_(binding [history/build-route (fn [[page :as page-route] local-route]
(println 'page-route page-route 'local-route local-route)
`[~@(case page `Index nil page-route)
~@local-route])])
(history/router nil
(let [Target (get hf/pages target)
Wrap (when ?wrap (get hf/pages ?wrap ::not-found))]
(cond
(= ::not-found Wrap) (dom/h1 (dom/text "not found, wrap: " ?wrap))
(some? Wrap) (Wrap. Target)
() (Target.)))))))
(e/client
(dom/div (dom/props {:class "user-examples"})
(dom/fieldset
(dom/props {:class "user-examples-code"})
(dom/legend (dom/text "Code"))
(CodeMirror. {:parent dom/node :readonly true} identity identity src))
(dom/fieldset
(dom/props {:class ["user-examples-target" (some-> target name)]})
(dom/legend (dom/text "Result"))
#_(dom/pre (dom/text (pr-str history/route)))
#_(binding [history/build-route (fn [[page :as page-route] local-route]
(println 'page-route page-route 'local-route local-route)
`[~@(case page `Index nil page-route)
~@local-route])])
(history/router nil
(let [Target (get hf/pages target)
Wrap (when ?wrap (get hf/pages ?wrap ::not-found))]
(cond
(= ::not-found Wrap) (dom/h1 (dom/text "not found, wrap: " ?wrap))
(some? Wrap) (Wrap. Target)
() (Target.))))))))

(e/defn Fiddle-fn [& [alt-text target-s ?wrap :as args]]
(let [target (symbol target-s)]
Expand Down
2 changes: 1 addition & 1 deletion src/electric_fiddle/fiddle_markdown.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
:= ['foo "example" "b" "c"])

(e/defn ExtensionNotFound [s & directive]
(dom/div (dom/text "Unsupported markdown directive: " (pr-str directive))))
(e/client (dom/div (dom/text "Unsupported markdown directive: " (pr-str directive)))))

(e/defn Custom-markdown [extensions essay-filename]
(e/server
Expand Down
43 changes: 22 additions & 21 deletions src/electric_fiddle/main.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@
(e/defn NotFoundPage [& args] (e/client (dom/h1 (dom/text "Page not found: " (pr-str history/route)))))

(e/defn Main []
(binding [history/encode route->path
history/decode #(or (path->route %) [`Index])]
(history/router (history/HTML5-History.)
(set! (.-title js/document) (str (some-> (identity history/route) first name (str " – ")) "Electric Fiddle"))
(binding [dom/node js/document.body]
(let [[page & args] history/route]
(dom/pre (dom/text (pr-str history/route)))
#_(binding [history/build-route (fn [top-route paths']
(vec (concat top-route #_(butlast top-route) paths')))]) ; page local fiddle links
(binding [history/build-route (fn [[page :as page-route] [x & xs :as local-route]]
#_(println 'page-route page-route 'local-route local-route)
; todo - root relative path? need another directive '/
; allow sideways nav inside same "dir" (app)
(cond
(= '.. x) `[~@(butlast page-route) ~@(rest local-route)] ; sideways
(= page `Index) local-route
() `[~@page-route ~@local-route]))] ; descend
(history/router 1 ; weird, paired with Index ~@
(case page
`Index (Index.)
(e/apply (get hf/pages page NotFoundPage) args)))))))))
(e/client
(binding [history/encode route->path
history/decode #(or (path->route %) [`Index])]
(history/router (history/HTML5-History.)
(set! (.-title js/document) (str (some-> (identity history/route) first name (str " – ")) "Electric Fiddle"))
(binding [dom/node js/document.body]
(let [[page & args] history/route]
(dom/pre (dom/text (pr-str history/route)))
#_(binding [history/build-route (fn [top-route paths']
(vec (concat top-route #_(butlast top-route) paths')))]) ; page local fiddle links
(binding [history/build-route (fn [[page :as page-route] [x & xs :as local-route]]
#_(println 'page-route page-route 'local-route local-route)
; todo - root relative path? need another directive '/
; allow sideways nav inside same "dir" (app)
(cond
(= '.. x) `[~@(butlast page-route) ~@(rest local-route)] ; sideways
(= page `Index) local-route
() `[~@page-route ~@local-route]))] ; descend
(history/router 1 ; weird, paired with Index ~@
(case page
`Index (Index.)
(e/apply (get hf/pages page NotFoundPage) args))))))))))
21 changes: 11 additions & 10 deletions src/electric_fiddle/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ Otherwise, the client connection is rejected gracefully."
(adapter/reject-websocket-handler 1008 "stale client")))) ; https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1
(next-handler ring-req))))

(defn wrap-electric-websocket [next-handler]
(defn wrap-electric-websocket [next-handler entrypoint]
(fn [ring-request]
(if (ring/ws-upgrade-request? ring-request)
(let [authenticated-request (auth/basic-authentication-request ring-request authenticate) ; optional
electric-message-handler (partial adapter/electric-ws-message-handler authenticated-request)] ; takes the ring request as first arg - makes it available to electric program
electric-message-handler (partial adapter/electric-ws-message-handler authenticated-request entrypoint)] ; takes the ring request as first arg - makes it available to electric program
(ring/ws-upgrade-response (adapter/electric-ws-adapter electric-message-handler)))
(next-handler ring-request))))

(defn electric-websocket-middleware [next-handler config]
(-> (wrap-electric-websocket next-handler) ; 4. connect electric client
(defn electric-websocket-middleware [next-handler config entrypoint]
(-> (wrap-electric-websocket next-handler entrypoint) ; 4. connect electric client
(cookies/wrap-cookies) ; 3. makes cookies available to Electric app
(wrap-reject-stale-client config) ; 2. reject stale electric client
(wrap-params) ; 1. parse query params
Expand All @@ -106,14 +106,14 @@ Otherwise, the client connection is rejected gracefully."
(-> (res/not-found "Not found")
(res/content-type "text/plain")))

(defn http-middleware [config]
(defn http-middleware [config entrypoint]
;; these compose as functions, so are applied bottom up
(-> not-found-handler
(wrap-index-page config) ; 5. otherwise fallback to default page file
(wrap-resource (:resources-path config)) ; 4. serve static file from classpath
(wrap-content-type) ; 3. detect content (e.g. for index.html)
(wrap-demo-router) ; 2. route
(electric-websocket-middleware config) ; 1. intercept electric websocket
(electric-websocket-middleware config entrypoint) ; 1. intercept electric websocket
))

(defn- add-gzip-handler
Expand All @@ -125,12 +125,13 @@ Otherwise, the client connection is rejected gracefully."
(.setMinGzipSize 1024)
(.setHandler (.getHandler server)))))

(defn start-server! [{:keys [port host]
(defn start-server! [entrypoint
{:keys [port host]
:or {port 8080, host "0.0.0.0"} ; insecure default?
:as config}]
(log/info (pr-str config))
(try
(let [server (ring/run-jetty (http-middleware config)
(let [server (ring/run-jetty (http-middleware config entrypoint)
(merge {:port port
:join? false
:configurator add-gzip-handler}
Expand All @@ -142,5 +143,5 @@ Otherwise, the client connection is rejected gracefully."
(catch IOException err
(if (instance? BindException (ex-cause err)) ; port is already taken, retry with another one
(do (log/warn "Port" port "was not available, retrying with" (inc port))
(start-server! (update config :port inc)))
(throw err)))))
(start-server! entrypoint (update config :port inc)))
(throw err)))))
13 changes: 7 additions & 6 deletions src/electric_starter_app/todos.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

(e/defn InputSubmit [F]
; Custom input control using lower dom interface for Enter handling
(dom/input (dom/props {:placeholder "Buy milk"})
(dom/on "keydown" (e/fn [e]
(when (= "Enter" (.-key e))
(when-some [v (contrib.str/empty->nil (-> e .-target .-value))]
(new F v)
(set! (.-value dom/node) "")))))))
(e/client
(dom/input (dom/props {:placeholder "Buy milk"})
(dom/on "keydown" (e/fn [e]
(when (= "Enter" (.-key e))
(when-some [v (contrib.str/empty->nil (-> e .-target .-value))]
(new F v)
(set! (.-value dom/node) ""))))))))

(e/defn TodoCreate []
(e/client
Expand Down
51 changes: 26 additions & 25 deletions src/electric_tutorial/demo_chat_extended.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,39 @@
(e/def present (e/server (e/watch !present)))

(e/defn Chat-UI [username]
(dom/div (dom/text "Present: "))
(dom/ul
(e/server
(e/for-by first [[session-id username] present]
(e/client
(dom/li (dom/text username (str " (session-id: " session-id ")")))))))
(e/client
(dom/div (dom/text "Present: "))
(dom/ul
(e/server
(e/for-by first [[session-id username] present]
(e/client
(dom/li (dom/text username (str " (session-id: " session-id ")")))))))

(dom/hr)
(dom/ul
(e/server
(e/for [{:keys [::username ::msg]} msgs]
(e/client
(dom/li (dom/strong (dom/text username))
(dom/text " " msg))))))
(dom/hr)
(dom/ul
(e/server
(e/for [{:keys [::username ::msg]} msgs]
(e/client
(dom/li (dom/strong (dom/text username))
(dom/text " " msg))))))

(dom/input
(dom/props {:placeholder "Type a message" :maxlength 100})
(dom/on "keydown" (e/fn [e]
(when (= "Enter" (.-key e))
(when-some [v (empty->nil (.substr (.. e -target -value) 0 100))]
(dom/style {:background-color "yellow"}) ; loading
(e/server
(swap! !msgs #(cons {::username username ::msg v}
(take 9 %))))
(set! (.-value dom/node) "")))))))
(dom/input
(dom/props {:placeholder "Type a message" :maxlength 100})
(dom/on "keydown" (e/fn [e]
(when (= "Enter" (.-key e))
(when-some [v (empty->nil (.substr (.. e -target -value) 0 100))]
(dom/style {:background-color "yellow"}) ; loading
(e/server
(swap! !msgs #(cons {::username username ::msg v}
(take 9 %))))
(set! (.-value dom/node) ""))))))))

(e/defn ChatExtended []
(e/client
(let [session-id
(e/server (get-in e/*http-request* [:headers "sec-websocket-key"]))
(e/server (get-in e/http-request [:headers "sec-websocket-key"]))
username
(e/server (get-in e/*http-request* [:cookies "username" :value]))]
(e/server (get-in e/http-request [:cookies "username" :value]))]
(if-not (some? username)
(dom/div
(dom/text "Set login cookie here: ")
Expand Down
Loading

0 comments on commit 01fbd49

Please sign in to comment.