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::move(func)](
Nui::val const& args)
mutable {
48 template <
typename ReturnType,
typename ArgType>
51 template <
typename FunctionT>
54 return [func = std::move(func)](
Nui::val const& arg)
mutable {
55 func(extractMember<ArgType>(arg));
60 template <
typename ReturnType,
typename... ArgsTypes, std::size_t... Is>
61 struct FunctionWrapperImpl<ReturnType, std::tuple<ArgsTypes...>, std::index_sequence<Is...>>
63 template <
typename FunctionT>
66 return [func = std::move(func)](
Nui::val const& args)
mutable {
67 func(extractMember<ArgsTypes>(args[Is])...);
72 template <
typename ReturnType,
typename ArgsTypes>
73 struct FunctionWrapperImpl2
76 template <
typename ReturnType,
typename... ArgsTypes>
77 struct FunctionWrapperImpl2<ReturnType, std::tuple<ArgsTypes...>>
78 :
public FunctionWrapperImpl<ReturnType, std::tuple<ArgsTypes...>, std::index_sequence_for<ArgsTypes...>>
81 template <
typename FunctionT>
82 struct FunctionWrapper
83 :
public FunctionWrapperImpl2<
84 FunctionReturnType_t<std::decay_t<FunctionT>>,
85 FunctionArgumentTypes_t<std::decay_t<FunctionT>>>
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())
111 using namespace std::string_literals;
114 Console::error(
"Remote callable with name '"s + name_ +
"' is undefined");
115 return Nui::val::undefined();
117 if (backChannel_.empty())
118 return callable_(
val);
128 explicit operator bool()
const
134 : name_{std::move(name)}
136 , callable_{
Nui::
val::undefined()}
141 : name_{std::move(name)}
142 , backChannel_{std::move(backChannel)}
143 , callable_{
Nui::
val::undefined()}
150 using namespace std::string_literals;
154 const auto rpcObject = Nui::val::global(
"nui_rpc");
155 if (rpcObject.isUndefined())
158 callable_ = Nui::val::global(
"nui_rpc")[
"backend"][name_.c_str()];
159 isSet_ = !callable_.isUndefined();
165 std::string backChannel_;
188 template <
typename... ArgsT>
189 static auto call(std::string name, ArgsT&&... args)
202 template <
typename FunctionT,
typename... ArgsT>
206 std::forward<ArgsT>(args)...);
212 template <
typename FunctionT>
224 template <
typename FunctionT>
227 using namespace std::string_literals;
228 if (Nui::val::global(
"nui_rpc").isUndefined())
233 auto tempId = Nui::val::global(
"nui_rpc")[
"tempId"].as<uint32_t>() + 1;
234 Nui::val::global(
"nui_rpc").set(
"tempId", tempId);
235 const auto tempIdString =
"temp_"s + std::to_string(tempId);
236 Nui::val::global(
"nui_rpc")[
"frontend"].set(
240 tempIdString](
Nui::val param)
mutable {
242 Nui::val::global(
"nui_rpc")[
"frontend"].delete_(tempIdString);
244 std::placeholders::_1));
254 template <
typename FunctionT>
257 using namespace std::string_literals;
258 if (Nui::val::global(
"nui_rpc").isUndefined())
263 Nui::val::global(
"nui_rpc")[
"frontend"].set(
270 std::placeholders::_1));
280 using namespace std::string_literals;
281 if (Nui::val::global(
"nui_rpc").isUndefined())
286 Nui::val::global(
"nui_rpc")[
"frontend"].delete_(name.c_str());
318 template <
typename FunctionT>
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:133
auto operator()(Args &&... args) const
Definition: rpc_client.hpp:96
RemoteCallable(std::string name, std::string backChannel)
Definition: rpc_client.hpp:140
bool isValid() const
Definition: rpc_client.hpp:123
auto operator()(Nui::val val) const
Definition: rpc_client.hpp:109
Definition: rpc_client.hpp:90
static void unregisterFunction(std::string const &name)
Unregister a function.
Definition: rpc_client.hpp:278
static void registerFunction(std::string const &name, FunctionT &&func)
Register a permanent function that is callable from the backend.
Definition: rpc_client.hpp:255
static auto call(std::string name, ArgsT &&... args)
Get a callable remote function and call it immediately.
Definition: rpc_client.hpp:189
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:213
static AutoUnregister autoRegisterFunction(std::string const &name, FunctionT &&func)
Definition: rpc_client.hpp:319
static std::string registerFunctionOnce(FunctionT &&func)
Registers a single shot function that is removed after it was called.
Definition: rpc_client.hpp:225
static auto getRemoteCallable(std::string name)
Get a callable remote function.
Definition: rpc_client.hpp:176
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:203
constexpr static auto extractMember(Nui::val const &val) -> decltype(auto)
Definition: rpc_client.hpp:24
Definition: file_dialog.hpp:6
requires(!std::is_union_v< T >) void convertFromValObjImpl(Nui requires(!std::is_union_v< T >) void convertFromVal(Nui requires(!std::is_union_v< T > &&!boost::describe::has_describe_bases< T >::value) void convertFromVal(Nui requires Fundamental< T > void convertFromVal(Nui::val const &val, T &value)
Definition: val_conversion.hpp:262
emscripten::val bind(F &&f, Args &&... args)
Equivalent of std::bind returning a javascript functor.
Definition: functions.hpp:98
void convertToVal(Nui::val &val, T const &obj)
Definition: val_conversion.hpp:109
emscripten::val val
Definition: val.hpp:5
constexpr static auto wrapFunction(FunctionT &&func)
Definition: rpc_client.hpp:52
constexpr static auto wrapFunction(FunctionT &&func)
Definition: rpc_client.hpp:64
constexpr static auto wrapFunction(FunctionT &&func)
Definition: rpc_client.hpp:40
Definition: rpc_hub.hpp:26
Definition: rpc_hub.hpp:75
Definition: rpc_client.hpp:290
AutoUnregister(std::string name)
Definition: rpc_client.hpp:294
void reset()
Definition: rpc_client.hpp:312
AutoUnregister(AutoUnregister const &)=delete
AutoUnregister()
Definition: rpc_client.hpp:291
AutoUnregister(AutoUnregister &&other)
Definition: rpc_client.hpp:302
AutoUnregister & operator=(AutoUnregister const &)=delete
AutoUnregister & operator=(AutoUnregister &&other)
Definition: rpc_client.hpp:306
~AutoUnregister()=default