المكتبة التي تدير دورة حياة المكونات الحكومية. هذا تباين في فكرة مشروع https://github.com/stuartsierra/component. في الوقت الحالي ، لا تدعم هذه المكتبة clojurecript.
أضف هذا الاعتماد على مشروعك
[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. تعتبر مساحة الاسم هذه تجريبية ، وقد تتغير محتوياتها في أي إصدار.
يحتفظ الوكيل ("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 Startup One. يتم احترام التبعيات المعلنة بشكل صريح فقط. إذا كنت بحاجة إلى حساب التبعيات الضمنية ، فيجب عليك إضافة عناصر إضافية إلى المكونات :require مجموعات.
يعجبني ما يوفره المكون ولكني أريد أيضًا
Lifecycle في الطريق عندما تحتاج فقط إلى مكون مخصص. كما أن شرط أن يكون المكون خريطة وتطبيق دورة الحياة يقيد المكون بشكل فعال ليكون سجلًا. هذا يعني أيضًا أنه في بعض الأحيان يلجأ الناس إلى عمليات العمل لتجنب إنشاء أنواع جديدة.حقوق الطبع والنشر © Petr Gladkikh [email protected]
تم توزيعه ضمن ترخيص Eclipse Public إما الإصدار 1.0 أو (في خيارك) أي إصدار لاحق.