Skip to content

Commit

Permalink
fix removeEventListener issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchdzugan committed May 12, 2020
1 parent 4a30eda commit 30f38f5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 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.34"
(defproject mayu "0.1.35"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
Expand Down
6 changes: 4 additions & 2 deletions src/cljc/mayu/dom.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@
to-js
(.addEventListener el target handler))
(fn []
(.removeEventListener el target handler)))
(.removeEventListener el target handler
(get opts :capture true))))
e/Event
e/on!
(e/defer-off #(swap! buffered (a/curry conj %))))]
Expand All @@ -151,7 +152,8 @@
(when (and (on?) (not (empty? @buffered)))
(let [v (peek @buffered)]
(swap! buffered pop)
(send-self! v)))))))))))]
(send-self! v)
(recur)))))))))))]
[[{:res res :make-event-from-target make-event-from-target}
(curry update :mdom #(-> [(->MCreateElement tag key path attrs %1)]))]])

Expand Down
9 changes: 7 additions & 2 deletions src/cljc/mayu/examples.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
;; this component draws a styled input component and label and
;; returns an event that fires everytime its input changes with
;; the value it is being changed to
(def cc (atom 1))
(defui styled-input [k label]
;; for every dom tag like `div` there is a corresponding
;; version ending in _ like `div_` that returns the result
Expand All @@ -290,14 +291,18 @@
;; as its input signal changes.
<[apply s/unwrap-event
<[dom/bind sig $[val]=
<[button "Btn"] el-btn >
(dom/consume! (dom/on-click el-btn) #(do (println (str "Clicked! " @cc))
(swap! cc inc)))
<[input {:value val}] el-input >
[(->> (dom/on-input el-input)
(e/map #(.. % -target -value)))]]]]])
(e/map #(.. % -target -value))
(#(e/dedup % val)))]]]]])

(defui inputs-demo []
<[dom/collect-values ::upper "" $=
e-changed <- <[styled-input ::upper "Force uppercase"]
(dom/emit ::upper (e/map #(do (slow-fib 37)
(dom/emit ::upper (e/map #(do (println (slow-fib 40))
(clojure.string/upper-case %))
e-changed))]
<[dom/collect-values ::max-5 "" $=
Expand Down
48 changes: 26 additions & 22 deletions src/cljc/mayu/frp/impl/event.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -516,28 +516,32 @@
(fn [] (go (>! c :off))))
#(-> [])))))

(defn dedup [e]
(let [set? (atom false)
last (atom nil)]
(if (never? e)
never
(on! (Event
#(let [off (subscribe! e
(fn [{:keys [val src count] :as msg}]
(cond (push? msg)
(let [was? @set?
curr @last]
(reset! set? true)
(reset! last val)
(if (or (not was?)
(not= curr val))
(% msg)
(% (->Src src count))))
:else (% msg))))]
(fn []
(reset! set? false)
(off)))
#(:deps @(:state e)))))))
(defn dedup
([e] (dedup e false nil))
([e init] (dedup e true init))
([e init-set? init]
(let [set? (atom init-set?)
last (atom init)]
(if (never? e)
never
(on! (Event
#(let [off (subscribe! e
(fn [{:keys [val src count] :as msg}]
(cond (push? msg)
(let [was? @set?
curr @last]
(reset! set? true)
(reset! last val)
(if (or (not was?)
(not= curr val))
(% msg)
(% (->Src src count))))
:else (% msg))))]
(fn []
(when-not init-set?
(reset! set? false))
(off)))
#(:deps @(:state e))))))))

(defn before-on [e f]
(let [a-e (atom nil)]
Expand Down
9 changes: 5 additions & 4 deletions src/cljs/mayu/attach.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@

(defn before-input [event]
(let [target (aget event "target")
value (aget target "value")
path (aget target "__mayu_path")]
(when path
value (or (aget target "value") "")
path (aget target "__mayu_path")
set? (aget target "__mayu_set?")]
(when (and path (not= false set?))
(aset target "__mayu_last" value)
(aset target "__mayu_set?" false))))

Expand All @@ -67,7 +68,7 @@
last (aget target "__mayu_last")
set? (aget target "__mayu_set?")
buffered (or (aget target "__mayu_buffered_input") (atom a/queue))]
(when (and (= elm target) path (not set?) (empty? @buffered))
(when (and last (= elm target) path (not set?) (empty? @buffered))
(aset target "value" last))))

(defn add-after-input [prev curr]
Expand Down

0 comments on commit 30f38f5

Please sign in to comment.