Is there an idiomatic way handle state in clojure?
I am writing a couple of endpoints manages a scheduler, for that I use quartzite.
I would like to have the scheduler instance visible across schedule, reschedule and unschedule functions, the easiest way is to create an atom but I guess it's not the best way to keep the code from smelling.
I would like to know if there's an idiomatic way to handle state in clojure?
A common approach is to use a library like Component or Integrant to manage the lifecycle and dependency injection of state.
In those cases you tend to end up with a single atom containing a system of interrelated state.
In my personal experience I use Quartzite and Component - I have a scheduler component that is available to start/stop/schedule etc.
I prefer Integrant these days and will slowly move my projects from Component.
I believe, it's really OK to have an atom in your code, but store as little data in it as possible, only those ones that are really needed to be changed.
You cannot avoid having state completely, and atoms are great for that.
链接地址: http://www.djcxy.com/p/66884.html上一篇: 使用可变参数模板创建静态数组