Controllers in Keechma are the place where you put the code
that has side-effects. They are managed by the keechma.controller-manager
which will start them or stop them based on the current route.
Each controller implements the params
function. params
function returns
a subset of the route params that are the controller is interested in.
For instance let's say that you have a UserController
which should be
running only when the user is on the route /users
:
;; let's say that your routes are defined like this:
(def routes [":page"]) ;; Routes are managed by the app-state library.
;; When user goes to the url `/users` the params function would receive
;; something that looks like this:
{:data {:page "users"}}
;; `params` function returns `true` only when user is on the `:page` "users"
(defrecord UserController [])
(defmethod controller/params UserController [_ route-params]
(when (= "users" (get-in route-params [:data :page]))
true))
When params
function returns a non nil
value the controller will be started:
start
function will be synchronously called with the current
application state. This function returns a new version of the state if needed.
(if the start
function is not doing any changes to the app-state it must return
the received version)handler
function will be called - this function will receive
application state atom, channel through which the controller receives the commands
(in-chan
) and the channel through which the controller can send commends to
other controllers (out-chan
).When params
function returns a nil
value that instance of the controller will
be stopped:
stop
function will be synchronously called with the current
application state. This function returns a new version of the state if needed -
use this function to clean up any data loaded by the controller (if the stop
function is not doing any changes to the app-state it must return the received
version).in-chan
(through which it can receive commands) will be closed.Controller's start
and stop
functions can asynchronuously send commends to the
controller. Calling (execute controller-instance :command)
will put that command
on the controller's in-chan
. Controller can react to these commands from the
handler
function.
Controllers in Keechma are the place where you put the code that has side-effects. They are managed by the [[keechma.controller-manager]] which will start them or stop them based on the current route. Each controller implements the `params` function. `params` function returns a subset of the route params that are the controller is interested in. For instance let's say that you have a `UserController` which should be running only when the user is on the route `/users`: ```clojure ;; let's say that your routes are defined like this: (def routes [":page"]) ;; Routes are managed by the app-state library. ;; When user goes to the url `/users` the params function would receive ;; something that looks like this: {:data {:page "users"}} ;; `params` function returns `true` only when user is on the `:page` "users" (defrecord UserController []) (defmethod controller/params UserController [_ route-params] (when (= "users" (get-in route-params [:data :page])) true)) ``` When `params` function returns a non `nil` value the controller will be started: 1. Controller's `start` function will be synchronously called with the current application state. This function returns a new version of the state if needed. (if the `start` function is not doing any changes to the app-state it must return the received version) 2. Controller's `handler` function will be called - this function will receive application state atom, channel through which the controller receives the commands (`in-chan`) and the channel through which the controller can send commends to other controllers (`out-chan`). When `params` function returns a `nil` value that instance of the controller will be stopped: 1. Controller's `stop` function will be synchronously called with the current application state. This function returns a new version of the state if needed - use this function to clean up any data loaded by the controller (if the `stop` function is not doing any changes to the app-state it must return the received version). 2. Controller's `in-chan` (through which it can receive commands) will be closed. Controller's `start` and `stop` functions can asynchronuously send commends to the controller. Calling `(execute controller-instance :command)` will put that command on the controller's `in-chan`. Controller can react to these commands from the `handler` function.
No vars found in this namespace.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close