mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-15 21:48:20 +02:00
Make imageryBlocklists return regex objects instead of strings
Fix performance bug due to checking background source blocks too often
This commit is contained in:
@@ -271,6 +271,7 @@ export function rendererBackground(context) {
|
||||
context.history().photoOverlaysUsed(photoOverlaysUsed);
|
||||
};
|
||||
|
||||
let _checkedBlocklists;
|
||||
|
||||
background.sources = (extent, zoom, includeCurrent) => {
|
||||
if (!_imageryIndex) return []; // called before init()?
|
||||
@@ -283,15 +284,19 @@ export function rendererBackground(context) {
|
||||
|
||||
const osm = context.connection();
|
||||
const blocklists = osm && osm.imageryBlocklists();
|
||||
const isBlocked = blocklists && function(source) {
|
||||
return blocklists.some(function(blocklist) {
|
||||
return new RegExp(blocklist).test(source.template());
|
||||
|
||||
if (blocklists && blocklists !== _checkedBlocklists) {
|
||||
_imageryIndex.backgrounds.forEach(source => {
|
||||
source.isBlocked = blocklists.some(function(blocklist) {
|
||||
return blocklist.test(source.template());
|
||||
});
|
||||
});
|
||||
};
|
||||
_checkedBlocklists = blocklists;
|
||||
}
|
||||
|
||||
return _imageryIndex.backgrounds.filter(source => {
|
||||
if (includeCurrent && currSource === source) return true; // optionally include the current imagery
|
||||
if (isBlocked && isBlocked(source)) return false; // even bundled sources may be blocked - #7905
|
||||
if (includeCurrent && currSource === source) return true; // optionally always include the current imagery
|
||||
if (source.isBlocked) return false; // even bundled sources may be blocked - #7905
|
||||
if (!source.polygon) return true; // always include imagery with worldwide coverage
|
||||
if (zoom && zoom < 6) return false; // optionally exclude local imagery at low zooms
|
||||
return visible[source.id]; // include imagery visible in given extent
|
||||
@@ -320,19 +325,15 @@ export function rendererBackground(context) {
|
||||
let regex;
|
||||
|
||||
for (let i = 0; i < blocklists.length; i++) {
|
||||
try {
|
||||
regex = new RegExp(blocklists[i]);
|
||||
fail = regex.test(template);
|
||||
tested++;
|
||||
if (fail) break;
|
||||
} catch (e) {
|
||||
/* noop */
|
||||
}
|
||||
regex = blocklists[i];
|
||||
fail = regex.test(template);
|
||||
tested++;
|
||||
if (fail) break;
|
||||
}
|
||||
|
||||
// ensure at least one test was run.
|
||||
if (!tested) {
|
||||
regex = new RegExp('.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*');
|
||||
regex = /.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/;
|
||||
fail = regex.test(template);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ var oauth = osmAuth({
|
||||
done: authDone
|
||||
});
|
||||
// hardcode default block of Google Maps
|
||||
var _imageryBlocklists = ['.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*'];
|
||||
var _imageryBlocklists = [/.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/];
|
||||
var _tileCache = { toLoad: {}, loaded: {}, inflight: {}, seen: {}, rtree: new RBush() };
|
||||
var _noteCache = { toLoad: {}, loaded: {}, inflight: {}, inflightPost: {}, note: {}, closed: {}, rtree: new RBush() };
|
||||
var _userCache = { toLoad: {}, user: {} };
|
||||
@@ -923,9 +923,14 @@ export default {
|
||||
var elements = xml.getElementsByTagName('blacklist');
|
||||
var regexes = [];
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
var regex = elements[i].getAttribute('regex'); // needs unencode?
|
||||
if (regex) {
|
||||
regexes.push(regex);
|
||||
var regexString = elements[i].getAttribute('regex'); // needs unencode?
|
||||
if (regexString) {
|
||||
try {
|
||||
var regex = new RegExp(regexString);
|
||||
regexes.push(regex);
|
||||
} catch (e) {
|
||||
/* noop */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (regexes.length) {
|
||||
|
||||
+5
-9
@@ -388,19 +388,15 @@ export function svgData(projection, context, dispatch) {
|
||||
var regex;
|
||||
|
||||
for (var i = 0; i < blocklists.length; i++) {
|
||||
try {
|
||||
regex = new RegExp(blocklists[i]);
|
||||
fail = regex.test(val);
|
||||
tested++;
|
||||
if (fail) break;
|
||||
} catch (e) {
|
||||
/* noop */
|
||||
}
|
||||
regex = blocklists[i];
|
||||
fail = regex.test(val);
|
||||
tested++;
|
||||
if (fail) break;
|
||||
}
|
||||
|
||||
// ensure at least one test was run.
|
||||
if (!tested) {
|
||||
regex = new RegExp('.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*');
|
||||
regex = /.*\.google(apis)?\..*\/(vt|kh)[\?\/].*([xyz]=.*){3}.*/;
|
||||
fail = regex.test(val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -732,7 +732,7 @@ describe('iD.serviceOsm', function () {
|
||||
it('updates imagery blocklists', function(done) {
|
||||
connection.status(function() {
|
||||
var blocklists = connection.imageryBlocklists();
|
||||
expect(blocklists).to.deep.equal(['\.foo\.com','\.bar\.org']);
|
||||
expect(blocklists).to.deep.equal([new RegExp('\.foo\.com'), new RegExp('\.bar\.org')]);
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user