ステートフルコンポーネントのライフサイクルを管理するライブラリ。これは、https://github.com/stuartsierra/componentプロジェクトのアイデアのバリエーションです。現時点では、このライブラリは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 and :stop関数は、コンポーネントの新しい値を返す必要があります:thisコンポーネントがリソースを取得した場合:start必要があります: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 namespaceによって実装されています。この名前空間は実験的であると考えられており、どのバージョンでも内容が変化する可能性があります。
エージェント(「キーパー」)は、ライフサイクルの例外とともに、システムの現在の状態を保持します。使用例:
( 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 ])} nrcompost/stopのcom.stuartsierra.componentシーケンスとは異なり、リバーススタートアップ1とは異なる場合があることに注意してください。明示的に宣言された依存関係のみが尊重されます。暗黙の依存関係を考慮する必要がある場合は、コンポーネントに追加の要素を追加する必要があります:require 。
コンポーネントが提供するものが好きですが、私も欲しいです
Lifecycleの実装は、アドホックコンポーネントのみが必要な場合に邪魔になります。また、コンポーネントがマップになり、ライフサイクルを実装するための要件は、コンポーネントをレコードに効果的に制限します。これはまた、新しいタイプの作成を避けるために人々が作業アラウンドに頼ることがあることを意味します。Copyright©Petr gladkikh [email protected]
Eclipse Publicライセンスの下でバージョン1.0または(オプションで)後のバージョンのいずれかで配布されます。