简体   繁体   English

是否有惯用的方式处理Clojure中的状态?

[英]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. 我想让调度程序实例在调度,重新调度和取消调度功能之间可见,最简单的方法是创建一个atom,但我想这不是防止代码散发出臭味的最佳方法。

I would like to know if there's an idiomatic way to handle state in clojure? 我想知道是否有惯用的方法来处理Clojure中的状态?

A common approach is to use a library like Component or Integrant to manage the lifecycle and dependency injection of state. 一种常见的方法是使用诸如ComponentIntegrant之类的库来管理状态的生命周期和依赖项注入。

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. 根据我的个人经验,我使用Quartzite和Component-我有一个Scheduler组件,可用于启动/停止/调度等。

I prefer Integrant these days and will slowly move my projects from Component. 这些天我更喜欢Integrant,并且会慢慢将我的项目从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. 您无法避免完全拥有状态,原子对此非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM