[test] One map per pixel ratio in render.test.js --recycle-map

This commit is contained in:
Bruno de Oliveira Abinader
2017-08-04 11:05:14 +03:00
parent b43d88b725
commit 6f00becffe

View File

@@ -10,23 +10,27 @@ mbgl.on('message', function(msg) {
console.log('%s (%s): %s', msg.severity, msg.class, msg.text);
});
// Map of map objects by pixel ratio
var maps = new Map();
module.exports = function (style, options, callback) {
var map = new mbgl.Map({
ratio: options.pixelRatio,
request: function(req, callback) {
request(req.url, {encoding: null}, function (err, response, body) {
if (err) {
callback(err);
} else if (response.statusCode == 404) {
callback();
} else if (response.statusCode != 200) {
callback(new Error(response.statusMessage));
} else {
callback(null, {data: body});
}
});
if (options.recycleMap) {
if (maps.has(options.pixelRatio)) {
var map = maps.get(options.pixelRatio);
map.request = mapRequest;
} else {
maps.set(options.pixelRatio, new mbgl.Map({
ratio: options.pixelRatio,
request: mapRequest
}));
var map = maps.get(options.pixelRatio);
}
});
} else {
var map = new mbgl.Map({
ratio: options.pixelRatio,
request: mapRequest
});
}
var timedOut = false;
var watchdog = setTimeout(function () {
@@ -48,12 +52,28 @@ module.exports = function (style, options, callback) {
map.load(style, { defaultStyleCamera: true });
function mapRequest(req, callback) {
request(req.url, {encoding: null}, function (err, response, body) {
if (err) {
callback(err);
} else if (response.statusCode == 404) {
callback();
} else if (response.statusCode != 200) {
callback(new Error(response.statusMessage));
} else {
callback(null, {data: body});
}
});
};
applyOperations(options.operations, function() {
map.render(options, function (err, pixels) {
var results = options.queryGeometry ?
map.queryRenderedFeatures(options.queryGeometry, options.queryOptions || {}) :
[];
map.release();
if (!options.recycleMap) {
map.release();
}
if (timedOut) return;
clearTimeout(watchdog);
callback(err, pixels, results.map(prepareFeatures));