morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::reactive::Store< ViewState, Msg > Class Template Reference

User-intent view state — what is selected, typed, open — changed only by messages. More...

#include <store.hpp>

Public Member Functions

template<typename Init, typename Update>
requires std::invocable<Init&, Runtime&> && std::same_as<std::invoke_result_t<Init&, Runtime&>, ViewState> &&
std::move_constructible<Update> && ExhaustiveUpdate<Update, ViewState, Msg>
 Store (Runtime &runtime, Init init, Update update)
 ~Store ()
 Destroys the state, and tells an update still running that the Store is gone.
 Store (Store const &)=delete
Store & operator= (Store const &)=delete
 Store (Store &&)=delete
Store & operator= (Store &&)=delete
void send (Msg msg)
 Applies msg in one batch. Refused off the owner, and inside an update.
ViewState const & state () const noexcept
 The state, for reading. A field's get() tracks exactly that field.
std::function< void()> action (Msg msg)
 A callable that sends msg each time it is called — what a button binds to.

Detailed Description

template<typename ViewState, typename Msg>
class morph::reactive::Store< ViewState, Msg >

User-intent view state — what is selected, typed, open — changed only by messages.

ViewState is a struct of Signal<T> fields, built in place by init (signals cannot move). Every send() runs the update in one batch, untracked, so N field writes are one flush. The view reads state(); only the update writes.

send() off the Runtime's owner is reported (detail::site::kOffOwner) and dropped, as is a send() from inside this Store's own update (detail::site::kSendInUpdate): an update is a pure state transition, and a message it sent would run against a half-applied state.

An update may destroy its own Store (a controller that closes its view): the update's callable stays alive until it returns, and send() touches nothing of the Store afterwards. The update itself must not use the state or the Store once it has destroyed them.

Template Parameters
ViewStateA struct of Signal<T> fields.
MsgA std::variant of message types.

Definition at line 93 of file store.hpp.

Constructor & Destructor Documentation

◆ Store()

template<typename ViewState, typename Msg>
template<typename Init, typename Update>
requires std::invocable<Init&, Runtime&> && std::same_as<std::invoke_result_t<Init&, Runtime&>, ViewState> &&
std::move_constructible<Update> && ExhaustiveUpdate<Update, ViewState, Msg>
morph::reactive::Store< ViewState, Msg >::Store ( Runtime & runtime,
Init init,
Update update )
inline
Template Parameters
InitCallable (Runtime&) -> ViewState, returning by prvalue.
UpdateCallable satisfying ExhaustiveUpdate<Update, ViewState, Msg>; it may be move-only.
Parameters
runtimeThe runtime. Borrowed: it must outlive the Store.
initBuilds the initial state.
updateApplies one message to the state.

Definition at line 103 of file store.hpp.

◆ ~Store()

template<typename ViewState, typename Msg>
morph::reactive::Store< ViewState, Msg >::~Store ( )
inline

Destroys the state, and tells an update still running that the Store is gone.

Definition at line 110 of file store.hpp.

Member Function Documentation

◆ send()

template<typename ViewState, typename Msg>
void morph::reactive::Store< ViewState, Msg >::send ( Msg msg)
inline

Applies msg in one batch. Refused off the owner, and inside an update.

An exception from the update propagates to the caller; writes made before it still flush.

Parameters
msgThe message.

Definition at line 124 of file store.hpp.

◆ state()

template<typename ViewState, typename Msg>
ViewState const & morph::reactive::Store< ViewState, Msg >::state ( ) const
inlinenodiscardnoexcept

The state, for reading. A field's get() tracks exactly that field.

Returns
The state.

Definition at line 153 of file store.hpp.

◆ action()

template<typename ViewState, typename Msg>
std::function< void()> morph::reactive::Store< ViewState, Msg >::action ( Msg msg)
inlinenodiscard

A callable that sends msg each time it is called — what a button binds to.

Parameters
msgThe message, copied into the callable.
Returns
The callable; it refers to this Store, which must outlive it.

Definition at line 158 of file store.hpp.


The documentation for this class was generated from the following file: