compost
1.0.0
管理状态组件的生命周期的库。这是https://github.com/stuartsierra/component Project的想法的变体。目前,该库不支持Clojurescript。
将此依赖性添加到您的项目中
[net.readmarks/compost "0.2.0"]
请参阅测试以获取示例。组件声明具有形式
{ :requires #{ :required-component-id-1 :required-component-id-2 }}
:this initial-state
:get ( fn [this] ...) ; ; Returns value of this component that other components will get as dependency.
:start ( fn [this dependency-components-map] ...) ; ; Acquire resources (open connections, start threads ...)
:stop ( fn [this] ...)} ; ; Release resources.所有字段都是可选的,默认值为:
{ :requires #{}
:this nil
:get identity
:start ( fn [this dependency-components-map] this)
:stop identity} :start和:stop功能应返回组件的新值:this 。如果组件获取资源:start IT必须将它们释放为:stop 。系统声明是一张普通地图
{ :component-1-id component-1-declaration
:component-2-id component-2-declaration
...
}生命周期用法示例
( require '[net.readmarks.compost :as compost])
( let [s ( compost/start system-map #{ :web-component :some-worker-component })]
( Thread/sleep 5000 )
( compost/stop s))您可以在例外之后挽救当前的系统状态,如下所示:
( try
( compost/start system-map)
( catch ExceptionInfo ex
( if-let [sys ( compost/ex-system ex)]
( compost/stop sys) ; ;; Handle this system as desired here.
( throw ex))))此功能由Net.ReadMarks.Compost.Keeper名称空间实现。该名称空间被认为是实验性的,其内容可能会在任何版本中都改变。
代理(“守护者”)与生命周期的例外保持了当前系统状态。用法示例:
( require '[net.readmarks.compost :as compost])
( require '[net.readmarks.compost.keeper :as keeper])
( def sys ( keeper/keeper system-map))
( keeper/update-keeper! sys compost/start)
; ; Now sys holds current system value and errors if there are any.
; ; (:system @sys) is current system map value.
; ; (:errors @sys) is list of of system lifecycle errors. It is empty if system changes were successful.
; ; You can send these errors into a log, for example:
( keeper/flush-errors! sys println) ; ; The function gets errors one by one.您可以根据以下方式调整现有组件:
( defn component-using [init using]
{ :requires ( set using)
:this init
:start ( fn [this deps]
( -> ( merge this deps)
component/start)
:stop component/stop})
( def system
{ :conn-source ( component-using
( ->MyConnPool )
[])
:dao ( component-using
( map->MyDbComponent {})
[ :conn-source ])}请注意,与com.stuartsierra.com不同, nrcompost/stop的序列可能与反向启动ONE不同。仅尊重明确声明的依赖项。如果您需要考虑隐式依赖关系,则应将其他元素添加到组件:require集合。
我喜欢组件提供的,但我也想要
Lifecycle会妨碍您。同样需要组件为地图并实现生命周期有效地将组件限制为记录。这也意味着有时人们会诉诸于工作,以避免创建新类型。版权所有©Petr Gladkikh [email protected]
根据Eclipse公共许可分发行1.0版或(根据您的选项)任何以后的版本。