This commit is contained in:
John Firebaugh
2015-09-03 15:54:18 -07:00
parent aa001f07c3
commit 0531b38367
10 changed files with 239 additions and 242 deletions

View File

@@ -11,7 +11,7 @@ struct NodeFileSource::Action {
mbgl::Resource const resource;
};
NodeFileSource::NodeFileSource(v8::Handle<v8::Object> options_) :
NodeFileSource::NodeFileSource(v8::Local<v8::Object> options_) :
queue(new Queue(uv_default_loop(), [this](Action &action) {
if (action.type == Action::Add) {
processAdd(action.resource);
@@ -20,7 +20,7 @@ NodeFileSource::NodeFileSource(v8::Handle<v8::Object> options_) :
}
}))
{
NanAssignPersistent(options, options_->ToObject());
options.Reset(options_);
// Make sure that the queue doesn't block the loop from exiting.
queue->unref();
@@ -30,7 +30,7 @@ NodeFileSource::~NodeFileSource() {
queue->stop();
queue = nullptr;
NanDisposePersistent(options);
options.Reset();
}
mbgl::Request* NodeFileSource::request(const mbgl::Resource& resource, uv_loop_t* loop, Callback callback) {
@@ -68,44 +68,30 @@ void NodeFileSource::cancel(mbgl::Request* req) {
}
void NodeFileSource::processAdd(const mbgl::Resource& resource) {
NanScope();
Nan::HandleScope scope;
// Make sure the loop stays alive as long as request is pending.
if (pending.empty()) {
queue->ref();
}
auto requestHandle = NanNew<v8::Object>(NodeRequest::Create(this, resource));
v8::Persistent<v8::Object> requestPersistent;
NanAssignPersistent(requestPersistent, requestHandle);
pending.emplace(resource, std::move(requestPersistent));
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto requestFunction = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), options)->Get(NanNew("request")).As<v8::Function>();
#else
auto requestFunction = options->Get(NanNew("request")).As<v8::Function>();
#endif
auto requestHandle = Nan::New<v8::Object>(NodeRequest::Create(this, resource));
pending.emplace(resource, requestHandle);
v8::Local<v8::Value> argv[] = { requestHandle };
NanMakeCallback(NanGetCurrentContext()->Global(), requestFunction, 1, argv);
Nan::MakeCallback(Nan::New(options), "request", 1, argv);
}
void NodeFileSource::processCancel(const mbgl::Resource& resource) {
NanScope();
Nan::HandleScope scope;
auto it = pending.find(resource);
if (it == pending.end()) {
// The response callback was already fired. There is no point in calling the cancelation
// callback because the request is already completed.
} else {
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto requestHandle = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), it->second);
v8::Local<v8::Object> requestHandle = Nan::New(it->second);
it->second.Reset();
#else
auto requestHandle = NanNew<v8::Object>(it->second);
NanDisposePersistent(it->second);
#endif
pending.erase(it);
// Make sure the the loop can exit when there are no pending requests.
@@ -113,20 +99,13 @@ void NodeFileSource::processCancel(const mbgl::Resource& resource) {
queue->unref();
}
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto optionsObject = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), options);
if (optionsObject->Has(NanNew("cancel"))) {
auto cancelFunction = optionsObject->Get(NanNew("cancel")).As<v8::Function>();
#else
if (options->Has(NanNew("cancel"))) {
auto cancelFunction = options->Get(NanNew("cancel")).As<v8::Function>();
#endif
if (Nan::Has(Nan::New(options), Nan::New("cancel").ToLocalChecked()).FromJust()) {
v8::Local<v8::Value> argv[] = { requestHandle };
NanMakeCallback(NanGetCurrentContext()->Global(), cancelFunction, 1, argv);
Nan::MakeCallback(Nan::New(options), "cancel", 1, argv);
}
// Set the request handle in the request wrapper handle to null
node::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
Nan::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
}
}
@@ -134,11 +113,7 @@ void NodeFileSource::notify(const mbgl::Resource& resource, const std::shared_pt
// First, remove the request, since it might be destructed at any point now.
auto it = pending.find(resource);
if (it != pending.end()) {
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
it->second.Reset();
#else
NanDisposePersistent(it->second);
#endif
pending.erase(it);
// Make sure the the loop can exit when there are no pending requests.