Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
melchor629 committed Jan 5, 2020
1 parent dd00287 commit 4965053
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ namespace flac_bindings {
if(res->IsPromise()) {
self.defer(res.template As<Promise>(), e, [endMode=endMode] (auto &c, auto e, auto &info) {
if(*e == '9' && endMode == "exception") {
c.reject(Nan::Error("Thrown :("));
Nan::ThrowError(Nan::Error("Thrown :("));
}
});
return;
}
}
if(*e == '9' && endMode == "exception") {
self.reject(Nan::Error("Thrown :("));
Nan::ThrowError(Nan::Error("Thrown :("));
}
};

Expand Down
13 changes: 7 additions & 6 deletions src/utils/async.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace flac_bindings {

void defer(v8::Local<v8::Promise> promise, const P* data, FullPromiseCallback resolve = nullptr, FullPromiseCallback reject = nullptr) {
using namespace std::placeholders;
Nan::HandleScope scope;
auto context = new PromiseContext {
resolve ? std::bind(resolve, *this, _1, _2) : PromiseCallback(nullptr),
reject ? std::bind(reject, *this, _1, _2) : PromiseCallback(nullptr),
Expand Down Expand Up @@ -144,7 +145,7 @@ namespace flac_bindings {
ToV8ConverterFunction convertFunction
): Nan::AsyncProgressQueueWorker<P>(((Nan::Callback*) 0x1), name), function(function), progress(progress), convertFunction(convertFunction) {
auto resolver = v8::Promise::Resolver::New(Nan::GetCurrentContext()).ToLocalChecked();
asyncContext = node::EmitAsyncInit(v8::Isolate::GetCurrent(), Nan::New<v8::Object>(), name);
asyncContext = node::EmitAsyncInit(v8::Isolate::GetCurrent(), resolver, name);
this->SaveToPersistent("promiseResolver", resolver);
}

Expand Down Expand Up @@ -179,7 +180,7 @@ namespace flac_bindings {
if(this->progress) {
Nan::HandleScope scope;
//Workaround https://github.com/nodejs/node/issues/5691 - This allows to handle promises without any warnings from node
node::CallbackScope callbackScope(v8::Isolate::GetCurrent(), Nan::New<v8::Object>(), asyncContext);
node::CallbackScope callbackScope(v8::Isolate::GetCurrent(), this->getResolver(), asyncContext);
Nan::TryCatch tryCatch;
this->progress(*executionContext, data, size);
if(tryCatch.HasCaught()) {
Expand All @@ -205,12 +206,12 @@ namespace flac_bindings {
auto resolver = this->getResolver();
//Workaround https://github.com/nodejs/node/issues/5691
node::CallbackScope callbackScope(v8::Isolate::GetCurrent(), resolver, this->asyncContext);
auto context = Nan::GetCurrentContext();
auto context = resolver->CreationContext();

if(this->returnValue.IsJust() && this->convertFunction) {
resolver->Resolve(context, this->convertFunction(this->returnValue.FromJust())).FromJust();
} else {
resolver->Resolve(context, Nan::Null()).FromJust();
resolver->Resolve(context, Nan::Undefined()).FromJust();
}
}

Expand All @@ -220,10 +221,10 @@ namespace flac_bindings {
auto resolver = this->getResolver();
//Workaround https://github.com/nodejs/node/issues/5691
node::CallbackScope callbackScope(v8::Isolate::GetCurrent(), resolver, this->asyncContext);
auto context = Nan::GetCurrentContext();
auto context = resolver->CreationContext();

if(this->exceptionValue.IsEmpty()) {
auto exception = v8::Exception::Error(Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked());
auto exception = Nan::Error(Nan::New<v8::String>(this->ErrorMessage()).ToLocalChecked());
resolver->Reject(context, exception).FromJust();
} else {
auto exception = Nan::New(this->exceptionValue);
Expand Down

0 comments on commit 4965053

Please sign in to comment.