Skip to content

Commit

Permalink
electric: standardize naming convention for tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
dustingetz committed Sep 23, 2024
1 parent 5c502f4 commit fb72a0b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
15 changes: 8 additions & 7 deletions src/hyperfiddle/electric3.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,13 @@ A mount point can be :

(cc/defn on-unmount* [f] (m/observe (cc/fn [!] (! nil) f)))

(defmacro on-unmount "Run clojure(script) thunk `f` during unmount.
(defmacro on-unmount [f] `(input (on-unmount* ~f))) ; experimental

Standard electric code runs on mount, therefore there is no `on-mount`."
[f] `(input (on-unmount* ~f))) ; experimental

(hyperfiddle.electric3/defn OnUnmount [f] (input (on-unmount* f)))
(hyperfiddle.electric3/defn On-unmount ; legacy
"Run clojure/script thunk `f` during unmount. Standard electric code runs on
mount, therefore there is no `on-mount`. (Todo: we intend to rework this API in
v3 to expose the full differential diff lifecycle)"
[f] (input (on-unmount* f)))

(defmacro boot-server [opts Main & args]
(let [env (merge (lang/normalize-env &env) web-config opts)
Expand Down Expand Up @@ -464,8 +465,8 @@ inhibiting all further reactive updates."

(def system-time-ms (m/signal (m/sample -get-system-time-ms <clock)))

(hyperfiddle.electric3/defn SystemTimeMs [] (input system-time-ms))
(hyperfiddle.electric3/defn SystemTimeSecs [] (math/floor-div (input system-time-ms) 1000))
(hyperfiddle.electric3/defn System-time-ms [] (input system-time-ms))
(hyperfiddle.electric3/defn System-time-secs [] (math/floor-div (input system-time-ms) 1000))

(cc/defn uf->is [uf]
(m/ap (m/amb (i/empty-diff 0)
Expand Down
24 changes: 12 additions & 12 deletions src/hyperfiddle/electric_dom3.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@
an existing DOM Element, typically libraries integration."
[element Body] (e/client (binding [node element] ($ Body))))

(e/defn WithElement
(e/defn With-element
"Mount a new DOM Element of type `tag` in the current `node` and run `Body` in
the context of the new Element."
([tag Body] ($ WithElement nil tag Body))
([tag Body] ($ With-element nil tag Body))
([ns tag Body]
(let [mp (e/client (e/mount-point))
elem (e/client (create-element ns tag mp))]
Expand Down Expand Up @@ -313,7 +313,7 @@
;; Note `(do a b c)` expands to (e/amb (e/drain a) (e/drain b) c), e/drain returns ∅.
(defn element*
([tag forms] (element* nil tag forms))
([ns tag forms] `($ WithElement ~ns ~tag (e/fn [] (do ~@forms)))))
([ns tag forms] `($ With-element ~ns ~tag (e/fn [] (do ~@forms)))))

(defmacro element
"Mount a new DOM Element of type `tag` in the current `node` and run `body` in
Expand Down Expand Up @@ -463,35 +463,35 @@ input's value, use `EventListener`."
running))
(m/amb)))))))

(e/defn OnAll
([event-type] ($ OnAll event-type identity))
([event-type f] ($ OnAll event-type f {}))
([event-type f opts] ($ OnAll event-type f opts ##Inf))
([event-type f opts concurrency-factor] ($ OnAll node event-type f opts concurrency-factor))
(e/defn On-all
([event-type] ($ On-all event-type identity))
([event-type f] ($ On-all event-type f {}))
([event-type f opts] ($ On-all event-type f opts ##Inf))
([event-type f opts concurrency-factor] ($ On-all node event-type f opts concurrency-factor))
([node event-type f opts concurrency-factor]
(e/client (e/join (e/input (fork concurrency-factor (listen-some node event-type ((e/capture-fn) f) opts)))))))

;;;;;;;;;;;;
;; Extras ;;
;;;;;;;;;;;;

#?(:cljs (defn updown [node enter-event leave-event init]
#?(:cljs (defn squarewave [node enter-event leave-event init]
(->> (mx/mix
(m/observe (fn [!] (with-listener node enter-event (fn [_] (! true)))))
(m/observe (fn [!] (with-listener node leave-event (fn [_] (! false))))))
(m/reductions {} init) (m/relieve {}))))

(e/defn Mouse-over?
([] (Mouse-over? node))
([node] (e/client (e/input (updown node "mouseenter" "mouseleave" false)))))
([node] (e/client (e/input (squarewave node "mouseenter" "mouseleave" false)))))

(e/defn Focused?
([] (Focused? node))
([node] (e/client (e/input (updown node "focus" "blur" (= node (.-activeElement js/document)))))))
([node] (e/client (e/input (squarewave node "focus" "blur" (= node (.-activeElement js/document)))))))

(e/defn Mouse-down?
([] (Mouse-down? node))
([node] (e/client (e/input (updown node "mouseup" "mousedown" false)))))
([node] (e/client (e/input (squarewave node "mouseup" "mousedown" false)))))

;;;;;;;;;;;
;; Sugar ;;
Expand Down
6 changes: 3 additions & 3 deletions src/hyperfiddle/input_zoo0.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ buffers (dirty), commit, discard bundled as enter/esc"
(= "Escape" k) (do (set! (.-value dom/node) "") nil)
() nil)))]
#_(PendingMonitor) ; the optimistic list item is responsible for pending/retry affordances
(dom/OnAll "keydown" submit!)))))
(dom/On-all "keydown" submit!)))))

;; Graveyard

Expand All @@ -229,15 +229,15 @@ buffers (dirty), commit, discard bundled as enter/esc"
(= "Enter" k) (read! (.-target e)) ; no clear
(= "Escape" k) (do (set! (.-value dom/node) "") nil)
() nil)))]
(dom/OnAll "keydown" submit!)))))) ; eagerly submit individual edits
(dom/On-all "keydown" submit!)))))) ; eagerly submit individual edits

(e/defn CheckboxSubmit!-nocancel-noretry
[checked & {:keys [id label] :as props
:or {id (random-uuid)}}]
(e/client
(e/amb
(dom/input (dom/props {:type "checkbox", :id id}) (dom/props (dissoc props :id :label))
(let [edits (dom/OnAll "change" #(-> % .-target .-checked))] ; eagerly submit individual edits
(let [edits (dom/On-all "change" #(-> % .-target .-checked))] ; eagerly submit individual edits
(when-not (or (dom/Focused?) (pos? (e/Count edits)))
(set! (.-checked dom/node) checked))
edits))
Expand Down
2 changes: 1 addition & 1 deletion test/hyperfiddle/electric3_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@
(with ((l/single {}
(when-let [x (e/watch !x)]
(tap x)
(e/OnUnmount #(tap :bye)))) {} {})
(e/On-unmount #(tap :bye)))) {} {})
(reset! !x 1) % := 1
(swap! !x inc) % := 2
(swap! !x inc) % := 3
Expand Down

0 comments on commit fb72a0b

Please sign in to comment.