5#include <traits/functions.hpp>
19 template <
typename ReturnType,
typename ArgsTypes,
typename IndexSeq>
20 struct FunctionWrapperImpl
23 template <
typename ArgT>
26 if constexpr (std::is_same_v<std::decay_t<ArgT>,
Nui::val>)
30 std::decay_t<ArgT> value;
36 template <
typename ReturnType>
39 template <
typename FunctionT>
42 return [
func = std::forward<FunctionT>(
func)](
Nui::val const& args)
mutable {
48 template <
typename ReturnType,
typename ArgType>
51 template <
typename FunctionT>
61 struct FunctionWrapperImpl<
ReturnType, std::
tuple<ArgsTypes...>, std::index_sequence<Is...>>
63 template <
typename FunctionT>
66 return [
func = std::forward<FunctionT>(
func)](
Nui::val const& args)
mutable {
72 template <
typename ReturnType,
typename ArgsTypes>
73 struct FunctionWrapperImpl2
78 :
public FunctionWrapperImpl<ReturnType, std::tuple<ArgsTypes...>, std::index_sequence_for<ArgsTypes...>>
81 template <
typename FunctionT>
82 struct FunctionWrapper
83 :
public FunctionWrapperImpl2<
84 typename Traits::FunctionTraits<std::decay_t<FunctionT>>::ReturnType,
85 typename Traits::FunctionTraits<std::decay_t<FunctionT>>::ArgsTuple>
95 template <
typename... Args>
98 using namespace std::string_literals;
101 Console::error(
"Remote callable with name '"s + name_ +
"' is undefined");
102 return Nui::val::undefined();
104 if (backChannel_.empty())
110 using namespace std::string_literals;
113 Console::error(
"Remote callable with name '"s + name_ +
"' is undefined");
114 return Nui::val::undefined();
116 if (backChannel_.empty())
117 return callable_(
val);
126 explicit operator bool()
const
132 : name_{std::move(name)}
134 , callable_{
Nui::
val::undefined()}
139 : name_{std::move(name)}
140 , backChannel_{std::move(backChannel)}
141 , callable_{
Nui::
val::undefined()}
148 using namespace std::string_literals;
152 const auto rpcObject = Nui::val::global(
"nui_rpc");
153 if (rpcObject.isUndefined())
156 callable_ = Nui::val::global(
"nui_rpc")[
"backend"][name_.c_str()];
157 isSet_ = !callable_.isUndefined();
163 std::string backChannel_;
186 template <
typename... ArgsT>
187 static auto call(std::string name, ArgsT&&... args)
200 template <
typename FunctionT,
typename... ArgsT>
204 std::forward<ArgsT>(args)...);
210 template <
typename FunctionT>
222 template <
typename FunctionT>
225 using namespace std::string_literals;
226 if (Nui::val::global(
"nui_rpc").isUndefined())
231 auto tempId = Nui::val::global(
"nui_rpc")[
"tempId"].as<uint32_t>() + 1;
232 Nui::val::global(
"nui_rpc").set(
"tempId", tempId);
233 const auto tempIdString =
"temp_"s + std::to_string(tempId);
234 Nui::val::global(
"nui_rpc")[
"frontend"].set(
238 tempIdString](
Nui::val param)
mutable {
240 Nui::val::global(
"nui_rpc")[
"frontend"].delete_(tempIdString);
242 std::placeholders::_1));
252 template <
typename FunctionT>
255 using namespace std::string_literals;
256 if (Nui::val::global(
"nui_rpc").isUndefined())
261 Nui::val::global(
"nui_rpc")[
"frontend"].set(
268 std::placeholders::_1));
278 using namespace std::string_literals;
279 if (Nui::val::global(
"nui_rpc").isUndefined())
284 Nui::val::global(
"nui_rpc")[
"frontend"].delete_(name.c_str());
316 template <
typename FunctionT>
319 registerFunction(name, std::forward<FunctionT>(func));
static void error(Args &&... args)
Definition console.hpp:18
Definition on_destroy.hpp:8
OnDestroy & operator=(OnDestroy const &)=delete
Definition rpc_client.hpp:93
RemoteCallable(std::string name)
Definition rpc_client.hpp:131
auto operator()(Args &&... args) const
Definition rpc_client.hpp:96
RemoteCallable(std::string name, std::string backChannel)
Definition rpc_client.hpp:138
bool isValid() const
Definition rpc_client.hpp:121
auto operator()(Nui::val val) const
Definition rpc_client.hpp:108
Definition rpc_client.hpp:90
static void unregisterFunction(std::string const &name)
Unregister a function.
Definition rpc_client.hpp:276
static void registerFunction(std::string const &name, FunctionT &&func)
Register a permanent function that is callable from the backend.
Definition rpc_client.hpp:253
static auto call(std::string name, ArgsT &&... args)
Get a callable remote function and call it immediately.
Definition rpc_client.hpp:187
static auto getRemoteCallableWithBackChannel(std::string name, FunctionT &&func)
Get a callable remote function and register a temporary callable for a response.
Definition rpc_client.hpp:211
static AutoUnregister autoRegisterFunction(std::string const &name, FunctionT &&func)
Definition rpc_client.hpp:317
static std::string registerFunctionOnce(FunctionT &&func)
Registers a single shot function that is removed after it was called.
Definition rpc_client.hpp:223
static auto getRemoteCallable(std::string name)
Get a callable remote function.
Definition rpc_client.hpp:174
static auto callWithBackChannel(std::string name, FunctionT &&cb, ArgsT &&... args)
Get a callable remote function and call it immediately with a callback.
Definition rpc_client.hpp:201
static constexpr auto extractJsonMember(nlohmann::json const &json) -> decltype(auto)
Definition rpc_hub.hpp:29
static constexpr auto extractMember(Nui::val const &val) -> decltype(auto)
Definition rpc_client.hpp:24
Definition file_dialog.hpp:6
emscripten::val bind(F &&f, Args &&... args)
Equivalent of std::bind returning a javascript functor.
Definition functions.hpp:98
void convertFromVal(Nui::val const &val, T &obj)
Definition val_conversion.hpp:229
void convertToVal(Nui::val &val, T const &obj)
Definition val_conversion.hpp:109
emscripten::val val
Definition val.hpp:5
static constexpr auto wrapFunction(FunctionT &&func)
Definition rpc_client.hpp:52
static constexpr auto wrapFunction(FunctionT &&func)
Definition rpc_client.hpp:64
static constexpr auto wrapFunction(FunctionT &&func)
Definition rpc_client.hpp:40
Definition rpc_hub.hpp:26
Definition rpc_hub.hpp:75
Definition rpc_client.hpp:288
AutoUnregister & operator=(AutoUnregister &&other) noexcept
Definition rpc_client.hpp:304
AutoUnregister & operator=(AutoUnregister const &)=delete
AutoUnregister(std::string name)
Definition rpc_client.hpp:292
void reset()
Definition rpc_client.hpp:310
AutoUnregister(AutoUnregister &&other) noexcept
Definition rpc_client.hpp:300
AutoUnregister(AutoUnregister const &)=delete
AutoUnregister()
Definition rpc_client.hpp:289
~AutoUnregister()=default