Design spec for morph::reactive::Store<ViewState, Msg>, ExhaustiveUpdate and request()
(include/morph/reactive/store.hpp): user-intent view state, changed only by messages, on the
nodes signals.md specifies.
Read this before giving a controller state of its own. Server data does not belong in a Store: it
is a Query (control.md).
ViewStateis a struct ofSignal<T>fields: what is selected, what is being typed, which tab is open. It is not called model: in morph a model is the domain object aBridgeHandler<Model>talks to.Msgis astd::variantof message types.Store(Runtime&, Init, Update):Initis invocable as(Runtime&)and returns exactlyViewState, by prvalue. Signals cannot move, so the Store's member is initialised from that prvalue directly.Updateis move-constructible — it may be move-only — and satisfiesExhaustiveUpdate<Update, ViewState, Msg>:Update&is invocable as(ViewState&, Alt const&)for every alternativeAltofMsg. An update that misses an alternative fails the constructor's constraint, so the Store does not compile. AMsgthat is not astd::variantnever satisfies it.- The runtime is borrowed and must outlive the Store.
send(msg)applies the update for the message's alternative in one batch, untracked: N field writes are one flush, and nothing the update reads subscribes anything.state()returnsViewState const&. The view reads it; only the update writes. A field'sget()tracks exactly that field.action(msg)returns astd::function<void()>that sends a copy ofmsgeach time it is called — what a button binds to. It refers to the Store, which must outlive it.sendoff the runtime's owner is reported (kOffOwner) and dropped.sendto a Store from inside that Store's own update is reported (kSendInUpdate) and dropped: an update is a pure state transition, and a message it sent would run against a half-applied state. The guard is per Store, so an update may send to a different Store.- An exception from the update propagates to the caller of
send. Writes the update made before it threw still flush, and the nextsendruns normally. - An update may destroy its own Store — a controller that closes its view. The update's callable
stays alive until it returns, and
sendtouches nothing of the Store afterwards. The update itself must not use the state or the Store once it has destroyed them.
request(store, handler, scope, action, toMsg, toFailMsg);Executes action through handler and sends toMsg(result) or toFailMsg(exception_ptr) to
store as one message. Each delivery is one send, so one batch.
- Errors travel per call, inside the message, never as a shared error string.
scopegates both callbacks: a reply arriving after it is stopped, reset or destroyed is dropped. The Store must outlive every delivery the scope does not gate.- Callbacks land on the handler's GUI executor, which must be the Store's owner; a delivery
anywhere else is refused by
sendlike any other off-owner call. - Every call delivers: there is no latest-wins. Superseding an earlier call means stopping,
resetting or destroying the scope it was given. A
Queryprovides latest-wins (control.md). - A throw is not a failure message. A throw from
toMsgor from the update it sends to, on the success path, or fromtoFailMsg, is caught and logged by theCompletiondelivering it (../core/completion.md, per-handler exception isolation).
Query and Mutation are not built on request(): neither owns a Store to send into. They follow
the same delivery rules — errors per call, one batch per delivery, every callback gated by a scope
— on Completion and CallbackScope directly. A controller reaches for those first, and for
request() when a reply should change user-intent state through the update.
The Store, the Runtime and every mounted view outlive any callback that can still fire. The
CallbackScope a controller passes to request() is the last member of the owning object, so
it is destroyed first and gates anything in flight
(../core/callback_scope.md).
| Decision | Why |
|---|---|
| Exhaustiveness as a constructor constraint | A message the update forgot is a compile error at the Store, not a silent no-op at run time. |
| The update runs untracked, in one batch | An update is a transition, not a binding: reading a field to compute the next value must not subscribe the caller, and a multi-field change must be one flush. |
send inside an update is refused, not queued |
A queued message would run against a state its sender had not finished writing, and the ordering would depend on the queue rather than the code. |
ViewState, not Model |
Model already names the domain object behind a BridgeHandler. |
Elm-style Cmd values. Server interaction a state change implies is a Query keyed on that state,
so an update never needs to issue a request.