I am trying understand how to model events in ClojureScript. Designing an event queue in JavaScript is easy. You just keep a (mutable) array of functions, and provide helper functions to add or remove callbacks from the array. When you trigger the event, just go through all callbacks listed in the array and invoke them one after the other.
This paradigm is far from functional style - for instance there is no point in triggering the callbacks unless they have side effects. Moreover it is implemented using a mutable array. Still it seems to me that in ClojureScript one needs to be able to do event-driven programming to do anything useful. Now, I know that Google Closure already implements an event library, but my question is about implementing it natively.
Since all basic Clojure/ClojureScript data types are immutable, what would be an idiomatic way to implement this mechanism? Changing a ref? Resorting to mutable data structures from the host (Java resp JavaScript)?
If I understand correctly, agents are possibly the right tool in Clojure, but I see they are currently not implemented in ClojureScript.
Clojurescript One has a library, one.dispatch
that is a good starting point. The wiki has usage examples here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With