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])(append-interceptor! id interceptor)Append interceptor to the chain of an already-registered id.
Idempotent on the interceptor's :id. Returns true when appended, false when the event is unregistered or the :id is already in the chain.
Append `interceptor` to the chain of an already-registered `id`. Idempotent on the interceptor's :id. Returns true when appended, false when the event is unregistered or the :id is already in the chain.
(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.
(get-event id)Return the registry entry for id, or nil.
Entry shape: {:interceptors <normalized user chain> :handler <fn>}. The handler-interceptor is NOT part of the stored chain; dispatch appends it, so a caller is free to append its own interceptors without ordering hazard.
Return the registry entry for `id`, or nil.
Entry shape: {:interceptors <normalized user chain> :handler <fn>}. The
handler-interceptor is NOT part of the stored chain; dispatch appends it, so
a caller is free to append its own interceptors without ordering hazard.(get-interceptors id)The normalized user interceptor chain registered for id, or nil.
Does not include the handler-interceptor, which dispatch appends. For inspection and for a caller checking its own interceptor is installed.
The normalized user interceptor chain registered for `id`, or nil. Does not include the handler-interceptor, which dispatch appends. For inspection and for a caller checking its own interceptor is installed.
(handler-interceptor handler)Build the terminal interceptor that invokes handler and MERGES its effect
map into the context's effects.
Build the terminal interceptor that invokes `handler` and MERGES its effect map into the context's effects.
(handler-registered? id)True when an event handler is registered for id.
True when an event handler is registered for `id`.
(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.
(registered-event-ids)Set of registered event ids.
Set of registered event ids.
(registry-snapshot)Current registry value. For inspection and for save/restore in tests.
Current registry value. For inspection and for save/restore in tests.
(restore-registry! handlers)Replace the whole registry with handlers. For test isolation.
Replace the whole registry with `handlers`. For test isolation.
(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.
(unreg-event id)Remove the handler for id. Returns true when one was removed.
Remove the handler for `id`. Returns true when one was removed.
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 |