MCPcopy Create free account
hub / github.com/Project-OSRM/osrm-backend / async

Function async

src/nodejs/node_osrm.cpp:104–199  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102
103template <typename ParameterParser, typename ServiceMemFn>
104inline void async(const Napi::CallbackInfo &info,
105 ParameterParser argsToParams,
106 ServiceMemFn service,
107 bool requires_multiple_coordinates)
108{
109 auto params = argsToParams(info, requires_multiple_coordinates);
110 if (!params)
111 return;
112 auto pluginParams = argumentsToPluginParameters(info, params->format);
113
114 BOOST_ASSERT(params->IsValid());
115
116 if (!info[info.Length() - 1].IsFunction())
117 return ThrowTypeError(info.Env(), "last argument must be a callback function");
118
119 auto *const self = Napi::ObjectWrap<Engine>::Unwrap(info.This().As<Napi::Object>());
120 using ParamPtr = decltype(params);
121
122 struct Worker final : Napi::AsyncWorker
123 {
124 Worker(std::shared_ptr<osrm::OSRM> osrm_,
125 ParamPtr params_,
126 ServiceMemFn service,
127 Napi::Function callback,
128 PluginParameters pluginParams_)
129 : Napi::AsyncWorker(callback), osrm{std::move(osrm_)}, service{std::move(service)},
130 params{std::move(params_)}, pluginParams{std::move(pluginParams_)}
131 {
132 }
133
134 void Execute() override
135 try
136 {
137 switch (
138 params->format.value_or(osrm::engine::api::BaseParameters::OutputFormatType::JSON))
139 {
140 case osrm::engine::api::BaseParameters::OutputFormatType::JSON:
141 {
142 osrm::engine::api::ResultT r;
143 r = osrm::util::json::Object();
144 const auto status = ((*osrm).*(service))(*params, r);
145 auto &json_result = std::get<osrm::json::Object>(r);
146 ParseResult(status, json_result);
147 if (pluginParams.renderToBuffer)
148 {
149 std::string json_string;
150 osrm::util::json::render(json_string, json_result);
151 result = std::move(json_string);
152 }
153 else
154 {
155 result = std::move(json_result);
156 }
157 }
158 break;
159 case osrm::engine::api::BaseParameters::OutputFormatType::FLATBUFFERS:
160 {
161 osrm::engine::api::ResultT r = flatbuffers::FlatBufferBuilder();

Callers 5

routeMethod · 0.85
nearestMethod · 0.85
tableMethod · 0.85
matchMethod · 0.85
tripMethod · 0.85

Calls 3

ThrowTypeErrorFunction · 0.85
IsValidMethod · 0.45

Tested by

no test coverage detected