Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
Initial version of async worker by @daniel-j-h
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarex committed May 19, 2016
1 parent 2599560 commit 3e843cd
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions src/node_osrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ template <class T, class... Types> std::unique_ptr<T> make_unique(Types &&... Ar
{
return (std::unique_ptr<T>(new T(std::forward<Types>(Args)...)));
}

template <typename ResultT> v8::Local<v8::Value> render(const ResultT &result);

template <> v8::Local<v8::Value> render(const std::string &result)
{
return Nan::CopyBuffer(result.data(), result.size()).ToLocalChecked();
}

template <> v8::Local<v8::Value> render(const osrm::json::Object &result)
{
v8::Local<v8::Value> value;
renderToV8(value, result);
return value;
}
}

// Supports
Expand Down Expand Up @@ -726,7 +740,6 @@ struct NearestQueryBaton;
struct TableQueryBaton;
struct TripQueryBaton;
struct TileQueryBaton;
struct MatchQueryBaton;

class Engine final : public Nan::ObjectWrap
{
Expand All @@ -753,7 +766,6 @@ class Engine final : public Nan::ObjectWrap
static void AsyncRunRoute(uv_work_t *);
static void AsyncRunNearest(uv_work_t *);
static void AsyncRunTable(uv_work_t *);
static void AsyncRunMatch(uv_work_t *);
static void AsyncRunTrip(uv_work_t *);
static void AsyncRunTile(uv_work_t *);

Expand All @@ -762,7 +774,6 @@ class Engine final : public Nan::ObjectWrap
static void AfterRunRoute(uv_work_t *);
static void AfterRunNearest(uv_work_t *);
static void AfterRunTable(uv_work_t *);
static void AfterRunMatch(uv_work_t *);
static void AfterRunTrip(uv_work_t *);
static void AfterRunTile(uv_work_t *);

Expand Down Expand Up @@ -857,11 +868,6 @@ struct TileQueryBaton : public RunQueryBaton<std::string>
tile_parameters_ptr params;
};

struct MatchQueryBaton : public RunQueryBaton<osrm::json::Object>
{
match_parameters_ptr params;
};

void Engine::route(const Nan::FunctionCallbackInfo<v8::Value> &args)
{
Nan::HandleScope scope;
Expand All @@ -883,7 +889,37 @@ void Engine::match(const Nan::FunctionCallbackInfo<v8::Value> &args)

BOOST_ASSERT(params->IsValid());

Run<MatchQueryBaton>(args, std::move(params), AsyncRunMatch, AfterRunMatch);
auto *const self = Nan::ObjectWrap::Unwrap<Engine>(args.Holder());

struct MatchWorker final : Nan::AsyncWorker {
using Base = Nan::AsyncWorker;

MatchWorker(osrm::OSRM& osrm_, match_parameters_ptr params_, Nan::Callback* callback)
: Base(callback), osrm{osrm_}, params{std::move(params_)} {}

void Execute() override try {
const auto status = osrm.Match(*params, result);
ParseResult(status, result);
} catch (const std::exception& e) {
SetErrorMessage(e.what());
}

void HandleOKCallback() override {
Nan::HandleScope scope;

const constexpr auto argc = 2u;
v8::Local<v8::Value> argv[argc] = {Nan::Null(), render(result)};

callback->Call(argc, argv);
}

osrm::OSRM& osrm;
const match_parameters_ptr params;
osrm::json::Object result;
};

auto *callback = new Nan::Callback{args[args.Length() - 1].As<v8::Function>()};
Nan::AsyncQueueWorker(new MatchWorker{*self->this_, std::move(params), callback});
}

void Engine::trip(const Nan::FunctionCallbackInfo<v8::Value> &args)
Expand Down Expand Up @@ -1058,35 +1094,6 @@ void Engine::AsyncRunTile(uv_work_t *req)
}
}

void Engine::AsyncRunMatch(uv_work_t *req)
{
MatchQueryBaton *closure = static_cast<MatchQueryBaton *>(req->data);
try
{
const auto status = closure->machine->this_->Match(*closure->params, closure->result);

ParseResult(status, closure->result);
}
catch (std::exception const &ex)
{
closure->error = ex.what();
}
}

template <typename ResultT> v8::Local<v8::Value> render(const ResultT &result);

template <> v8::Local<v8::Value> render(const std::string &result)
{
return Nan::CopyBuffer(result.data(), result.size()).ToLocalChecked();
}

template <> v8::Local<v8::Value> render(const osrm::json::Object &result)
{
v8::Local<v8::Value> value;
renderToV8(value, result);
return value;
}

template <typename BatonType> void Engine::AfterRun(BatonType *closure)
{
Nan::TryCatch try_catch;
Expand Down Expand Up @@ -1145,13 +1152,6 @@ void Engine::AfterRunTile(uv_work_t *req)
AfterRun(closure);
}

void Engine::AfterRunMatch(uv_work_t *req)
{
Nan::HandleScope scope;
MatchQueryBaton *closure = static_cast<MatchQueryBaton *>(req->data);
AfterRun(closure);
}

extern "C" {
static void start(v8::Handle<v8::Object> target) { Engine::Initialize(target); }
}
Expand Down

0 comments on commit 3e843cd

Please sign in to comment.