Skip to content

Commit

Permalink
Merge pull request #498 from Zireael-N/async_context
Browse files Browse the repository at this point in the history
Pass async_context to node::MakeCallback()
  • Loading branch information
kjvalencik authored Mar 16, 2020
2 parents a66dbf0 + d9c9b19 commit 20df99b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/neon-sys/native/src/neon_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define NEON_TASK_H_

#include <uv.h>
#include <nan_new.h>
#include <node_version.h>
#include "neon.h"
#include "v8.h"

Expand All @@ -21,12 +23,23 @@ class Task {
{
request_.data = this;
result_ = nullptr;

#if NODE_MODULE_VERSION >= 59
async_context_ = node::EmitAsyncInit(isolate, Nan::New<v8::Object>(), "neon_task");
#endif

// Save the callback to be invoked when the task completes.
callback_.Reset(isolate, callback);
// Save the context (aka realm) to be used when invoking the callback.
context_.Reset(isolate, isolate->GetCurrentContext());
}

#if NODE_MODULE_VERSION >= 59
~Task() {
node::EmitAsyncDestroy(isolate_, async_context_);
}
#endif

void execute() {
result_ = perform_(rust_task_);
}
Expand Down Expand Up @@ -59,7 +72,13 @@ class Task {
}

v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(isolate_, callback_);

#if NODE_MODULE_VERSION >= 59
node::MakeCallback(isolate_, context->Global(), callback, 2, argv, async_context_);
#else
node::MakeCallback(isolate_, context->Global(), callback, 2, argv);
#endif

callback_.Reset();
context_.Reset();
}
Expand All @@ -78,6 +97,9 @@ class Task {
void *result_;
v8::Persistent<v8::Function> callback_;
v8::Persistent<v8::Context> context_;
#if NODE_MODULE_VERSION >= 59
node::async_context async_context_;
#endif
};

void execute_task(uv_work_t *request) {
Expand Down

0 comments on commit 20df99b

Please sign in to comment.