|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
RAII wrapper that binds a single model type to a Bridge. More...
#include <bridge.hpp>
Public Member Functions | |
| BridgeHandler (Bridge &bridge, ::morph::exec::IExecutor *guiExec) | |
| Constructs and registers the handler using the default model factory. | |
| BridgeHandler (Bridge &bridge, ::morph::exec::IExecutor *guiExec, std::shared_ptr< detail::HandlerBinding > binding) | |
| Constructs the handler with a pre-built binding (for dependency injection). | |
| ~BridgeHandler () | |
| Deregisters the binding from the bridge, on the owner. | |
| BridgeHandler (const BridgeHandler &)=delete | |
| BridgeHandler & | operator= (const BridgeHandler &)=delete |
| template<typename Action> | |
| ::morph::async::Completion< typename ::morph::model::ActionTraits< Action >::Result > | execute (Action action) |
Dispatches action via the underlying Bridge and returns a Completion. | |
| template<typename Action> | |
| ::morph::async::Completion< typename ::morph::model::ActionTraits< Action >::Result > | execute (Action action, ::core::async::StopToken stop) |
Dispatches action as execute(action) does, cancellable through stop. | |
| template<typename M = Model> requires kShared | |
| void | attach (const ::morph::model::PrimaryKeyOf< M > &key) |
Attaches (or re-points) this handler to the instance for key. | |
| template<typename M = Model> requires kShared | |
| std::optional<::morph::model::PrimaryKeyOf< M > > | primary () |
| This handler's current primary key, or nullopt if unattached. | |
| template<typename M = Model> requires kShared | |
| ::morph::async::Completion< std::vector<::morph::model::PrimaryKeyOf< M > > > | instances () |
| Snapshot of the live shared instance keys for Model. | |
| ::morph::async::Completion< std::string > | executeJson (std::string_view actionType, std::string_view bodyJson) |
| Type-erased execute: looks up the action by its registered string id and dispatches it through ActionExecuteRegistry. | |
| bool | servesAction (std::string_view actionId) const noexcept |
Whether Model has an action registered under actionId, without invoking it. | |
| ::morph::exec::IExecutor * | guiExecutor () const noexcept |
| The executor used to deliver this handler's Completion callbacks. | |
| ::morph::exec::IExecutor & | owner () const noexcept |
| The executor this handler and its bridge belong to. | |
| bool | onOwner () const noexcept |
| Whether the calling thread is on this handler's owner. | |
| bool | isBound () const noexcept |
| Whether this handler currently has a live backend instance. | |
| template<typename R> | |
| void | subscribe (std::function< void(R)> cb) |
Subscribes to results of type R produced on the attached instance. | |
| template<typename R> | |
| void | subscribe (const ::morph::async::CallbackScope &scope, std::function< void(R)> cb) |
Subscribes to results of type R, gated on scope. | |
| template<typename R> | |
| void | subscribe (::morph::async::CallbackToken token, std::function< void(R)> cb) |
Subscribes to results of type R, gated on an already-issued token. | |
| template<typename R> | |
| void | unsubscribe () |
Removes this handler's subscription for R. | |
| const std::shared_ptr< detail::HandlerBinding > & | binding () const |
| Returns the underlying HandlerBinding. | |
Static Public Attributes | |
| static constexpr bool | kShared = std::is_same_v<Sharing, AllowShared> |
| Whether this handler participates in the shared instance directory. | |
RAII wrapper that binds a single model type to a Bridge.
On construction, registers a HandlerBinding on the bridge. On destruction, deregisters it. The handler is non-copyable, and belongs to its bridge's owner: it is used and destroyed there.
| Model | Concrete model type. |
| Sharing | NoSharing (the default) or AllowShared. |
Definition at line 2583 of file bridge.hpp.
|
inline |
Constructs and registers the handler using the default model factory.
On the bridge's owner the bind is issued here; a backend that settles it before returning has bound the handler when this returns. Anywhere else — inside a running action on a pool thread — the registration is posted to the owner. Either way nothing waits: a call made before the bind settles is held and dispatched when it does.
| bridge | The bridge to register on. Borrowed: it must outlive every call made on this handler. |
| guiExec | Executor used to deliver Completion callbacks: the bridge's owner, or an executor that runs its tasks on the owner's thread (a debug build checks it once). Borrowed: it must outlive this handler. |
Definition at line 2602 of file bridge.hpp.
|
inline |
Constructs the handler with a pre-built binding (for dependency injection).
Registers binding exactly as the constructor above registers its own.
| bridge | The bridge to register on. Borrowed, on the same terms as the constructor above. |
| guiExec | Executor for callback delivery, on the same terms as the constructor above. |
| binding | Pre-built binding whose factory captures injected dependencies. Its contextKey must be set before this call. |
Definition at line 2617 of file bridge.hpp.
|
inline |
Deregisters the binding from the bridge, on the owner.
Every call still held for a bind in flight is rejected with HandlerDestroyedError. Destroyed after its bridge — still on the owner — it finds the bridge's token expired and deregisters nothing; on one thread that check is exact.
Definition at line 2640 of file bridge.hpp.
|
inline |
Dispatches action via the underlying Bridge and returns a Completion.
The bridge's currently-installed default session is attached automatically. A call made before the handler's bind has settled is held and dispatched when it does; a failed bind rejects it with the bind's error.
| Action | Concrete action type registered with BRIDGE_REGISTER_ACTION. |
| action | Action to execute (moved into the dispatch). |
Definition at line 2667 of file bridge.hpp.
|
inline |
Dispatches action as execute(action) does, cancellable through stop.
A stop on stop before the call settles cancels it: the returned Completion is rejected with core::async::OperationCancelled, posted to the GUI executor from the thread that requested the stop, and the call's stop source is asked to stop. A handler returning a Task, on a LocalBackend, sees that stop at its next stop-aware co_await; a synchronous handler runs to its end, and a remote server's handler runs on, since no cancel crosses the wire. Either way the call's own outcome, later, reaches nobody. A token already stopped rejects the call the same way without dispatching it.
The call stays counted as pending until the backend settles it, as a call the execute deadline settled does.
| Action | Concrete action type registered with BRIDGE_REGISTER_ACTION. |
| action | Action to execute (moved into the dispatch). |
| stop | The caller's cancel. Observed until the call settles; a token that can never be stopped makes this execute(action). |
stop is stopped. Definition at line 2716 of file bridge.hpp.
|
inline |
Attaches (or re-points) this handler to the instance for key.
Creates the instance if no live instance holds key, otherwise joins the existing one. Re-pointing an already-attached handler releases the old instance, which survives only if another handler still holds it. A backend that settles before returning has attached the handler by the time this returns; otherwise calls made meanwhile are held until it has. A refused attach is logged and leaves the handler on the instance it held; a call held behind it is rejected with its error only when the handler has no instance to fall back to. A key that names no instance — an empty string, a strong id with no value — is refused the same way: a call through a handler with no instance is then rejected with std::invalid_argument or std::runtime_error respectively.
| M | Defaulted to Model; never named explicitly. Present only so the signature is instantiated lazily, since PrimaryKeyOf is ill-formed for an unkeyed model. |
| key | Primary key of the instance to attach to. |
Definition at line 2750 of file bridge.hpp.
|
inlinenodiscard |
This handler's current primary key, or nullopt if unattached.
| M | Defaulted to Model; never named explicitly. See attach. |
Definition at line 2767 of file bridge.hpp.
|
inlinenodiscard |
Snapshot of the live shared instance keys for Model.
Asynchronous even in local mode: the directory is backend state, and in remote mode answering costs a round trip. The result is a snapshot, stale the moment it arrives.
| M | Defaulted to Model; never named explicitly. See attach. |
Definition at line 2786 of file bridge.hpp.
|
inlinenodiscard |
Type-erased execute: looks up the action by its registered string id and dispatches it through ActionExecuteRegistry.
Use this only when the concrete Action type is not known at the call site (e.g. a schema-driven UI reading action names out of a JSON Schema at runtime). Prefer the templated execute<Action>() whenever the type is known at compile time.
| actionType | Registered action type-id (the NAME passed to BRIDGE_REGISTER_ACTION). |
| bodyJson | JSON-encoded action body. |
| std::runtime_error | if actionType was never registered for Model. |
Definition at line 2824 of file bridge.hpp.
|
inlinenodiscardnoexcept |
Whether Model has an action registered under actionId, without invoking it.
A pure existence check over ActionExecuteRegistry – the same registry executeJson dispatches through.
| actionId | Action type id to check. |
Definition at line 2842 of file bridge.hpp.
|
inlinenodiscardnoexcept |
The executor used to deliver this handler's Completion callbacks.
Definition at line 2849 of file bridge.hpp.
|
inlinenodiscardnoexcept |
The executor this handler and its bridge belong to.
Definition at line 2853 of file bridge.hpp.
|
inlinenodiscardnoexcept |
Whether the calling thread is on this handler's owner.
Definition at line 2858 of file bridge.hpp.
|
inlinenodiscardnoexcept |
Whether this handler currently has a live backend instance.
A point-in-time atomic read, safe from any thread. Nothing needs to wait on it: a call made while the handler's bind is in flight is held until the bind settles.
Definition at line 2866 of file bridge.hpp.
|
inline |
Subscribes to results of type R produced on the attached instance.
Fires whenever an R is produced on the instance this handler is attached to by a typed call — this handler's, another handler's sharing the instance, or another screen's. A RawHandler's call on the same instance notifies nobody: its result is JSON text, not an R.
One callback per (handler, R): subscribing again replaces the previous one. Callbacks are delivered on this handler's executor. Failed actions notify nobody; delivery is best-effort and unbuffered, with no replay.
| R | Result/state type to observe. |
| cb | Callable receiving the value by value on the GUI executor. |
Definition at line 2882 of file bridge.hpp.
|
inline |
Subscribes to results of type R, gated on scope.
Same subscription as subscribe<R>(cb), except that cb runs only while scope is alive and un-stopped. The sink itself is not pruned when the scope goes inactive: delivery is refused, the subscription entry stays until unsubscribe<R>() or handler destruction removes it.
| R | Result/state type to observe. |
| scope | Receiver-owned gate; observed weakly, and only its current generation is captured (a later reset() retires this sink). |
| cb | Callable receiving the value by value on the GUI executor. |
Definition at line 2900 of file bridge.hpp.
|
inline |
Subscribes to results of type R, gated on an already-issued token.
The token-taking form of subscribe<R>(const CallbackScope&, cb).
| R | Result/state type to observe. |
| token | Gate observing some receiver's CallbackScope. A default-constructed token suppresses every delivery. |
| cb | Callable receiving the value by value on the GUI executor. |
Definition at line 2913 of file bridge.hpp.
|
inline |
Removes this handler's subscription for R.
| R | Result/state type to stop hearing about. |
Definition at line 2920 of file bridge.hpp.
|
inlinenodiscard |
Returns the underlying HandlerBinding.
Definition at line 2929 of file bridge.hpp.
|
staticconstexpr |
Whether this handler participates in the shared instance directory.
Definition at line 2586 of file bridge.hpp.