Event driven programming in Clojure / ClojureScript

I am trying to understand how to model events in ClojureScript. Designing an event queue in JavaScript is easy. You simply save the (mutable) array of functions and provide helper functions to add or remove callbacks from the array. When you fire an event, just go through all the callbacks listed in the array and call them one by one.

This paradigm is far from functional style - for example, it makes no sense to run callbacks if they have no side effects. In addition, it is implemented using a mutable array. However, it seems to me that ClojureScript needs to do event-driven programming to do something useful. Now I know that Google Closure already implements an event library, but my question is how to implement it.

Since all basic Clojure / ClojureScript data types are immutable, what would be an idiomatic way to implement this mechanism? Link change? To resort to mutable data structures from the host (Java and JavaScript)?

If I understand correctly, agents are probably the right tool in Clojure, but I see that they are not currently implemented in ClojureScript.

+7
source share
3 answers

Clojurescript One has one.dispatch library, which is a good starting point. There are usage examples on the wiki here

+5
source

Since 2013, the best way to achieve event handling in clojure is the excellent core.async library:

core.async allows core.async to program events with channels in the same way that Go .

+2
source

Changing the ref / atom would be great if you want to implement such a solution, since that is what most clojure libraries do, etc. when they have a situation where they need a store to store / delete files at runtime.

+1
source

All Articles