Coeffects (cofx) system - dependency injection for event handlers.
Ported from re-frame/cofx.cljc with JVM compatibility.
Coeffects are input data that handlers need but shouldn't fetch themselves. They enable pure handlers by injecting all dependencies.
Built-in coeffects:
Usage: ;; Register a coeffect handler (reg-cofx :now (fn [coeffects] (assoc coeffects :now (System/currentTimeMillis))))
;; Inject into event handler (reg-event-fx :log/timestamp [(inject-cofx :now)] (fn [{:keys [db now]} _] {:db (assoc db :last-timestamp now)}))
Coeffects (cofx) system - dependency injection for event handlers.
Ported from re-frame/cofx.cljc with JVM compatibility.
Coeffects are input data that handlers need but shouldn't fetch themselves.
They enable pure handlers by injecting all dependencies.
Built-in coeffects:
- :db - Current application state
- :event - The event being processed
Usage:
;; Register a coeffect handler
(reg-cofx :now
(fn [coeffects]
(assoc coeffects :now (System/currentTimeMillis))))
;; Inject into event handler
(reg-event-fx :log/timestamp
[(inject-cofx :now)]
(fn [{:keys [db now]} _]
{:db (assoc db :last-timestamp now)}))(clear-cofx)(clear-cofx id)Clear coeffect handler. If no id provided, clear all.
Clear coeffect handler. If no id provided, clear all.
(get-cofx id)Get coeffect handler by id.
Get coeffect handler by id.
(inject-cofx id)(inject-cofx id value)Create an interceptor that injects a coeffect.
Can be used with or without a value parameter: (inject-cofx :now) ; calls handler with no extra arg (inject-cofx :config :prod) ; calls handler with :prod as second arg
The coeffect handler receives [coeffects] or [coeffects value].
Create an interceptor that injects a coeffect. Can be used with or without a value parameter: (inject-cofx :now) ; calls handler with no extra arg (inject-cofx :config :prod) ; calls handler with :prod as second arg The coeffect handler receives [coeffects] or [coeffects value].
(reg-cofx id handler)Register a coeffect handler.
(reg-cofx :cofx-id (fn [coeffects] (assoc coeffects :cofx-id (compute-value))))
Coeffect handlers:
Register a coeffect handler.
(reg-cofx :cofx-id
(fn [coeffects]
(assoc coeffects :cofx-id (compute-value))))
Coeffect handlers:
- Receive the current coeffects map
- Return updated coeffects map with new key(s) added
- Should be relatively fast (called during event processing)(registered-cofx-ids)Return set of registered coeffect handler IDs.
Return set of registered coeffect handler IDs.
(unreg-cofx id)Remove coeffect handler for cofx-id. Returns true if the handler was found and removed, false if not found. Thread-safe (uses swap! on atom).
Remove coeffect handler for cofx-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 |