peripheral.core
attach
(attach component k attach-component)
(attach component k attach-component dependencies)
Attach the given component to this one (associng it using k
and having it depend on dependencies
).
concurrently
(concurrently component-seq)
Tag the seq of components as elligible to be concurrently started. The return value is the same sequence of components, but #’start-all will start them concurrently.
connect
deprecated in 0.5.0
(connect m src-id dst-id)
(connect m src-id src-key dst-id)
Let the component identified by src-id
be using the component identified by dst-id
as a dependency to be assoc’d in at the key src-key
.
defcomponent
macro
(defcomponent id dependencies & component-logic)
Create new component type from a vector of dependencies
(the components/fields that should be filled before the component is started, as well as a series of keyword/start/stop forms.
Each keyword has to be followed by a form that will be executed at startup and optionally a (non-keyword) function to be run at shutdown.
(defcomponent LoopRunner [f interval]
:data (fetch-some-data!)
:thread (doto (Thread. #(while (not (.isInterrupted ...)) ...))
(.start))
#(.interrupt ^Thread %))
The results of the form/function will be assoc’d into the component. Please use the map->...
function to create an instance of the record. Using fields with the namespace peripheral
you can manipulate the component record directly:
(defcomponent TestComponent [...]
:peripheral/start #(...) ;; called before fields are initialized
:peripheral/started #(...) ;; called after fields are initialized
:peripheral/stop #(...) ;; called before fields are cleaned up
:peripheral/stopped #(...)) ;; called after fields are cleaned up
Note that these take a function, not a form, and only allow for one value!
If you don’t need to alter the component record, the ‘on’ prefix can be used to directly execute forms:
(defcomponent TestComponent [...]
:on/start (println "starting"))
Finally, if you need access to the whole component, you can bind it to a symbol using ‘:this/as’:
(defcomponent TestComponent [x]
:this/as *this*
:y (+ (:x *this*) 10)
:z (- (:y *this*) 5))
defsystem+
macro
added in 0.5.0
(defsystem+ id & components)
(defsystem+ id [dependency ...] & components)
Create a new system of components. Example:
(defsystem+ MySystem [^:global configuration, api-options]
:engine []
:monitor [:scheduler :api]
:api [:engine {:options :api-options}]
:scheduler [:engine])
On startup, the following injections will happen:
:configuration
into:engine
,:api
and:scheduler
(since it is marked as:global
),:engine
into:api
and:scheduler
,:api-options
- aliased as:options
- into:api
,:scheduler
and:api
into:monitor
.
It is possible to give default values for each component using the :default
metadata:
(defsystem+ MySystem [...]
:engine ^{:default (default-engine)} []
...)
Unless the key :engine
is present and non-nil on system startup, it’ll be filled with the result of (default-engine)
.
reify-component
macro
(reify-component dependencies & component-logic)
(reify-component & component-logic)
Create a one-off component (i.e. without creating an explicit component record). The syntax is identical to defcomponent without the class name.
(let [db-config {...}]
(reify-component
:db (connect! db-config) disconnect!
...))
The resulting value will implement all map interfaces, as well as the com.stuartsierra.component/Lifecycle
protocol.
start
(start component)
Begins operation of this component. Synchronous, does not return until the component is started. Returns an updated version of this component.
stop
(stop component)
Ceases operation of this component. Synchronous, does not return until the component is stopped. Returns an updated version of this component.
subsystem
(subsystem system components)
Create subsystem. The resulting system will only start the given components and their dependencies.
with-start
macro
(with-start [form component & more] & body)
Start the given component, binding the started component to the given form, ensuring the shutdown of the component after the body has been executed.
with-start*
(with-start* component f)
Start the given component, call the given function with the started component as only parameter, then stop the component.