[node] Change Map/FileSource relationship to composition

This commit is contained in:
Bruno de Oliveira Abinader
2019-03-12 15:22:48 +02:00
parent fb1b601038
commit 096dea16e3
2 changed files with 27 additions and 12 deletions

View File

@@ -615,14 +615,14 @@ void NodeMap::cancel() {
reinterpret_cast<NodeMap *>(h->data)->renderFinished();
});
frontend = std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{ 256, 256 }, pixelRatio, *this, threadpool);
frontend = std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{ 256, 256 }, pixelRatio, fileSource, threadpool);
mbgl::MapOptions options;
options.withMapMode(mode)
.withConstrainMode(mbgl::ConstrainMode::HeightOnly)
.withViewportMode(mbgl::ViewportMode::Default)
.withCrossSourceCollisions(crossSourceCollisions);
map = std::make_unique<mbgl::Map>(*frontend, mapObserver, frontend->getSize(), pixelRatio,
*this, threadpool, options);
fileSource, threadpool, options);
// FIXME: Reload the style after recreating the map. We need to find
// a better way of canceling an ongoing rendering on the core level
@@ -1200,12 +1200,13 @@ NodeMap::NodeMap(v8::Local<v8::Object> options)
: true;
}())
, mapObserver(NodeMapObserver())
, frontend(std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size { 256, 256 }, pixelRatio, *this, threadpool))
, fileSource(this)
, frontend(std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size { 256, 256 }, pixelRatio, fileSource, threadpool))
, map(std::make_unique<mbgl::Map>(*frontend,
mapObserver,
frontend->getSize(),
pixelRatio,
*this,
fileSource,
threadpool,
mbgl::MapOptions().withMapMode(mode)
.withConstrainMode(mbgl::ConstrainMode::HeightOnly)
@@ -1226,15 +1227,19 @@ NodeMap::~NodeMap() {
if (map) release();
}
std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) {
NodeFileSource::NodeFileSource(NodeMap* nodeMap_) : nodeMap(nodeMap_) {}
std::unique_ptr<mbgl::AsyncRequest> NodeFileSource::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) {
assert(nodeMap);
Nan::HandleScope scope;
// Because this method may be called while this NodeMap is already eligible for garbage collection,
// we need to explicitly hold onto our own handle here so that GC during a v8 call doesn't destroy
// *this while we're still executing code.
handle();
nodeMap->handle();
v8::Local<v8::Value> argv[] = {
Nan::New<v8::External>(this),
Nan::New<v8::External>(nodeMap),
Nan::New<v8::External>(&callback_)
};

View File

@@ -25,10 +25,21 @@ class NodeMapObserver : public mbgl::MapObserver {
void onDidFailLoadingMap(std::exception_ptr) override;
};
class NodeMap;
class NodeFileSource : public mbgl::FileSource {
public:
NodeFileSource(NodeMap*);
std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, mbgl::FileSource::Callback) final;
private:
NodeMap* nodeMap;
};
class RenderRequest;
class NodeMap : public Nan::ObjectWrap,
public mbgl::FileSource {
class NodeMap : public Nan::ObjectWrap {
public:
struct RenderOptions;
class RenderWorker;
@@ -78,13 +89,12 @@ public:
static RenderOptions ParseOptions(v8::Local<v8::Object>);
std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, mbgl::FileSource::Callback);
const float pixelRatio;
mbgl::MapMode mode;
bool crossSourceCollisions;
NodeThreadPool threadpool;
NodeMapObserver mapObserver;
NodeFileSource fileSource;
std::unique_ptr<mbgl::HeadlessFrontend> frontend;
std::unique_ptr<mbgl::Map> map;
@@ -98,4 +108,4 @@ public:
bool loaded = false;
};
}
} // namespace node_mbgl