Handler for establishing persistent SSE connections to component instances.
Manages a !connections atom that tracks, per (component-id, user-id), the
open SSE stream AND the component flow (taia.ds.datastar.use-cases.process-event)
that lives as long as it.
One route, any method and any event:
/ds/nds-connect/<component>/<component-id>/<event>. Every request opens a
connection: a new flow starts, the request's event runs through it, and its
render is sent down the stream. The flow is registered with the stream under
(component-id, user), replacing (and stopping) whatever was there, so a
reconnect or a reload simply takes over. The registered flow is what
send-event! puts server events on; their renders are pushed down the open
stream. Browser events after the open belong on /ds/nds/… (one-shot): a
request on the connect path would open a connection of its own.
The flow keeps nothing between events.
Handler for establishing persistent SSE connections to component instances. Manages a `!connections` atom that tracks, per (component-id, user-id), the open SSE stream AND the component flow (`taia.ds.datastar.use-cases.process-event`) that lives as long as it. One route, any method and any event: /ds/nds-connect/<component>/<component-id>/<event>. Every request opens a connection: a new flow starts, the request's event runs through it, and its render is sent down the stream. The flow is registered with the stream under (component-id, user), replacing (and stopping) whatever was there, so a reconnect or a reload simply takes over. The registered flow is what `send-event!` puts server events on; their renders are pushed down the open stream. Browser events after the open belong on /ds/nds/… (one-shot): a request on the connect path would open a connection of its own. The flow keeps nothing between events.
Dynamic endpoint handler for component events. Handles routes like /:component/:component-id/:event.
The event runs through a component flow of its own
(process-component-event: request → message → the process-event use
case); the response
shaping (render-response, error-response) lives in
taia.ds.datastar.adapters.handlers.response. This namespace only owns the
Ring handler entrypoint.
Dynamic endpoint handler for component events. Handles routes like /:component/:component-id/:event. The event runs through a component flow of its own (`process-component-event`: request → message → the process-event use case); the response shaping (`render-response`, `error-response`) lives in `taia.ds.datastar.adapters.handlers.response`. This namespace only owns the Ring handler entrypoint.
Lookup cache for Datastar component resolution.
WHAT IS CACHED: the resolved NAMESPACE SYMBOLS, never the vars.
The expensive half of discovery/discover-component is the search-path scan:
for "chat.chat" an earlier prefix such as my.app.admin.components does not hold it, so
require throws a FileNotFoundException (stack trace and all) before the scan
falls through to my.app.components.chat.chat. That answer never
changes for a given [component-name controller-name] pair, so it is what we
store. Turning the cached symbols back into vars is a namespace-map lookup
with no I/O, and it is done on every hit — which keeps the cache REPL-safe:
caching the vars themselves would survive an ordinary defn re-evaluation
(same var object, new root binding) but go stale on remove-ns / :reload-all,
which mint brand-new var objects.
Deliberately NOT cache.interface: that component's contract is
EDN-serializable values under a TTL, and it would add a cross-component
dependency to the intentionally generic datastar component.
Lookup cache for Datastar component resolution. WHAT IS CACHED: the resolved NAMESPACE SYMBOLS, never the vars. The expensive half of `discovery/discover-component` is the search-path scan: for "chat.chat" an earlier prefix such as `my.app.admin.components` does not hold it, so `require` throws a FileNotFoundException (stack trace and all) before the scan falls through to `my.app.components.chat.chat`. That answer never changes for a given [component-name controller-name] pair, so it is what we store. Turning the cached symbols back into vars is a namespace-map lookup with no I/O, and it is done on every hit — which keeps the cache REPL-safe: caching the vars themselves would survive an ordinary `defn` re-evaluation (same var object, new root binding) but go stale on `remove-ns` / `:reload-all`, which mint brand-new var objects. Deliberately NOT `cache.interface`: that component's contract is EDN-serializable values under a TTL, and it would add a cross-component dependency to the intentionally generic `datastar` component.
Pluggable observation hook for the two Datastar entry points
(/ds/nds and /ds/nds-connect). datastar stays metrics-agnostic: it
reports plain maps to ONE observer fn installed at startup
(taia.ds.datastar.interface/set-observer!), and does nothing at all when none is set.
Observations:
No component-id is ever reported — every instance of a component is one
series. And the reported values are BOUNDED: component and event come
from the URL, so anyone could mint new ones. A component counts only once
discovery has resolved it (the discovery cache holds it), an event only
when that component's machine declares it; everything else collapses to
unknown-component / other-event.
Pluggable observation hook for the two Datastar entry points
(/ds/nds and /ds/nds-connect). `datastar` stays metrics-agnostic: it
reports plain maps to ONE observer fn installed at startup
(`taia.ds.datastar.interface/set-observer!`), and does nothing at all when none is set.
Observations:
- {:type :event :route :nds|:nds-connect :component c :event e
:outcome :ok|:error :seconds s}
- {:type :sse-opened :component c}
- {:type :sse-closed :component c :seconds lifetime}
No component-id is ever reported — every instance of a component is one
series. And the reported values are BOUNDED: component and event come
from the URL, so anyone could mint new ones. A component counts only once
discovery has resolved it (the discovery cache holds it), an event only
when that component's machine declares it; everything else collapses to
`unknown-component` / `other-event`.From a Ring request to the plain event message a component's flow takes
(see taia.ds.datastar.use-cases.process-event.common). This is the only
place that knows where Datastar puts things in a request: the event's
params, the component's slice of the signal store, the logged user.
From a Ring request to the plain event message a component's flow takes (see `taia.ds.datastar.use-cases.process-event.common`). This is the only place that knows where Datastar puts things in a request: the event's params, the component's slice of the signal store, the logged user.
The component context: how it is built from what the client sent, and how removed memory keys become client-side signal removals. Pure functions over plain data: no request, no channels.
The component context: how it is built from what the client sent, and how removed memory keys become client-side signal removals. Pure functions over plain data: no request, no channels.
An event against a component's state machine: its keyword, whether it is one of Datastar's own, what the machine steps from, and the :from-state stamped on what it returns. Pure: running the machine is the controller's.
An event against a component's state machine: its keyword, whether it is one of Datastar's own, what the machine steps from, and the :from-state stamped on what it returns. Pure: running the machine is the controller's.
Malli schemas for the Datastar component pipeline context.
Malli schemas for the Datastar component pipeline context.
The use case: process a component's event. It runs as a
clojure.core.async.flow of three procs, each in its own namespace:
in-chan ──▶ :controller ──:to-view──▶ :view ──▶ out-chan (state machine, (render) one step at a time) process-event.view process-event.controller │ :to-invoke ▲ :from-invoke ▼ :from-machine │ :to-machine :invoke (an invoke's :src, off the controller) process-event.invoke
The :invoke proc is taia.state-machines' (taia.state-machines.flow/ invoke-step), so its two ports have that library's names: to it, the
proc on the other side is the machine.
The messages are described in process-event.common. Everything here is
plain data in and out: the caller resolves the component (its machine and
render fn, as vars) and turns a request into a message (adapters); the
rules about the ctx and the transition are the domain's.
Two lifetimes:
process-event):
put the event, take the render or time out, stop the flow. One render
per event: the last step's;start-flow! with :async? true), so a later event — the connect's
INIT or a server send-event! — goes in through the same in-chan. Every
step is rendered: a state with an :invoke is seen (a spinner, say) while
its :src runs, then where it lands.
Either way nothing is kept between events: each starts from the client's
signals (a click) or from the machine's initial state (a server event,
which brings what it needs as its params).The use case: process a component's event. It runs as a
`clojure.core.async.flow` of three procs, each in its own namespace:
in-chan ──▶ :controller ──:to-view──▶ :view ──▶ out-chan
(state machine, (render)
one step at a time) process-event.view
process-event.controller
│ :to-invoke ▲ :from-invoke
▼ :from-machine │ :to-machine
:invoke (an invoke's :src, off the controller)
process-event.invoke
The :invoke proc is taia.state-machines' (`taia.state-machines.flow/
invoke-step`), so its two ports have that library's names: to it, the
proc on the other side is the machine.
The messages are described in `process-event.common`. Everything here is
plain data in and out: the caller resolves the component (its machine and
render fn, as vars) and turns a request into a message (adapters); the
rules about the ctx and the transition are the domain's.
Two lifetimes:
- a DEFAULT component's event gets a flow of its own (`process-event`):
put the event, take the render or time out, stop the flow. One render
per event: the last step's;
- a CONNECTED component's flow lives as long as its SSE stream
(`start-flow!` with `:async? true`), so a later event — the connect's
INIT or a server `send-event!` — goes in through the same in-chan. Every
step is rendered: a state with an :invoke is seen (a spinner, say) while
its :src runs, then where it lands.
Either way nothing is kept between events: each starts from the client's
signals (a click) or from the machine's initial state (a server event,
which brings what it needs as its params).What travels through a component's flow — the contract the procs
(process-event.controller, process-event.invoke, process-event.view)
and the flow's callers share.
IN (on the flow's in-chan, whoever sends it):
{:event [event-name params] ; required :signals {…} ; the component's slice of the client's signals, or nil :request-ctx {:logged-user … :app-config … :request-method …} ; only for an event that came with a request :mulog-ctx ctx ; optional: the sender's trace, re-established :reply promise} ; optional: rides through to the render
Plain data: no Ring request. adapters.handlers.request/->event-msg builds
one from a request; server-event builds one for a server event.
BETWEEN the controller and the :invoke proc (over the controller's
:to-invoke / :from-invoke ports): the incoming message with the ctx
being stepped, plus :invoke on the way out and the raised event on the
way back. Any other key rides along, :mulog-ctx and :reply included.
To the view and OUT (on the flow's out-chan): {:event :initial-ctx :ctx …}
with the same :mulog-ctx and :reply, plus :html once the view has
rendered — or an error message when a step threw (error, error-of). One
per event, or one per step in an :async? flow (a connected component's).
Errors travel as messages, never to the flow's error-chan.
What travels through a component's flow — the contract the procs
(`process-event.controller`, `process-event.invoke`, `process-event.view`)
and the flow's callers share.
IN (on the flow's in-chan, whoever sends it):
{:event [event-name params] ; required
:signals {…} ; the component's slice of the client's signals, or nil
:request-ctx {:logged-user … :app-config … :request-method …}
; only for an event that came with a request
:mulog-ctx ctx ; optional: the sender's trace, re-established
:reply promise} ; optional: rides through to the render
Plain data: no Ring request. `adapters.handlers.request/->event-msg` builds
one from a request; `server-event` builds one for a server event.
BETWEEN the controller and the :invoke proc (over the controller's
`:to-invoke` / `:from-invoke` ports): the incoming message with the ctx
being stepped, plus `:invoke` on the way out and the raised event on the
way back. Any other key rides along, `:mulog-ctx` and `:reply` included.
To the view and OUT (on the flow's out-chan): {:event :initial-ctx :ctx …}
with the same `:mulog-ctx` and `:reply`, plus `:html` once the view has
rendered — or an error message when a step threw (`error`, `error-of`). One
per event, or one per step in an `:async?` flow (a connected component's).
Errors travel as messages, never to the flow's error-chan.The :controller proc of a component's flow: the state machine, one step
at a time.
An event from the flow's in-chan (an in-port) starts from the ctx
domain.ctx builds (the client's signals, or the machine's initial state
for a server event); an event back from the :invoke proc (:from-invoke)
steps on from the ctx it carries. Each step is taia.state-machines'
(taia.state-machines.flow/machine-transform): entering a state with an
:invoke sends it on :to-invoke; the step goes to the view on :to-view
when it is the last — or every step, in an :async? flow (a connected
component's). Nothing is kept between events: the ctx travels in the
messages.
The `:controller` proc of a component's flow: the state machine, one step at a time. An event from the flow's in-chan (an in-port) starts from the ctx `domain.ctx` builds (the client's signals, or the machine's initial state for a server event); an event back from the `:invoke` proc (`:from-invoke`) steps on from the ctx it carries. Each step is taia.state-machines' (`taia.state-machines.flow/machine-transform`): entering a state with an :invoke sends it on `:to-invoke`; the step goes to the view on `:to-view` when it is the last — or every step, in an `:async?` flow (a connected component's). Nothing is kept between events: the ctx travels in the messages.
The :invoke proc of a component's flow: the :src of a state the
controller's step entered (an HTTP call, a save, …), off the controller.
It is taia.state-machines' invoke step (taia.state-machines.flow/invoke-step):
it takes {:ctx :invoke …} on :from-machine, runs the :src on the ctx
without its :state, and sends the message back on :to-machine with the
event it raised ([:on-done result] / [:on-error ex-data]) — or the :error,
when the :src threw something other than an ex-info. Every other key rides
along. Here each :src runs under the sender's trace (:mulog-ctx), and is
traced at :debug (around-invoke).
The `:invoke` proc of a component's flow: the :src of a state the
controller's step entered (an HTTP call, a save, …), off the controller.
It is taia.state-machines' invoke step (`taia.state-machines.flow/invoke-step`):
it takes {:ctx :invoke …} on `:from-machine`, runs the :src on the ctx
without its `:state`, and sends the message back on `:to-machine` with the
event it raised ([:on-done result] / [:on-error ex-data]) — or the `:error`,
when the :src threw something other than an ex-info. Every other key rides
along. Here each :src runs under the sender's trace (`:mulog-ctx`), and is
traced at :debug (`around-invoke`).The :view proc of a component's flow: rendering.
It calls the component's render fn with the ctx the controller emitted
(:from-controller) and puts the result, as :html (hiccup or an effects
list), on the flow's out-chan (the :to-render out-port). An error
message from the controller passes through untouched.
The `:view` proc of a component's flow: rendering. It calls the component's render fn with the ctx the controller emitted (`:from-controller`) and puts the result, as `:html` (hiccup or an effects list), on the flow's out-chan (the `:to-render` out-port). An error message from the controller passes through untouched.
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 |