[node] Add map.cancel()

Cancels an ongoing rendering.
This commit is contained in:
Thiago Marcos P. Santos
2017-04-10 20:34:39 +03:00
committed by Thiago Marcos P. Santos
parent d767545f72
commit 51ebb96b1d
5 changed files with 167 additions and 11 deletions

View File

@@ -57,6 +57,7 @@ void NodeMap::Init(v8::Local<v8::Object> target) {
Nan::SetPrototypeMethod(tpl, "loaded", Loaded);
Nan::SetPrototypeMethod(tpl, "render", Render);
Nan::SetPrototypeMethod(tpl, "release", Release);
Nan::SetPrototypeMethod(tpl, "cancel", Cancel);
Nan::SetPrototypeMethod(tpl, "addClass", AddClass);
Nan::SetPrototypeMethod(tpl, "addSource", AddSource);
@@ -508,6 +509,42 @@ void NodeMap::release() {
map.reset();
}
/**
* Cancel an ongoing render request. The callback will be called with
* the error set to "Canceled". Will throw if no rendering is in progress.
* @name cancel
* @returns {undefined}
*/
void NodeMap::Cancel(const Nan::FunctionCallbackInfo<v8::Value>& info) {
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());
if (!nodeMap->callback) return Nan::ThrowError("No render in progress");
try {
nodeMap->cancel();
} catch (const std::exception &ex) {
return Nan::ThrowError(ex.what());
}
info.GetReturnValue().SetUndefined();
}
void NodeMap::cancel() {
auto style = map->getStyleJSON();
map = std::make_unique<mbgl::Map>(backend, mbgl::Size{ 256, 256 },
pixelRatio, *this, threadpool, mbgl::MapMode::Still);
// FIXME: Reload the style after recreating the map. We need to find
// a better way of canceling an ongoing rendering on the core level
// without resetting the map, which is way too expensive.
map->setStyleJSON(style);
error = std::make_exception_ptr(std::runtime_error("Canceled"));
renderFinished();
}
void NodeMap::AddClass(const Nan::FunctionCallbackInfo<v8::Value>& info) {
auto nodeMap = Nan::ObjectWrap::Unwrap<NodeMap>(info.Holder());
if (!nodeMap->map) return Nan::ThrowError(releasedMessage());