fitter
1.0.0
ไลบรารีการจัดการส่วนประกอบของระบบสำหรับ Clojure
ห้องสมุดวัตถุประสงค์ที่คล้ายกัน:
ส่วนประกอบที่น้อยที่สุดเป็นเพียงฟังก์ชั่นที่ได้รับ“ แผนที่” ของอินสแตนซ์ส่วนประกอบอื่น ๆ และส่งคืนอินสแตนซ์ส่วนประกอบนี้ ระบบ“ แผนที่” รองรับอินเตอร์เฟส ILookup เท่านั้นดูการทดสอบระบบ การค้นหาทั้งหมดอินสแตนซ์ส่วนประกอบที่ร้องขอและสร้างการพึ่งพาระหว่างส่วนประกอบแบบไดนามิก (!) ส่วนประกอบทั้งหมดจะถูกรวมเข้าด้วยกันอย่างแน่นหนาด้วยชื่อคีย์ระบบ
ส่วนประกอบที่สมบูรณ์กำหนดพฤติกรรมการเริ่มต้นหยุดและระงับ
( ns readme.component
( :require [strojure.fitter.component :as component]))
( def function-component
" Simple function describes component start behaviour. "
( fn [{ :keys [another-component]}]
( comment " Use " another-component)
:instance ))
( def constant-component
" Just constant component. "
( constantly true ))
( def map-component
" Component described as hash map with required `::component/start` key. "
{ ::component/start ( constantly :instance )
::component/stop! ( fn stop! [instance]
( comment " Destroy " instance))
::component/suspend! ( fn suspend! [old-instance old-system]
( comment " Suspend " old-instance old-system)
( fn resume [new-system]
( comment " Resume " old-instance new-system)
:instance ))})
( def assembled-component
" Same map as above created using `component/of`. "
( component/of ( constantly :instance )
( fn stop! [instance]
( comment " Destroy " instance))
( fn suspend! [old-instance old-system]
( comment " Suspend " old-instance old-system)
( fn resume [new-system]
( comment " Resume " old-instance new-system)
:instance )))) สถานะระบบเป็นอินสแตนซ์การถือครองตัวแปรของส่วนประกอบที่กำลังทำงาน สถานะจะเริ่มต้นโดย init แล้วเปลี่ยนโดย start! และ stop! -
( ns readme.system-state
( :require [strojure.fitter.system :as system]))
( def ^:private registry
{ ::a ( constantly ::a )
::b ( fn [{ ::keys [a]}] { ::b a})})
; ; Initialize system state.
( defonce ^:private system!
( system/init { :registry registry}))
; ; Start all system keys.
( system/start! system!)
; ; Stop all running keys.
( system/stop! system!)
; ; The `start!`, `stop!` and `deref` return the actual system map.
( let [{ ::keys [a b]} ( system/start! system!)]
( comment " Work with " a b))
( let [_ ( system/start! system!)
{ ::keys [a b]} ( deref system!)]
( comment " Work with " a b))
; ; Start/stop only specific keys.
( system/start! system! { :filter-keys #{ ::a }})
( system/stop! system! { :filter-keys #{ ::a }})
; ; Start/stop system incrementally.
( doto system! ( system/start! { :filter-keys #{ :a }})
( system/start! { :filter-keys ( complement #{ :a })}))
( doto system! ( system/stop! { :filter-keys ( complement #{ :a })})
( system/stop! { :filter-keys #{ :a }}))
; ; Update registry on start.
( system/start! system! { :registry ( assoc registry ::c ( constantly ::c ))})
; ; Suspend suspendable components on stop and resume them on start.
( doto system! ( system/stop! { :suspend true })
( system/start! ))
; ; Execute components in parallel
( doto ( system/init { :parallel true }) ( system/start! )
( system/stop! ))
( system/start! system! { :parallel true })
( system/stop! system! { :parallel true })
; ; Use `with-open` to stop system automatically.
( with-open [s! ( system/init { :registry registry})]
( let [{ ::keys [a b]} ( system/start! s!)]
( comment " Work with " a b)))