Skip to content

Commit

Permalink
Fix listener cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raystubbs committed May 9, 2024
1 parent 9799303 commit ec6c878
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/zero/component.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ Implements web components. Require this ns to enable them.
(doseq [[k watchable] (:zero.core/bind props)]
(when (some? watchable)
(dom/unbind [watchable dom k])))
(doseq [[k listener] (:zero.core/on props)]
(when (some? listener)
(if (instance? Signal k)
(sig/unlisten k [listener dom k])
(dom/unlisten [listener dom k]))))
(gobj/set dom dom/PROPS-SYM (dissoc props :zero.core/bind)))
(doseq [child-dom (-> dom .-childNodes array-seq)]
(prepare-dom-to-be-detached child-dom !instance-state))
Expand Down
2 changes: 1 addition & 1 deletion src/zero/impl/signals.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@

(defn unlisten
[sig k]
(swap! !listeners dissoc-in [(.-key sig) k])
(swap! !listeners dissoc-in [(.-key sig) k])
nil)
24 changes: 18 additions & 6 deletions test/zero/browser/component_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@
(assert (= "FOO" (-> el .-shadowRoot .-firstChild .-innerText)))
(reset! !atom "ZZZ"))
(fn [^js/HTMLElement el]
(assert (= "FOO" (-> el .-shadowRoot .-firstChild .-innerText))))))
(assert (= "FOO" (-> el .-shadowRoot .-firstChild .-innerText))))
(fn [^js/HTMLElement el]
(set! (.-vdom el) [:div]))
(fn [_]
(assert (empty? @@(resolve 'zero.impl.dom/!binds)))
(assert (empty? @@(resolve 'zero.impl.dom/!bound-props))))))

(js/it "listens for events"
(let [!invoked? (atom false)]
Expand All @@ -119,8 +124,12 @@
::z/on {:click #(reset! !invoked? true)}]]))
(fn [^js/HTMLElement el]
(-> el .-shadowRoot .-firstChild .click))
(fn [^js/HTMLElement _el]
(assert (= true @!invoked?))))))
(fn [_]
(assert (= true @!invoked?)))
(fn [^js/HTMLElement el]
(set! (.-vdom el) [:div]))
(fn [_]
(assert (empty? @@(resolve 'zero.impl.dom/!listener-aborters)))))))

(js/it "listens for signals and receives correct context"
(let [!signal-context (atom nil)
Expand All @@ -132,10 +141,13 @@
[:root>
[:button
::z/on {my-signal #(reset! !signal-context %)}]]))
(fn [^js/HTMLElement _el]
(fn [_]
(my-signal))
(fn [^js/HTMLElement el]
(prn @!signal-context)
(assert (= el (::z/host @!signal-context)))
(assert (= (.-shadowRoot el) (::z/root @!signal-context)))
(assert (= (-> el .-shadowRoot .-firstChild) (::z/current @!signal-context))))))))
(assert (= (-> el .-shadowRoot .-firstChild) (::z/current @!signal-context))))
(fn [^js/HTMLElement el]
(set! (.-vdom el) [:div]))
(fn [_]
(assert (empty? @@(resolve 'zero.impl.signals/!listeners))))))))

0 comments on commit ec6c878

Please sign in to comment.