[node] NodeRequest inherits from Nan::AsyncWorker
drop NodeRequest::Create, move MakeCallback to NodeRequest::Execute rework Respond -> HandleCallback modern NAN style updates
This commit is contained in:
@@ -32,9 +32,6 @@ struct NodeMap::RenderOptions {
|
||||
mbgl::MapDebugOptions debugOptions = mbgl::MapDebugOptions::NoDebug;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Static Node Methods
|
||||
|
||||
Nan::Persistent<v8::Function> NodeMap::constructor;
|
||||
|
||||
static std::shared_ptr<mbgl::HeadlessDisplay> sharedDisplay() {
|
||||
@@ -114,7 +111,7 @@ NAN_MODULE_INIT(NodeMap::Init) {
|
||||
* fs.writeFileSync('image.png', image);
|
||||
* });
|
||||
*/
|
||||
NAN_METHOD(NodeMap::New) {
|
||||
void NodeMap::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
if (!info.IsConstructCall()) {
|
||||
return Nan::ThrowTypeError("Use the new operator to create new Map objects");
|
||||
}
|
||||
@@ -123,7 +120,7 @@ NAN_METHOD(NodeMap::New) {
|
||||
return Nan::ThrowTypeError("Requires an options object as first argument");
|
||||
}
|
||||
|
||||
auto options = info[0]->ToObject();
|
||||
auto options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
|
||||
|
||||
// Check that 'request' is set. If 'cancel' is set it must be a
|
||||
// function and if 'ratio' is set it must be a number.
|
||||
@@ -157,9 +154,12 @@ NAN_METHOD(NodeMap::New) {
|
||||
std::string StringifyStyle(v8::Local<v8::Value> styleHandle) {
|
||||
Nan::HandleScope scope;
|
||||
|
||||
v8::Local<v8::Object> JSON = Nan::Get(
|
||||
Nan::GetCurrentContext()->Global(),
|
||||
Nan::New("JSON").ToLocalChecked()).ToLocalChecked()->ToObject();
|
||||
v8::Local<v8::Object> JSON = Nan::To<v8::Object>(
|
||||
Nan::Get(
|
||||
Nan::GetCurrentContext()->Global(),
|
||||
Nan::New("JSON").ToLocalChecked()
|
||||
).ToLocalChecked()
|
||||
).ToLocalChecked();
|
||||
|
||||
return *Nan::Utf8String(Nan::MakeCallback(JSON, "stringify", 1, &styleHandle));
|
||||
}
|
||||
@@ -179,7 +179,7 @@ std::string StringifyStyle(v8::Local<v8::Value> styleHandle) {
|
||||
* // providing a string
|
||||
* map.load(fs.readFileSync('./test/fixtures/style.json', 'utf8'));
|
||||
*/
|
||||
NAN_METHOD(NodeMap::Load) {
|
||||
void NodeMap::Load(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -212,7 +212,7 @@ NAN_METHOD(NodeMap::Load) {
|
||||
info.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::Loaded) {
|
||||
void NodeMap::Loaded(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -262,16 +262,16 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
|
||||
}
|
||||
|
||||
if (Nan::Has(obj, Nan::New("classes").ToLocalChecked()).FromJust()) {
|
||||
auto classes = Nan::Get(obj, Nan::New("classes").ToLocalChecked()).ToLocalChecked()->ToObject().As<v8::Array>();
|
||||
auto classes = Nan::To<v8::Object>(Nan::Get(obj, Nan::New("classes").ToLocalChecked()).ToLocalChecked()).ToLocalChecked().As<v8::Array>();
|
||||
const int length = classes->Length();
|
||||
options.classes.reserve(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
options.classes.push_back(std::string { *Nan::Utf8String(Nan::Get(classes, i).ToLocalChecked()->ToString()) });
|
||||
options.classes.push_back(std::string { *Nan::Utf8String(Nan::To<v8::String>(Nan::Get(classes, i).ToLocalChecked()).ToLocalChecked()) });
|
||||
}
|
||||
}
|
||||
|
||||
if (Nan::Has(obj, Nan::New("debug").ToLocalChecked()).FromJust()) {
|
||||
auto debug = Nan::Get(obj, Nan::New("debug").ToLocalChecked()).ToLocalChecked()->ToObject().As<v8::Object>();
|
||||
auto debug = Nan::To<v8::Object>(Nan::Get(obj, Nan::New("debug").ToLocalChecked()).ToLocalChecked()).ToLocalChecked();
|
||||
if (Nan::Has(debug, Nan::New("tileBorders").ToLocalChecked()).FromJust()) {
|
||||
if (Nan::Get(debug, Nan::New("tileBorders").ToLocalChecked()).ToLocalChecked()->BooleanValue()) {
|
||||
options.debugOptions = options.debugOptions | mbgl::MapDebugOptions::TileBorders;
|
||||
@@ -318,7 +318,7 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
|
||||
* @returns {undefined} calls callback
|
||||
* @throws {Error} if stylesheet is not loaded or if map is already rendering
|
||||
*/
|
||||
NAN_METHOD(NodeMap::Render) {
|
||||
void NodeMap::Render(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -338,7 +338,7 @@ NAN_METHOD(NodeMap::Render) {
|
||||
return Nan::ThrowError("Map is currently rendering an image");
|
||||
}
|
||||
|
||||
auto options = ParseOptions(info[0]->ToObject());
|
||||
auto options = ParseOptions(Nan::To<v8::Object>(info[0]).ToLocalChecked());
|
||||
|
||||
assert(!nodeMap->callback);
|
||||
assert(!nodeMap->image.data);
|
||||
@@ -448,7 +448,7 @@ void NodeMap::renderFinished() {
|
||||
* @name release
|
||||
* @returns {undefined}
|
||||
*/
|
||||
NAN_METHOD(NodeMap::Release) {
|
||||
void NodeMap::Release(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -471,7 +471,7 @@ void NodeMap::release() {
|
||||
map.reset();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::AddClass) {
|
||||
void NodeMap::AddClass(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -488,7 +488,7 @@ NAN_METHOD(NodeMap::AddClass) {
|
||||
info.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::AddSource) {
|
||||
void NodeMap::AddSource(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -512,7 +512,7 @@ NAN_METHOD(NodeMap::AddSource) {
|
||||
nodeMap->map->addSource(std::move(*source));
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::AddLayer) {
|
||||
void NodeMap::AddLayer(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -532,7 +532,7 @@ NAN_METHOD(NodeMap::AddLayer) {
|
||||
nodeMap->map->addLayer(std::move(*layer));
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::RemoveLayer) {
|
||||
void NodeMap::RemoveLayer(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -550,7 +550,7 @@ NAN_METHOD(NodeMap::RemoveLayer) {
|
||||
nodeMap->map->removeLayer(*Nan::Utf8String(info[0]));
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::SetLayoutProperty) {
|
||||
void NodeMap::SetLayoutProperty(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -583,7 +583,7 @@ NAN_METHOD(NodeMap::SetLayoutProperty) {
|
||||
info.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::SetPaintProperty) {
|
||||
void NodeMap::SetPaintProperty(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -621,7 +621,7 @@ NAN_METHOD(NodeMap::SetPaintProperty) {
|
||||
info.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::SetFilter) {
|
||||
void NodeMap::SetFilter(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
using namespace mbgl::style;
|
||||
using namespace mbgl::style::conversion;
|
||||
|
||||
@@ -676,7 +676,7 @@ NAN_METHOD(NodeMap::SetFilter) {
|
||||
Nan::ThrowTypeError("layer doesn't support filters");
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::DumpDebugLogs) {
|
||||
void NodeMap::DumpDebugLogs(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -684,7 +684,7 @@ NAN_METHOD(NodeMap::DumpDebugLogs) {
|
||||
info.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
NAN_METHOD(NodeMap::QueryRenderedFeatures) {
|
||||
void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
|
||||
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
|
||||
|
||||
@@ -732,9 +732,6 @@ NAN_METHOD(NodeMap::QueryRenderedFeatures) {
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Instance
|
||||
|
||||
NodeMap::NodeMap(v8::Local<v8::Object> options) :
|
||||
view(sharedDisplay(), [&] {
|
||||
Nan::HandleScope scope;
|
||||
@@ -756,22 +753,23 @@ NodeMap::~NodeMap() {
|
||||
if (map) release();
|
||||
}
|
||||
|
||||
std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, Callback callback_) {
|
||||
Nan::HandleScope handleScope;
|
||||
std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) {
|
||||
Nan::HandleScope scope;
|
||||
|
||||
// Enter a new v8::Context to avoid leaking v8::FunctionTemplate
|
||||
// from Nan::New<v8::Function>
|
||||
v8::Local<v8::Context> context = v8::Context::New(v8::Isolate::GetCurrent());
|
||||
v8::Context::Scope scope(context);
|
||||
v8::Local<v8::Value> argv[] = {
|
||||
Nan::New<v8::External>(this),
|
||||
Nan::New<v8::External>(&callback_)
|
||||
};
|
||||
|
||||
auto requestHandle = NodeRequest::Create(resource, callback_)->ToObject();
|
||||
auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(requestHandle);
|
||||
auto callbackHandle = Nan::New<v8::Function>(NodeRequest::Respond, requestHandle);
|
||||
auto instance = Nan::New(NodeRequest::constructor)->NewInstance(2, argv);
|
||||
|
||||
v8::Local<v8::Value> argv[] = { requestHandle, callbackHandle };
|
||||
Nan::MakeCallback(handle()->GetInternalField(1)->ToObject(), "request", 2, argv);
|
||||
Nan::Set(instance, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked());
|
||||
Nan::Set(instance, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(resource.kind));
|
||||
|
||||
auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(instance);
|
||||
request->Execute();
|
||||
|
||||
return std::make_unique<NodeRequest::NodeAsyncRequest>(request);
|
||||
}
|
||||
|
||||
} // namespace node_mbgl
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user