Event router - registration and dispatch.
Ported from re-frame with JVM-first async support via core.async.
Supports two types of event handlers:
Dispatch modes:
Usage: ;; Initialize with app state atom (init! (atom {}))
;; Register handlers (reg-event-db :counter/inc (fn [db [_ amount]] (update db :count + amount)))
(reg-event-fx :user/fetch (fn [{:keys [db]} [_ user-id]] {:db (assoc db :loading? true) :http {:url (str "/users/" user-id)}}))
;; Dispatch events (dispatch [:counter/inc 5]) (dispatch-sync [:counter/inc 1])
Event router - registration and dispatch.
Ported from re-frame with JVM-first async support via core.async.
Supports two types of event handlers:
- reg-event-db: Handler receives db, returns new db
- reg-event-fx: Handler receives coeffects, returns effects map
Dispatch modes:
- dispatch: Async dispatch (queued, processed in order)
- dispatch-sync: Synchronous dispatch (immediate processing)
Usage:
;; Initialize with app state atom
(init! (atom {}))
;; Register handlers
(reg-event-db :counter/inc
(fn [db [_ amount]]
(update db :count + amount)))
(reg-event-fx :user/fetch
(fn [{:keys [db]} [_ user-id]]
{:db (assoc db :loading? true)
:http {:url (str "/users/" user-id)}}))
;; Dispatch events
(dispatch [:counter/inc 5])
(dispatch-sync [:counter/inc 1])(clear-event)(clear-event id)Clear event handler. If no id provided, clear all.
Clear event handler. If no id provided, clear all.
(dispatch event)Dispatch event asynchronously.
On JVM: Queues event for processing in order. Captures the caller's
thread bindings (via get-thread-bindings) so that dynamic vars
(e.g. test-isolation *test-conn*) propagate into the event-loop
thread when the handler runs.
On JS: Uses setTimeout for async behavior.
Dispatch event asynchronously. On JVM: Queues event for processing in order. Captures the caller's thread bindings (via `get-thread-bindings`) so that dynamic vars (e.g. test-isolation `*test-conn*`) propagate into the event-loop thread when the handler runs. On JS: Uses setTimeout for async behavior.
(dispatch-all events)Dispatch multiple events and wait for all to complete.
Returns a channel with all contexts.
Dispatch multiple events and wait for all to complete. Returns a channel with all contexts.
(dispatch-async event)Dispatch event and return a channel with the result.
Useful for waiting on event processing completion. Captures the
caller's thread bindings so dynamic-var isolation hooks propagate
into the go-block that runs process-event.
Dispatch event and return a channel with the result. Useful for waiting on event processing completion. Captures the caller's thread bindings so dynamic-var isolation hooks propagate into the go-block that runs `process-event`.
(dispatch-sync event)Dispatch event synchronously.
Processes event immediately, blocking until complete. Use for events that must complete before continuing.
Dispatch event synchronously. Processes event immediately, blocking until complete. Use for events that must complete before continuing.
(get-app-db)Return the inner db atom currently held by the router.
app-db is internally an atom-of-atom (an outer pointer containing the
caller-supplied inner atom, or the default inner atom when init! has
not been called). This returns the INNER atom — deref it to read the
current db value, or reset! / swap! it to mutate state.
Note: this is a snapshot of the pointer at call time; if init! is
later called with a different atom, the returned handle will be stale.
Return the inner db atom currently held by the router. `app-db` is internally an atom-of-atom (an outer pointer containing the caller-supplied inner atom, or the default inner atom when `init!` has not been called). This returns the INNER atom — deref it to read the current db value, or `reset!` / `swap!` it to mutate state. Note: this is a snapshot of the pointer at call time; if `init!` is later called with a different atom, the returned handle will be stale.
(init! db-atom)Initialize the router with an app-db atom.
May be called to install a caller-supplied app-db; dispatch also works without calling init! thanks to the default registrations above.
Initialize the router with an app-db atom. May be called to install a caller-supplied app-db; dispatch also works without calling init! thanks to the default registrations above.
(reg-event-db id handler)(reg-event-db id interceptors handler)Register an event handler that returns new db value.
Handler signature: (fn [db event] new-db)
Syntactic sugar for reg-event-fx handlers that only update :db.
Register an event handler that returns new db value. Handler signature: (fn [db event] new-db) Syntactic sugar for reg-event-fx handlers that only update :db.
(reg-event-fx id handler)(reg-event-fx id interceptors handler)Register an event handler that returns effects map.
Handler signature: (fn [coeffects event] effects-map)
Coeffects contain :db, :event, and any injected coeffects. Effects map can contain :db, :dispatch, and custom effects.
Register an event handler that returns effects map. Handler signature: (fn [coeffects event] effects-map) Coeffects contain :db, :event, and any injected coeffects. Effects map can contain :db, :dispatch, and custom effects.
(stop!)Stop the event processing loop and reset the queue.
Closes the current event-queue channel (causing the go-loop to terminate), waits (bounded) for the loop to actually exit, then installs a fresh channel and resets the processing flag.
After calling stop!, you can restart by calling init! and dispatching events.
Stop the event processing loop and reset the queue. Closes the current event-queue channel (causing the go-loop to terminate), waits (bounded) for the loop to actually exit, then installs a fresh channel and resets the processing flag. After calling stop!, you can restart by calling init! and dispatching events.
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 |