상태가 많은 구성 요소의 수명주기를 관리하는 라이브러리. 이것은 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 :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 의 순서와 달리 리버스 스타트 업과 다를 수 있습니다. 명시 적으로 선언 된 종속성 만 존중됩니다. 암시 적 의존성을 설명 해야하는 경우 구성 요소에 추가 요소를 추가해야합니다 :require .
나는 구성 요소가 제공하는 것을 좋아하지만 또한 원합니다
Lifecycle 구현은 임시 구성 요소 만 필요할 때 방해가됩니다. 또한 구성 요소가 맵이되고 수명주기를 구현 해야하는 요구 사항은 구성 요소를 레코드로 효과적으로 제한합니다. 이것은 또한 때때로 사람들이 새로운 유형을 만드는 것을 피하기 위해 근로에 의지한다는 것을 의미합니다.Copyright © Petr Gladkikh [email protected]
Eclipse Public License에 따라 버전 1.0 또는 이후 버전에서 배포됩니다.