8#include <traits/functions.hpp>
9#include <nlohmann/json.hpp>
10#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::forward<FunctionT>(func)](nlohmann::json
const& args)
mutable {
49 template <
typename ReturnType,
typename... ArgsTypes, std::size_t... Is>
52 template <
typename FunctionT>
55 return [func = std::forward<FunctionT>(func)](nlohmann::json
const& args)
mutable {
61 template <
typename ReturnType,
typename ArgsTypes>
67 :
public FunctionWrapperImpl<ReturnType, std::tuple<ArgsTypes...>, std::index_sequence_for<ArgsTypes...>>
70 template <
typename FunctionT>
73 typename Traits::FunctionTraits<std::decay_t<FunctionT>>::ReturnType,
74 typename Traits::FunctionTraits<std::decay_t<FunctionT>>::ArgsTuple>
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 json = std::forward<Arg>(arg);
157 callRemoteImpl(name, json);
159 void callRemote(std::string
const& name, nlohmann::json
const& json)
const
161 callRemoteImpl(name, json);
165 void callRemote(std::string
const& name, nlohmann::json&& json)
const
167 callRemoteImpl(name, json);
171 callRemoteImpl(name);
175 template <
typename... Args>
176 void call(std::string
const& name, Args&&... args)
const
178 callRemote(name, std::forward<Args>(args)...);
184 void enableFileDialogs()
const;
194 void enableWindowFunctions()
const;
199 void enableFetch()
const;
204 void enableThrottle();
219 void enableEnvironmentVariables();
226 template <
typename ManagerT>
229 std::scoped_lock lock{guard_};
230 auto iter = stateStores_.find(
id);
231 if (iter == stateStores_.end())
236 std::unique_ptr<void, std::function<void(
void*)>>{
239 ManagerT::destroy(ptr);
241 .first->second.get();
243 return iter->second.get();
247 void callRemoteImpl(std::string
const& name, nlohmann::json
const& json)
const
250 window_->eval(fmt::format(remoteCallScript, name, json.dump()));
252 void callRemoteImpl(std::string
const& name)
const
255 window_->eval(fmt::format(remoteCallScript, name,
"undefined"));
259 std::recursive_mutex guard_;
261 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:169
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:165
static constexpr char const * remoteCallScript
Definition rpc_hub.hpp:88
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
RpcHub & operator=(const RpcHub &)=delete
void registerFunction(std::string const &name, T &&func) const
Definition rpc_hub.hpp:120
void * accessStateStore(std::string const &id)
Definition rpc_hub.hpp:227
void call(std::string const &name, Args &&... args) const
Definition rpc_hub.hpp:176
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
Window & window() const
Returns the attached window.
Definition rpc_hub.hpp:136
RpcHub & operator=(RpcHub &&)=delete
This class encapsulates the webview.
Definition window.hpp:133
static constexpr auto extractJsonMember(nlohmann::json const &json) -> decltype(auto)
Definition rpc_hub.hpp:29
Definition file_dialog.hpp:6
Definition rpc_hub.hpp:63
static constexpr auto wrapFunction(FunctionT &&func)
Definition rpc_hub.hpp:53
static constexpr 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