ห้องสมุดที่จัดการวงจรชีวิตของส่วนประกอบของรัฐ นี่คือรูปแบบของ 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 และ :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 เนมสเปซนี้ถือว่าเป็นการทดลองเนื้อหาอาจเปลี่ยนแปลงได้ในทุกรุ่น
เอเจนต์ ("ผู้ดูแล") ถือสถานะปัจจุบันของระบบพร้อมกับข้อยกเว้นของ Lifecycle ตัวอย่างการใช้งาน:
( 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.Component ลำดับของ nrcompost/stop อาจแตกต่างจากการเริ่มต้นย้อนกลับหนึ่ง การพึ่งพาที่ประกาศอย่างชัดเจนเท่านั้นที่ได้รับการเคารพเท่านั้น หากคุณต้องการบัญชีสำหรับการพึ่งพาโดยนัยคุณควรเพิ่มองค์ประกอบเพิ่มเติมให้กับส่วนประกอบ ' :require คอลเลกชัน
ฉันชอบสิ่งที่ส่วนประกอบมีให้ แต่ฉันก็ต้องการ
Lifecycle จะเข้ามาในทางเมื่อคุณต้องการส่วนประกอบเฉพาะกิจเท่านั้น ข้อกำหนดสำหรับส่วนประกอบที่จะเป็นแผนที่และใช้งานวงจรชีวิตอย่างมีประสิทธิภาพ จำกัด ส่วนประกอบให้เป็นบันทึก นอกจากนี้ยังหมายความว่าบางครั้งผู้คนหันไปทำงานเพื่อหลีกเลี่ยงการสร้างประเภทใหม่ลิขสิทธิ์© petr gladkikh [email protected]
แจกจ่ายภายใต้ใบอนุญาตสาธารณะ Eclipse ไม่ว่าจะเป็นเวอร์ชัน 1.0 หรือ (ตามตัวเลือกของคุณ) เวอร์ชันใด ๆ ในภายหลัง