Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[node] Need to have a singleton RunLoop instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Nov 14, 2015
1 parent b1a91b9 commit 1f67471
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
12 changes: 3 additions & 9 deletions platform/node/src/node_file_source.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "node_file_source.hpp"
#include "node_request.hpp"
#include "node_mapbox_gl_native.hpp"

namespace node_mbgl {

Expand All @@ -8,15 +9,8 @@ class NodeFileSourceRequest : public mbgl::FileRequest {
std::unique_ptr<mbgl::WorkRequest> workRequest;
};

NodeFileSource::NodeFileSource(v8::Local<v8::Object> options_)
: nodeLoop(uv_default_loop()) {
NodeFileSource::NodeFileSource(v8::Local<v8::Object> options_) {
options.Reset(options_);

// This has the effect of unreffing an async handle, which otherwise would keep the
// default loop running. You would think we could do this in the destructor instead,
// but in fact the destructor might not get called if there's no GC pressure which
// would cause the NodeMap object which owns us to get destroyed.
nodeLoop.stop();
}

NodeFileSource::~NodeFileSource() {
Expand All @@ -28,7 +22,7 @@ std::unique_ptr<mbgl::FileRequest> NodeFileSource::request(const mbgl::Resource&

// This function can be called from any thread. Make sure we're executing the
// JS implementation in the node event loop.
req->workRequest = nodeLoop.invokeWithCallback([this] (mbgl::Resource res, Callback cb) {
req->workRequest = NodeRunLoop().invokeWithCallback([this] (mbgl::Resource res, Callback cb) {
Nan::HandleScope scope;

auto requestHandle = NodeRequest::Create(res, cb)->ToObject();
Expand Down
2 changes: 0 additions & 2 deletions platform/node/src/node_file_source.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/run_loop.hpp>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
Expand All @@ -21,7 +20,6 @@ class NodeFileSource : public mbgl::FileSource {

private:
Nan::Persistent<v8::Object> options;
mbgl::util::RunLoop nodeLoop;
};

}
15 changes: 15 additions & 0 deletions platform/node/src/node_mapbox_gl_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@
#include <nan.h>
#pragma GCC diagnostic pop

#include "node_mapbox_gl_native.hpp"
#include "node_map.hpp"
#include "node_log.hpp"
#include "node_request.hpp"

namespace node_mbgl {

mbgl::util::RunLoop& NodeRunLoop() {
static mbgl::util::RunLoop nodeRunLoop(uv_default_loop());
return nodeRunLoop;
}

}

NAN_MODULE_INIT(RegisterModule) {
// This has the effect of:
// a) Ensuring that the static local variable is initialized before any thread contention.
// b) unreffing an async handle, which otherwise would keep the default loop running.
node_mbgl::NodeRunLoop().stop();

node_mbgl::NodeMap::Init(target);
node_mbgl::NodeRequest::Init(target);

Expand Down
9 changes: 9 additions & 0 deletions platform/node/src/node_mapbox_gl_native.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <mbgl/util/run_loop.hpp>

namespace node_mbgl {

mbgl::util::RunLoop& NodeRunLoop();

}

0 comments on commit 1f67471

Please sign in to comment.