13#include "../../core/bridge.hpp"
14#include "../../core/completion.hpp"
15#include "../../core/executor.hpp"
23namespace morph::forms {
25namespace detail::engine {
29 std::function<std::optional<async::Completion<std::string>>(std::string_view actionType, std::string
const& body)>;
36template <
class Model,
class Sharing>
37[[nodiscard]] JsonRoute routeThrough(bridge::BridgeHandler<Model, Sharing>& handler) {
38 return [&handler](std::string_view actionType,
39 std::string
const& body) -> std::optional<async::Completion<std::string>> {
40 if (!handler.servesAction(actionType)) {
43 return handler.executeJson(actionType, body);
53[[nodiscard]]
inline async::Completion<std::string> routeFirst(std::vector<JsonRoute>
const& routes,
54 exec::IExecutor& callbacks, std::string_view actionType,
55 std::string
const& body) {
56 for (JsonRoute
const& route : routes) {
57 if (std::optional<async::Completion<std::string>> completion = route(actionType, body)) {
58 return std::move(*completion);
62 promise.reject(std::make_exception_ptr(
63 std::invalid_argument{
"no handler serves the action \"" + std::string{actionType} +
"\""}));
64 return std::move(failed);
77template <
class... Handlers>
78 requires(
sizeof...(Handlers) > 0)
80 std::vector<detail::engine::JsonRoute> routes{detail::engine::routeThrough(handlers)...};
81 return [routes = std::move(routes), &callbacks](std::string_view actionType,
const std::string& bodyJson) {
82 return detail::engine::routeFirst(routes, callbacks, actionType, bodyJson);
91template <
class... Handlers>
92 requires(
sizeof...(Handlers) > 0)
94 std::vector<detail::engine::JsonRoute> routes{detail::engine::routeThrough(handlers)...};
95 return [routes = std::move(routes), &callbacks](std::string_view optionsAction,
const std::string& bodyJson) {
96 return detail::engine::routeFirst(routes, callbacks, optionsAction, bodyJson);
static std::pair< Completion< T >, Promise > makeSettleable(::morph::exec::IExecutor *execPtr)
Constructs a Completion<T>/Promise<T> pair sharing one settleable state.
Submitter handlerSubmitter(exec::IExecutor &callbacks, Handlers &... handlers)
A Submitter that sends each action through the first of handlers whose model registers it.
ChoiceFetcher handlerChoiceFetcher(exec::IExecutor &callbacks, Handlers &... handlers)
A ChoiceFetcher that runs each options action through the first of handlers that serves it.
Abstract executor interface.