Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove layers #2116

Merged
merged 2 commits into from
May 11, 2018
Merged

Fix remove layers #2116

merged 2 commits into from
May 11, 2018

Conversation

ivanmalagon
Copy link
Contributor

This PR fixes removeLayers.

We were removing elements from the same array that we were traversing. The side effect of that is that the internal array was not completely emptied so next time the user instantiate the map, it has layers that were supposed to be removed.

This PR adds also missing tests. Neither client.removeLayers nor layers.remove had any.

@@ -163,7 +163,13 @@ Client.prototype.removeLayer = function (layer) {
* @api
*/
Client.prototype.removeLayers = function (layers) {
layers.forEach(this._removeLayer, this);
var clone = layers.slice(0);
Copy link
Contributor

@jesusbotella jesusbotella May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been thinking about what we did yesterday here and I think there are two slightly easier solutions.

The one I think is better is to keep doing .forEach but using the cloned array to iterate, like:

var layersToRemove = layers.slice(0);
layersToRemove.forEach(this._removeLayer, this);

And you will avoid the mutations on the original array passed on the parameters because of the cloned array. I think we tested this one but don't know why it didn't work 🤔, I have just tested it on the browser and it works.

And the other one, avoids cloning the array, but it is riskier than the previous one:

var length = layers.length;

for (var i = 0; i < length; i++) {
  this._removeLayer(layers[0]);
}

I prefer the first one because of obvious reasons. The thing that we did yesterday is not bad but I think it can be improved :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the code to your first suggestion. 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@ivanmalagon ivanmalagon merged commit 1c362ce into master May 11, 2018
@ivanmalagon ivanmalagon deleted the fix-remove-layers branch May 11, 2018 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants