babashka / sci.configs

A collection of ready to be used SCI configs
https://babashka.org/sci.configs/
Other
19 stars 17 forks source link

add reagent `track!` and `dispose!` #17

Closed rgkirch closed 1 year ago

rgkirch commented 1 year ago

The test currently doesn't actually work.

rgkirch commented 1 year ago

I expect that commenting out (r/dispose! click-count-tracker) would fail the test but it doesn't. Needs work.

borkdude commented 1 year ago

Does it work with the code outside of SCI?

rgkirch commented 1 year ago

I haven't tested that yet.

rgkirch commented 1 year ago
(ns sandbox.app
  (:require [reagent.core :as r]
            [reagent.dom :as rdom]))

(def click-count (r/atom 1))

(def integral (r/atom 0))

(def click-count-tracker (r/track! (fn integrate [] (swap! integral + @click-count))))

(defn counting-component []
  [:div @integral])

(defn init []
  (rdom/render [counting-component] (js/document.getElementById "app"))

  (swap! click-count inc)

  (r/dispose! click-count-tracker)

  (swap! click-count inc))

gives 1 while

(ns sandbox.app
  (:require [reagent.core :as r]
            [reagent.dom :as rdom]))

(def click-count (r/atom 1))

(def integral (r/atom 0))

(def click-count-tracker (r/track! (fn integrate [] (swap! integral + @click-count))))

(defn counting-component []
  [:div @integral])

(defn init []
  (rdom/render [counting-component] (js/document.getElementById "app"))

  (swap! click-count inc)

  (swap! click-count inc))

gives 4

🤔

rgkirch commented 1 year ago

(ns sandbox.app
  (:require [reagent.core :as r]
            [reagent.dom :as rdom]))

(def click-count (r/atom 1))

(add-watch click-count (gensym) (fn [k r o n] (println "click-count: " n)))

(def integral (r/atom 0))

(add-watch integral (gensym) (fn [k r o n] (println "integral: " n)))

(def click-count-tracker (r/track! (fn integrate []
                                     (println (str "(swap! integral + " @click-count ")"))
                                     (swap! integral + @click-count))))

(defn counting-component []
  [:div @integral])

(defn init []
  (rdom/render [counting-component] (js/document.getElementById "app"))

  (swap! click-count inc)

  (r/dispose! click-count-tracker)

  (swap! click-count inc))
(swap! integral + 1)
integral:  1
click-count:  2
click-count:  3

(ns sandbox.app
  (:require [reagent.core :as r]
            [reagent.dom :as rdom]))

(def click-count (r/atom 1))

(add-watch click-count (gensym) (fn [k r o n] (println "click-count: " n)))

(def integral (r/atom 0))

(add-watch integral (gensym) (fn [k r o n] (println "integral: " n)))

(def click-count-tracker (r/track! (fn integrate []
                                     (println (str "(swap! integral + " @click-count ")"))
                                     (swap! integral + @click-count))))

(defn counting-component []
  [:div @integral])

(defn init []
  (rdom/render [counting-component] (js/document.getElementById "app"))

  (swap! click-count inc)

  (swap! click-count inc))
(swap! integral + 1)
integral:  1
click-count:  2
click-count:  3
(swap! integral + 3)
integral:  4
borkdude commented 1 year ago

Btw, I'm fine with just adding these functions. Since they are just functions, I don't see why they would behave differently inside SCI than outside SCI...

rgkirch commented 1 year ago

Ok. Should I just remove the test?

borkdude commented 1 year ago

Fine with me