(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))
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))
```
(reify-component & component-logic)(reify-component dependencies & 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.
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.(restart component)Restart the given component by calling stop and start.
Restart the given component by calling `stop` and `start`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |