| 200 | |
| 201 | template <typename ParameterParser, typename ServiceMemFn> |
| 202 | inline void asyncForTiles(const Napi::CallbackInfo &info, |
| 203 | ParameterParser argsToParams, |
| 204 | ServiceMemFn service, |
| 205 | bool requires_multiple_coordinates) |
| 206 | { |
| 207 | auto params = argsToParams(info, requires_multiple_coordinates); |
| 208 | if (!params) |
| 209 | return; |
| 210 | |
| 211 | auto pluginParams = argumentsToPluginParameters(info); |
| 212 | |
| 213 | BOOST_ASSERT(params->IsValid()); |
| 214 | |
| 215 | if (!info[info.Length() - 1].IsFunction()) |
| 216 | return ThrowTypeError(info.Env(), "last argument must be a callback function"); |
| 217 | |
| 218 | auto *const self = Napi::ObjectWrap<Engine>::Unwrap(info.This().As<Napi::Object>()); |
| 219 | using ParamPtr = decltype(params); |
| 220 | |
| 221 | struct Worker final : Napi::AsyncWorker |
| 222 | { |
| 223 | Worker(std::shared_ptr<osrm::OSRM> osrm_, |
| 224 | ParamPtr params_, |
| 225 | ServiceMemFn service, |
| 226 | Napi::Function callback, |
| 227 | PluginParameters pluginParams_) |
| 228 | : Napi::AsyncWorker(callback), osrm{std::move(osrm_)}, service{std::move(service)}, |
| 229 | params{std::move(params_)}, pluginParams{std::move(pluginParams_)} |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | void Execute() override |
| 234 | try |
| 235 | { |
| 236 | result = std::string(); |
| 237 | const auto status = ((*osrm).*(service))(*params, result); |
| 238 | auto str_result = std::get<std::string>(result); |
| 239 | ParseResult(status, str_result); |
| 240 | } |
| 241 | catch (const std::exception &e) |
| 242 | { |
| 243 | SetError(e.what()); |
| 244 | } |
| 245 | |
| 246 | void OnOK() override |
| 247 | { |
| 248 | Napi::HandleScope scope{Env()}; |
| 249 | |
| 250 | Callback().Call({Env().Null(), render(Env(), std::get<std::string>(result))}); |
| 251 | } |
| 252 | |
| 253 | // Keeps the OSRM object alive even after shutdown until we're done with callback |
| 254 | std::shared_ptr<osrm::OSRM> osrm; |
| 255 | ServiceMemFn service; |
| 256 | const ParamPtr params; |
| 257 | const PluginParameters pluginParams; |
| 258 | |
| 259 | osrm::engine::api::ResultT result; |
no test coverage detected