Effects (fx) system - side effect handlers.
Ported from re-frame/fx.cljc with JVM compatibility.
Effects are declarative descriptions of side effects. Each effect type has a registered handler that performs the actual work.
Built-in effects:
Usage: ;; Register a custom effect handler (reg-fx :http (fn [{:keys [method url on-success]}] (http-request method url (fn [response] (dispatch [on-success response])))))
;; Event handler returns effects map (reg-event-fx :user/login (fn [{:keys [db]} [_ credentials]] {:db (assoc db :loading? true) :http {:method :post :url "/api/login" :body credentials}}))
Effects (fx) system - side effect handlers.
Ported from re-frame/fx.cljc with JVM compatibility.
Effects are declarative descriptions of side effects.
Each effect type has a registered handler that performs the actual work.
Built-in effects:
- :db - Update application state
- :dispatch - Dispatch another event
- :dispatch-n - Dispatch multiple events
Usage:
;; Register a custom effect handler
(reg-fx :http
(fn [{:keys [method url on-success]}]
(http-request method url
(fn [response]
(dispatch [on-success response])))))
;; Event handler returns effects map
(reg-event-fx :user/login
(fn [{:keys [db]} [_ credentials]]
{:db (assoc db :loading? true)
:http {:method :post :url "/api/login" :body credentials}}))Dynamic var for intercepting effect execution.
When bound to a function, do-fx-seq calls this instead of executing effects directly.
Used by run-sub-fsm-fx to capture child effects without executing them (thread-safe).
Default: nil (effects execute normally).
Dynamic var for intercepting effect execution. When bound to a function, `do-fx-seq` calls this instead of executing effects directly. Used by `run-sub-fsm-fx` to capture child effects without executing them (thread-safe). Default: nil (effects execute normally).
(clear-fx)(clear-fx id)Clear effect handler. If no id provided, clear all.
Clear effect handler. If no id provided, clear all.
(do-fx effects)Execute all effects in an effects map.
Processes effects in undefined order (except :db which is always first). Unknown effects are warned about but don't throw.
Execute all effects in an effects map. Processes effects in undefined order (except :db which is always first). Unknown effects are warned about but don't throw.
(do-fx-seq effects)Execute effects from a sequential collection of [effect-id value] tuples.
Unlike do-fx (which takes a map), this preserves ordering and allows
the same effect-id to appear multiple times.
Used by the FSM engine when handlers return {:data ... :fx [...]}.
When *fx-interceptor* is bound, delegates to it instead of executing
effects directly. This enables sub-FSM effect capture (thread-safe).
Example: (do-fx-seq [[:log {:msg "starting"}] [:http {:url "/api"}] [:log {:msg "done"}]])
Execute effects from a sequential collection of [effect-id value] tuples.
Unlike `do-fx` (which takes a map), this preserves ordering and allows
the same effect-id to appear multiple times.
Used by the FSM engine when handlers return `{:data ... :fx [...]}`.
When `*fx-interceptor*` is bound, delegates to it instead of executing
effects directly. This enables sub-FSM effect capture (thread-safe).
Example:
(do-fx-seq [[:log {:msg "starting"}]
[:http {:url "/api"}]
[:log {:msg "done"}]])(reg-fx id handler)Register an effect handler.
(reg-fx :effect-id (fn [effect-value] ;; perform side effect ))
Effect handlers:
Register an effect handler.
(reg-fx :effect-id
(fn [effect-value]
;; perform side effect
))
Effect handlers:
- Receive the effect value from the effects map
- Perform the side effect
- Return value is ignored(registered-fx-ids)Return set of registered effect handler IDs.
Return set of registered effect handler IDs.
(unreg-fx id)Remove effect handler for fx-id. Returns true if the handler was found and removed, false if not found. Thread-safe (uses swap! on atom).
Remove effect handler for fx-id. Returns true if the handler was found and removed, false if not found. Thread-safe (uses swap! on atom).
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 |