Add reactivity #25
Description
Hi,
So I've been experimenting with references and watches, and I came with idea of a trait that allows you to hook to the reference and alter the widget when reference value changes:
(deftrait :ref-text [^android.widget.TextView wdg {:keys [ref-text]}]
(add-watch ref-text nil
(fn [_ _ o n] (post wdg (.setText wdg n)))))
This helped me to refrain from fetching widgets by ids/vars and just allowed to focus on the values prepared for them:
(def txt (atom nil))
(def ui [:linear-layout {:id-holder true}
[:text-view {:text "Hello from Clojure!!!"}]
[:edit-text {:text "Initial text" :ref-text txt}]])
(reset! txt "Some other text")
There is one issue here: I haven't hooked to the widget destroy event to release the reference that watcher holds (I haven't yet found the way to do it, am not very knowledgeable in android SDK ;) ), but I'm pretty sure it can easily be done.
Real question is, do you think it would be feasible to alter existing traits to work this way if the value specified is a reference (just additional case)? Many traits would benefit from such thing, and it would give neko that 'reactive' feel (possibility of using dataflow programming a'la javelin for example, is so tempting). Or whether it's better as an 'add-on' (like I did in my code)?