8 #include <nlohmann/json.hpp>
9 #include <fmt/format.h>
17 #include <unordered_map>
24 template <
typename ReturnType,
typename ArgsTypes,
typename IndexSeq>
28 template <
typename ArgT>
31 if constexpr (std::is_same_v<std::decay_t<ArgT>, nlohmann::json>)
34 return json.get<std::decay_t<ArgT>>();
37 template <
typename ReturnType>
40 template <
typename FunctionT>
43 return [func = std::move(func)](nlohmann::json
const& args)
mutable {
49 template <
typename ReturnType,
typename... ArgsTypes, std::size_t... Is>
52 template <
typename FunctionT>
55 return [func = std::move(func)](nlohmann::json
const& args)
mutable {
56 func(extractJsonMember<ArgsTypes>(args[Is])...);
61 template <
typename ReturnType,
typename ArgsTypes>
65 template <
typename ReturnType,
typename... ArgsTypes>
67 :
public FunctionWrapperImpl<ReturnType, std::tuple<ArgsTypes...>, std::index_sequence_for<ArgsTypes...>>
70 template <
typename FunctionT>
73 FunctionReturnType_t<std::decay_t<FunctionT>>,
74 FunctionArgumentTypes_t<std::decay_t<FunctionT>>>
90 if (globalThis.nui_rpc.frontend.hasOwnProperty("{0}")) {{
91 globalThis.nui_rpc.frontend["{0}"]({1});
95 globalThis.nui_rpc.errors = globalThis.nui_rpc.errors || [];
96 globalThis.nui_rpc.errors.push("Function {0} does not exist.");
97 if (globalThis.nui_rpc.errors.length > 100) {{
98 globalThis.nui_rpc.errors.shift();
106 :
OnDestroy{[hub, name = std::move(name)]() {
112 template <
typename T>
115 registerFunction(name, std::forward<T>(func));
119 template <
typename T>
128 window_->unbind(name);
148 template <
typename... Args>
149 void callRemote(std::string
const& name, Args&&... args)
const
151 callRemoteImpl(name, nlohmann::json{std::forward<Args>(args)...});
153 template <
typename Arg>
156 nlohmann::json j = arg;
157 callRemoteImpl(name, std::move(j));
159 void callRemote(std::string
const& name, nlohmann::json
const& json)
const
161 callRemoteImpl(name, json);
163 void callRemote(std::string
const& name, nlohmann::json&& json)
const
165 callRemoteImpl(name, json);
169 callRemoteImpl(name);
173 template <
typename... Args>
174 void call(std::string
const& name, Args&&... args)
const
176 callRemote(name, std::forward<Args>(args)...);
182 void enableFileDialogs()
const;
192 void enableWindowFunctions()
const;
197 void enableFetch()
const;
202 void enableThrottle();
217 void enableEnvironmentVariables();
224 template <
typename ManagerT>
227 std::scoped_lock lock{guard_};
228 auto iter = stateStores_.find(
id);
229 if (iter == stateStores_.end())
234 std::unique_ptr<void, std::function<void(
void*)>>{
237 ManagerT::destroy(ptr);
239 .first->second.get();
243 return iter->second.get();
248 void callRemoteImpl(std::string
const& name, nlohmann::json
const& json)
const
251 window_->eval(fmt::format(remoteCallScript, name, json.dump()));
253 void callRemoteImpl(std::string
const& name)
const
256 window_->eval(fmt::format(remoteCallScript, name,
"undefined"));
260 std::recursive_mutex guard_;
262 std::unordered_map<std::string, std::unique_ptr<void, std::function<void(
void*)>>> stateStores_;
Definition: on_destroy.hpp:8
Definition: rpc_hub.hpp:79
RpcHub(const RpcHub &)=delete
void callRemote(std::string const &name) const
Definition: rpc_hub.hpp:167
void callRemote(std::string const &name, Args &&... args) const
For technical reasons these cannot return a value currently.
Definition: rpc_hub.hpp:149
void callRemote(std::string const &name, nlohmann::json &&json) const
Definition: rpc_hub.hpp:163
RpcHub & operator=(const RpcHub &)=delete
void callRemote(std::string const &name, nlohmann::json const &json) const
Definition: rpc_hub.hpp:159
void callRemote(std::string const &name, Arg &&arg) const
Definition: rpc_hub.hpp:154
void * accessStateStore(std::string const &id)
Definition: rpc_hub.hpp:225
constexpr static char const * remoteCallScript
Definition: rpc_hub.hpp:88
RpcHub & operator=(RpcHub &&)=delete
void registerFunction(std::string const &name, T &&func) const
Definition: rpc_hub.hpp:120
void call(std::string const &name, Args &&... args) const
Definition: rpc_hub.hpp:174
AutoUnregister autoRegisterFunction(std::string const &name, T &&func) const
Definition: rpc_hub.hpp:113
void unregisterFunction(std::string const &name) const
Definition: rpc_hub.hpp:125
RpcHub(Window &window)
Definition: rpc_hub.cpp:14
Window & window() const
Returns the attached window.
Definition: rpc_hub.hpp:136
This class encapsulates the webview.
Definition: window.hpp:133
constexpr static auto extractJsonMember(nlohmann::json const &json) -> decltype(auto)
Definition: rpc_hub.hpp:29
Definition: file_dialog.hpp:6
Definition: rpc_hub.hpp:63
constexpr static auto wrapFunction(FunctionT &&func)
Definition: rpc_hub.hpp:53
constexpr static auto wrapFunction(FunctionT &&func)
Definition: rpc_hub.hpp:41
Definition: rpc_hub.hpp:26
Definition: rpc_hub.hpp:75
Definition: rpc_hub.hpp:104
AutoUnregister(RpcHub const *hub, std::string name)
Definition: rpc_hub.hpp:105