re-frame.flows¶
A flow keeps a derived value in app-db. You name the input paths, a pure function over the values at those paths, and the app-db path to write; whenever an input value changes, re-frame recomputes the function and writes the result, so no event handler has to remember to. Use a flow when an event handler, a schema or another flow must read the derived value as plain app-db data; when only views read it, use a subscription. When one handler needs the value once, compute it in that handler.
Flows ship in the optional day8/re-frame2-flows artefact. Require re-frame.flows at app boot to load it; without it, rf/reg-flow, (rf/clear :flow id) and a frame whose :flows is not empty throw :rf.error/flows-artefact-missing.
(rf/reg-flow :cart/subtotal
{:inputs [[:cart :items] [:tax :rate]] ;; app-db paths to watch
:output-path [:cart :subtotal]} ;; where the result is written
(fn [items rate] ;; input values, in order
(* (reduce + (map :price items)) (+ 1 (or rate 0)))))
;; => :cart/subtotal
(rf/make-frame {:id :rf/default :flows [:cart/subtotal]}) ;; this frame runs it
;; [:cart :subtotal] now follows the items and the rate. Read it with a plain
;; subscription or from any handler's db; leave writing it to the flow.
A flow is a top-level registration under the registrar's :flow kind, and a frame runs the flows its :flows config names. Register and clear flows through the facade, with rf/reg-flow and (rf/clear :flow id); read them with the ordinary registrar queries (Reading flows). The model is taught in Flows: derived values your handlers can read.
Registering and clearing flows¶
reg-flow¶
- Kind: macro (
rf/reg-flow); also a function,re-frame.flows/reg-flow, which records no source coordinates - Signature:
- Description: Registers a flow definition and returns
flow-id. It reads no frame, so it needs no scope and no live frame; registering at namespace load is the normal case.- Unlike
reg-sub's:inputs, each entry is a path (see Options), andderive-fnreceives the values as separate positional arguments, in order.derive-fnreturns the output; it must be pure and deterministic, and runs whenever an input value changes. - The flow runs inside each event of every frame whose
:flowsnames it, after the handler and before the newapp-dbinstalls. When flows run gives the order. - A bare path in
:inputsreadsapp-db; a path led by:rf.db/runtimereadsruntime-db(route or machine state), with the partition key stripped before the read. The output always goes toapp-db. See Deriving from route or machine state. - Leave
:output-pathto the flow and change the inputs instead. Nothing stops an event writing the output path directly, but the flow overwrites that value the next time an input changes. - Registering an id again replaces the definition, which is how hot reload picks up a changed
derive-fn. Each frame running it recomputes on its next event; if the new definition moves:output-path, that event removes the value at the old path. See Re-registering a flow. - A rejected registration changes nothing; any previous definition stays in place.
- Unlike
-
Options (the
metadatamap):Key Required Notes :inputsyes A vector of paths into app-db, or intoruntime-dbwhen a path starts with:rf.db/runtime. Each path is a non-empty vector of concrete segments (keywords, strings, symbols, integers, booleans, UUIDs, instants ornil), so a flow cannot read the wholeapp-dbthrough[]. The value at each path is passed toderive-fnin the same order.:output-pathyes Where the output is written in app-db: a non-empty vector of concrete segments, not starting with:rf.db/runtime. It may not overlap this flow's own:inputs, or another flow's output path in a frame that runs both (Activation).:docno One sentence on what the flow computes and why; shown in tooling. :sensitiveno A vector of output subpaths (each a vector of scalar keys; []is the whole output) whose values are sensitive. They are recorded rooted at:output-pathin each frame that runs the flow, so trace and egress projections redact them. A flow classifies only its own output; its inputs' classification does not carry over. There is no:sensitive?boolean: write:sensitive [[]]for the whole output.:largeno A vector of output subpaths (the same shape as :sensitive) marked large for wire-size elision.:large?no Boolean; truemarks the whole output large.- Errors: - :rf.error/invalid-flow-metadata:metadatais not a map, or a:derive,:frameor:schemakey sits inside it (the derive function is the third argument, which frames run a flow is each frame's:flows, and the output's shape is declared withreg-app-schemaat:output-path). The ex-data's:valuecarries the offending value.- :rf.error/flow-missing-id/:rf.error/flow-bad-id:flow-idisnil/ not a keyword.- :rf.error/flow-bad-inputs::inputsis not a vector, or an entry is not a non-empty vector of concrete segments; the ex-data's:bad-entrieslists them.- :rf.error/flow-bad-output:derive-fnis not a function.- :rf.error/flow-bad-path::output-pathis not a non-empty vector of concrete segments;:bad-elementslists any bad segments.- :rf.error/flow-bad-marks: a malformed:sensitive,:largeor:large?, or a:sensitive?or:rf.egress/output-sensitivitykey;:bad-keynames it.- :rf.error/flow-reserved-output-path::output-pathstarts with:rf.db/runtime. That prefix is only valid in:inputs; outputs always writeapp-db.- Cycles and output overlaps depend on which flows a frame runs together, so they are checked when a frame is made (Activation). - Example:
Activation¶
- Kind: frame config key (
:flowsonmake-frameandframe-root) - Signature:
- Description: A frame runs the flows its
:flowsvector names, and no others; omitting it, or passing[], runs none. The order carries no meaning: flows run in dependency order.- Each id resolves through the frame's own view of the registrations: live for a frame made without
:images, through its sealed image for one made with them. - A frame runs its flows from its first event, so a seed in
:initial-eventsalready writes their outputs. - Re-making a frame with the same
:idand a different:flowsupdates it in place: an added flow runs from the next event, and a dropped one has its output removed on that event. A mountedframe-rootrefuses a changed:flowswith:rf.error/frame-root-reconfigured. - Destroying a frame drops its evaluation memo. The definitions stay registered, and sibling frames are unaffected.
- Each id resolves through the frame's own view of the registrations: live for a frame made without
- Errors (the frame is not made):
:rf.error/unregistered-flow: an id the frame's view does not resolve, or a:flowsvalue that is not a vector of keywords. The ex-data carries:frame,:flow-idand:flows.:rf.error/ambiguous-registration: an id two namespaces register, in a frame made without:images.:rf.error/flow-path-overlap: two of the frame's flows have output paths that are equal or one a prefix of the other. The ex-data's:overlapis{:flow-ids [a b] :paths [path-a path-b]}. Two flows may write one path as long as no frame runs both.:rf.error/flow-cycle: the frame's flows depend on each other in a cycle. Flow B depends on flow A, and runs after it in each pass, when one of B's:inputsand A's:output-pathis a prefix of the other. A flow whose own:inputsoverlap its:output-pathis a cycle of one, so:inputs [[:cart]]with:output-path [:cart :subtotal]is refused; read the specific keys instead. The ex-data's:cyclenames the chain with its first id repeated at the end:[:a :b :a], or[:cart/subtotal :cart/subtotal]for a cycle of one.:rf.error/flows-artefact-missing::flowsis not empty and the flows artefact is not loaded.- A re-registration that creates a cycle or an overlap in a running frame's set is caught on that frame's next event, which aborts with
:rf.error/flow-eval-exception(see When a flow throws); every later event does the same until a further registration repairs the set.
- Example:
Clearing a flow¶
- Kind: function (the facade's
rf/clear) - Signature:
- Description: Removes the flow definition
idfrom the registrar and returnsid.:flowis one of the kindsclearaccepts.- Each frame whose
:flowsnamesidremoves the value at its:output-pathon its next event and warns:rf.warning/unregistered-flow, naming the frame and the id. The frame keeps running its other flows, and registering the id again resumes it. - Only the leaf is removed. A map parent loses the key, and a parent map left empty stays in place. A vector parent keeps its length and the slot is set to
nil, so later elements do not shift. - A no-op when
idis not registered. - It takes no opts: a trailing opts map raises
:rf.error/registrar-clear-bad-request. To stop one frame running a flow, re-make that frame without it.
- Each frame whose
- Example:
When flows run¶
Flows run inside the event pipeline, in a pass the router makes once per event:
- After the handler, before the install. Every event a frame handles runs the flow pass over the flows its
:flowsnames as the router's outermost:afterinterceptor: after the handler and every other interceptor, so apathinterceptor has already spliced its slice back into the wholeapp-db, and before the newapp-dbinstalls and the event's:fxrun. An event whose handler returns no:dbstill runs the pass, over the currentapp-db. - Against the pending state. Inputs are read from the state the event is about to commit, its pending
app-dbandruntime-db. Each recomputed output is written into the pendingapp-db, so the event installsapp-dbonce, with the handler's write and the flows' outputs together. A handler therefore reads a flow's output as it stood after the previous event. - In dependency order, dirty-checked. A flow that reads another flow's output runs after it and sees the value computed in the same pass. A flow whose input values are
=to those of its last run skipsderive-fnand emits:rf.flow/skip. - Checked with the event. The frame's app-db schemas validate the pending
app-dbwith the flows' outputs in it (What is validated). When they reject it, nothing installs and the frame's evaluation memo does not advance, since it advances only with the install, so the flows recompute on the next event. - Not after a failure. When the handler or an interceptor throws, the pass does not run. When a flow throws, the event commits nothing; see When a flow throws.
- Writes made by
:fx. The pass has already run when the:fxwalk starts, so an effect that writesapp-dborruntime-db, such as a machine spawn or a user effect callingswap-runtime-db!, lands after it. When the walk changed the frame's state, the event runs the pass again over what the walk left and installs the outputs before it ends, so the flows see the write in the same event and epoch, before any event the handler dispatched. A throw in that second pass installs nothing of it and reports:rf.error/flow-eval-exceptionagainst the event; the walk's writes stand. - Never outside an event. Registering, re-registering or clearing a flow runs nothing; each frame running it reconciles the change on its next event.
- Re-registration. A frame whose memo row was computed with a different definition recomputes that flow on its next event even when its inputs are unchanged. When the new definition moves
:output-path, that pass removes the value at the old path in the same pendingapp-db.
When a flow throws¶
If a flow's derive-fn throws, or its result cannot be written at :output-path, the event aborts before app-db is installed. Nothing from that event is committed, the flows after it in the pass do not run, and the flow is tried again on the frame's next event. A throw in the second pass an event runs after its :fx wrote the frame's state installs nothing of that pass; the event's own install and its :fx writes stand. The failure is reported as :rf.error/flow-eval-exception. What happens when a derive throws walks through an example.
The error goes through the always-on error reporting, so production builds report it too, to the frame's :observability :errors sinks (or the process default's). See Report errors in production to wire the listeners.
The error record carries :where :flow-eval, :flow-id, and a top-level :phase:
:derive: yourderive-fnthrew.:output-write:derive-fnreturned, but re-frame could not write the value at:output-path, usually because the pendingapp-dbholds a container there that cannot take the path's last segment. Fix the:output-pathor the shape at its parent; thederive-fnis not at fault.
The phase is decided by which step threw, never by reading the exception message. It sits at the top level of the record so it survives an egress profile that drops :exception. The thrown exception's ex-data carries :rf.flow/failed-id, :rf.flow/failed-phase and :rf.flow/output-path.
Tracing¶
Development builds trace each flow's lifecycle. Evaluation events carry :flow-id and :frame in their tags, and their input and output values are elided according to the frame's :sensitive / :large classification. Registration events name the flow and no frame, because a registration belongs to none.
| Operation | When | Other tags |
|---|---|---|
:rf.flow/registered |
A flow id is registered for the first time. | :inputs, :path (no :frame) |
:rf.registry/handler-replaced |
A registration replaces a flow (:kind :flow, the flow id under :id). |
:different-fn?, comparing the derive-fn (no :frame) |
:rf.flow/computed |
derive-fn ran and its result was written. |
:input-values, :before, :result, :path, :elapsed-ms |
:rf.flow/skip |
The inputs were = to the last run, so derive-fn did not run. |
:reason :inputs-value-equal, :input-paths-unchanged; :repaired? true when the visit wrote the flow's remembered output back |
:rf.flow/cleared |
(rf/clear :flow id) removed a registration. |
:path (no :frame) |
:rf.warning/unregistered-flow |
A frame's pass found an id its :flows names without a registration, and removed that flow's output. |
:frame, :path, :reason |
:rf.flow/failed |
The flow threw; see When a flow throws. | :phase, :path, :exception-message, :exception-data, :inputs |
Reading flows¶
A flow definition is an ordinary registration, so the registrar queries in re-frame.core read it; re-frame.flows has no read API of its own.
(rf/registrations {:kind :flow}) ;; every definition
(rf/handler-meta {:kind :flow :id :cart/subtotal}) ;; one, from the store
(rf/handler-meta {:frame :rf/default :kind :flow :id :cart/subtotal}) ;; as that frame resolves it
(:flows (rf/frame-meta :rf/default)) ;; the ids a frame runs
The map is the one reg-flow stored: :id, :inputs, :output-path, :derive (the derive function), the other metadata keys (:doc, the classification keys), and, for an rf/reg-flow call in a development build, source coordinates (:ns, :line, :column, :file).
A tool that draws subscriptions, flows, resources, route facts and machine selectors as one dependency graph takes each flow's graph node from re-frame.flows.tooling, which it requires directly. re-frame.flows has no function for it.
Framework integration¶
Not for application code — used by adapters, tools and the test harness.
run-flows-on-db¶
- Kind: function
- Signature:
- Description: Runs the flows a frame's
:flowsnames over a pending frame-state and returns the candidate: the resultingapp-dbunder:db, plus what the caller publishes once that candidate installs. The router runs it as the outermost:afterinterceptor, so it runs last, against the chain's pending:dbeffect and beforeapp-dbis installed, and again after an:fxwalk that wrote the frame's state; When flows run places it in the event.- Flows run in dependency order. Each is dirty-checked, and each recomputed result is written with
assoc-ininto the returnedapp-db. Outputs writeapp-dbonly. dbis the pendingapp-db;runtime-dbis the pendingruntime-db(passnilto resolve only bareapp-dbinputs).- The pass does not advance the frame's evaluation memo. When the memo would change, the result carries
:publish!, a no-argument function the caller invokes once the candidate has installed. - A throw stops the pass and re-raises as
:rf.error/flow-eval-exception(see When a flow throws). The router then discards the pending:dbeffect, so the event commits nothing and the memo stays as it was.
- Flows run in dependency order. Each is dirty-checked, and each recomputed result is written with
reset-flows!¶
- Kind: function
- Signature:
- Description: Test-only. Drops every frame's evaluation memo, so no flow is skipped against a stale,
=-equal entry. Definitions are registrations, which a test fixture snapshots and restores with the rest of the registrar.re-frame.test-support'smake-reset-runtime-fixturecalls it around each test. Returnsnil.
See also¶
- re-frame.core: the
rf/reg-flowfacade entry,rf/clear,make-frame's:flows, and the registrar queries. - Migration reference: re-frame v1's
on-changesandenrichare replaced by flows (and, forenrich, schemas).