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版或(根據您的選項)任何以後的版本。