Skip to content

Commit

Permalink
fix first render cycle events continuously firing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchdzugan committed May 9, 2020
1 parent 3c2ba9d commit 1740c8c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject mayu "0.1.27"
(defproject mayu "0.1.28"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
Expand Down
14 changes: 14 additions & 0 deletions src/cljc/mayu/examples.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,27 @@
(dom/emit :c1 (dom/on-click b3))
<[p $= <[span (str "c3 " c3)]]]]]]]])

(defui stop-prop []
<[dom/collect-reduce-and-bind :a inc 0 $[a]=
<[div $=
<[dom/collect-reduce-and-bind :b inc 0 $[b]=
<[p (str "A: " a)]
<[p (str "B: " b)]
<[button "B"] d-b >
(->> (dom/on-click d-b)
(e/map #(.stopPropagation %))
(dom/emit :b))]
] d-a >
(dom/emit :a (dom/on-click d-a))])

(defui my-ui []
[(let [c (dom/render-to-string {} ssr-await-demo)]
(go-loop []
(let [more (<! c)]
(when more
(println more)
(recur)))))]
<[stop-prop]
<[div "1"]
<[div 1]
<[div nil]
Expand Down
93 changes: 93 additions & 0 deletions src/cljc/mayu/frp/\
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(ns mayu.frp.signal
(:require [allpa.core
:refer [varg#]]
[wayra.core :as w
:refer [defnm defm mdo]]
#?(:clj [clojure.core :as core]
:cljs [cljs.core :as core])
[mayu.frp.impl.event :as e]))

(defn off! [s] ((:off! s)))
(defn inst! [s] ((:inst! s)))
(defn hot-swap! [s e] ((:hot-swap! s) e))
(def changed :changed)
(defn consume! [s f] ((:consume! s) f))

(defnm step [k m]
step-fn <- (w/asks :step-fn)
((or step-fn (fn [_ m] m)) k m))

(defm active-signal
getter <- (w/asks :active-signal)
(or getter (w/pure nil)))

(defnm default-setter [signal]
(w/tell (fn [] (off! signal))))

(defnm set-active [signal]
setter <- (w/asks :set-active)
((or setter default-setter) signal))

(defnm raw-from [get-init e-arg]
signal <-
(mdo
active <- active-signal
active --> [(hot-swap! active e-arg)
active]
let [e-internal (e/on! (e/Event))
sval (atom (get-init))
changed (->> e-internal
e/flatten
(e/filter #(not= %1 @sval))
(e/map #(do (reset! sval %1)
%1)))
off! (let [unsub (e/consume! changed (fn [x]))]
(fn []
(unsub)
(e/off! changed)))]
[(e/push! e-internal e-arg)
{:off! off!
:inst! (fn [] @sval)
:hot-swap! (fn [e-next]
(e/push! e-internal e-next))
:changed changed
:consume! (fn [f]
(f @sval)
(e/consume! changed f))}])
(set-active signal)
[signal])

(defnm from [init e]
(step ::from (raw-from #(-> init) e)))

(defnm const [val]
(step ::const (raw-from #(-> val) e/never)))

(defn build [b]
(let [{:keys [result writer]} (w/exec {} b)]
{:off (fn [] (doseq [off writer]
(off)))
:signal result}))

(defn unwrap-event [se]
(e/join-skip-siblings (inst! se)
(e/flatten (changed se))))

(defn map [f s]
(step ::map (raw-from #(f (inst! s)) (e/map f (changed s)))))

(defnm reduce [r i-arg e]
(step ::reduce
(mdo active <- active-signal
reader <- w/ask
let [i (if active (inst! active) i-arg)]
(raw-from #(-> i) (e/reduce r i e)))))

(defnm zip-with [f & sigs]
(step ::zip-with
(mdo let [e-changed (apply e/join-skip-siblings (core/map changed sigs))
val! (varg# (apply f (core/map inst! sigs)))]
(raw-from val! (e/map val! e-changed)))))

(defnm shadow [s]
(step ::shadow (raw-from #(inst! s) (e/shadow (changed s)))))
2 changes: 2 additions & 0 deletions src/cljc/mayu/frp/event.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

(def map e/map)

(def until e/until)

(def reduce e/reduce)

(def filter e/filter)
Expand Down
21 changes: 21 additions & 0 deletions src/cljc/mayu/frp/impl/event.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@
msg))))
#(:deps @(:state e))))))

(defn until [get-e e-until]
(on! (Event
#(let [a-off-1 (atom (fn []))
a-off-2 (atom (fn []))]
(reset! a-off-1 (subscribe! (get-e) %))
(reset! a-off-2
(subscribe! e-until
(fn [msg]
(when (push? msg)
(@a-off-1)
(@a-off-2)
(reset! a-off-1 (fn []))
(reset! a-off-2 (fn []))
(% (->Deps []))))))
(fn []
(@a-off-1)
(@a-off-2)
(reset! a-off-1 (fn []))
(reset! a-off-2 (fn []))))
#(:deps @(:state (get-e))))))

(defn reduce [r i e]
(if (never? e)
never
Expand Down
3 changes: 2 additions & 1 deletion src/cljc/mayu/frp/signal.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
:signal result}))

(defn unwrap-event [se]
(e/join-skip-siblings (inst! se) (e/flatten (changed se))))
(e/join-skip-siblings (e/until #(inst! se) (changed se))
(e/flatten (changed se))))

(defn map [f s]
(step ::map (raw-from #(f (inst! s)) (e/map f (changed s)))))
Expand Down
15 changes: 15 additions & 0 deletions test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@
(e/push! e 5)
(e/push! e 6)
(is (= @consumed [4 5])))))
(testing "until"
(let [consumed (atom [])
e1 (e/on! (e/Event))
e2 (e/on! (e/Event))
untild (e/until #(-> e1) e2)]
(e/push! e1 1)
(e/push! e1 2)
(let [off (e/consume! untild #(swap! consumed (curry conj %1)))]
(e/push! e1 3)
(e/push! e2 :O)
(e/push! e1 4)
(off)
(e/push! e1 5)
(e/push! e1 6)
(is (= @consumed [3])))))
(testing "filter"
(let [consumed (atom [])
e (e/on! (e/Event))
Expand Down

0 comments on commit 1740c8c

Please sign in to comment.