Skip to content

Commit

Permalink
Convert OpenBaton to AsyncWorker
Browse files Browse the repository at this point in the history
Signed-off-by: Gareth Hancock <gazhank@gmail.com>
  • Loading branch information
GazHank committed Aug 24, 2021
1 parent 16dbc23 commit d042c04
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
29 changes: 2 additions & 27 deletions packages/bindings/src/serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Napi::Value Open(const Napi::CallbackInfo& info) {
return env.Null();
}

OpenBaton* baton = new OpenBaton();
OpenBaton* baton = new OpenBaton(info[2].As<Napi::Function>());
snprintf(baton->path, sizeof(baton->path), "%s", path.c_str());
baton->baudRate = getIntFromObject(options, "baudRate");
baton->dataBits = getIntFromObject(options, "dataBits");
Expand All @@ -66,41 +66,16 @@ Napi::Value Open(const Napi::CallbackInfo& info) {
baton->xany = getBoolFromObject(options, "xany");
baton->hupcl = getBoolFromObject(options, "hupcl");
baton->lock = getBoolFromObject(options, "lock");
baton->callback.Reset(info[2].As<Napi::Function>());

#ifndef WIN32
baton->vmin = getIntFromObject(options, "vmin");
baton->vtime = getIntFromObject(options, "vtime");
#endif

napi_value resource_name;
napi_create_string_utf8(env, "Open", NAPI_AUTO_LENGTH, &resource_name);
napi_create_async_work(env, NULL, resource_name, EIO_Open, EIO_AfterOpen, baton, &baton->work);
napi_queue_async_work(env, baton->work);
baton->Queue();
return env.Undefined();
}

void EIO_AfterOpen(napi_env n_env, napi_status status, void* req) {
Napi::Env env = Napi::Env::Env(n_env);
Napi::HandleScope scope(env);

OpenBaton* data = (OpenBaton*)req;

std::vector<napi_value> args;
args.reserve(2);
if (data->errorString[0]) {
args.push_back(Napi::String::New(env, data->errorString));
args.push_back(env.Undefined());
} else {
args.push_back(env.Null());
args.push_back(Napi::Number::New(env, data->result));
}

data->callback.Call(args);
napi_delete_async_work(env, data->work);
free(data);
}

Napi::Value Update(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// file descriptor
Expand Down
24 changes: 18 additions & 6 deletions packages/bindings/src/serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
#define ERROR_STRING_SIZE 1024

Napi::Value Open(const Napi::CallbackInfo& info);
void EIO_Open(napi_env env, void* req);
void EIO_AfterOpen(napi_env n_env, napi_status status, void* req);
void EIO_Open(void* req);

Napi::Value Update(const Napi::CallbackInfo& info);
void EIO_Update(napi_env env, void* req);
Expand Down Expand Up @@ -64,12 +63,10 @@ enum SerialPortStopBits {
SerialPortParity ToParityEnum(const Napi::String& str);
SerialPortStopBits ToStopBitEnum(double stopBits);

struct OpenBaton {//: public Napi::AsyncResource {
OpenBaton() : // AsyncResource("node-serialport:OpenBaton"),
struct OpenBaton : public Napi::AsyncWorker {
OpenBaton(Napi::Function& callback) : Napi::AsyncWorker(callback, "node-serialport:OpenBaton"),
errorString(), path() {}
char errorString[ERROR_STRING_SIZE];
Napi::FunctionReference callback;
napi_async_work work;
char path[1024];
int fd = 0;
int result = 0;
Expand All @@ -88,6 +85,21 @@ struct OpenBaton {//: public Napi::AsyncResource {
uint8_t vmin = 0;
uint8_t vtime = 0;
#endif
void Execute() override {
EIO_Open(this);
}

void OnError(Napi::Error const &error) override {

This comment has been minimized.

Copy link
@NickNaso

NickNaso Aug 24, 2021

This method will be called only if you set the error using the method Napi::AsyncWorker::SetError otherwise the Napi::AsyncWorker::OnOK method will be called.
See: https://github.com/nodejs/node-addon-api/blob/main/doc/async_worker.md#seterror and https://github.com/nodejs/node-addon-api/blob/main/doc/async_worker.md#execute
One idea is to call the Napi::AsyncWorker::SetError at the end of the Napi::AsyncWorker::Execute method like repoted below:

  void Execute() override {
    EIO_Open(this);
   SetError(std::string(errorString));
  }
auto env = Env();
Napi::HandleScope scope(env);
Callback().Call({Napi::String::New(env, errorString), env.Undefined()});
}

void OnOK() override {
auto env = Env();
Napi::HandleScope scope(env);
Callback().Call({env.Null(), Napi::Number::New(env, result)});
}
};

struct ConnectionOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/bindings/src/serialport_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int ToDataBitsConstant(int dataBits) {
return -1;
}

void EIO_Open(napi_env env, void* req) {
void EIO_Open(void* req) {
OpenBaton* data = (OpenBaton*)req;

int flags = (O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC | O_SYNC);
Expand Down
2 changes: 1 addition & 1 deletion packages/bindings/src/serialport_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void AsyncCloseCallback(uv_handle_t* handle) {
delete async;
}

void EIO_Open(napi_env env, void* req) {
void EIO_Open(void* req) {
OpenBaton* data = (OpenBaton*)req;

char originalPath[1024];
Expand Down

0 comments on commit d042c04

Please sign in to comment.