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

error if invalid 'before' argument is passed to Map#addLayer #5401

Merged
merged 2 commits into from
Oct 5, 2017

Conversation

mollymerp
Copy link
Contributor

fix #3868

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • post benchmark scores
  • manually test the debug page

@@ -568,7 +568,10 @@ class Style extends Evented {

layer.setEventedParent(this, {layer: {id: id}});

const index = before ? this._order.indexOf(before) : this._order.length;
const beforeIndex = before ? this._order.indexOf(before) : 0;
Copy link
Contributor

@peterqliu peterqliu Oct 4, 2017

Choose a reason for hiding this comment

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

@mollymerp is 0 the top or bottom of the stack? Hitting map.style._order in my console, the first layer in the array is visually under all other layers on the map.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose this is misleading – beforeIndex isn't actually used unless before exists so the 0 value never gets assigned to index. I made it 0 instead of null to appease flow but I could make this clearer.


const beforeIndex = before ? this._order.indexOf(before) : -1;
const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length;
if (before && beforeIndex === -1) util.warnOnce(`Layer with id "${before}" does not exist on this map. Adding layer to the top of the stack.`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's report this via this.fire('error', ...) for consistency with non-existent layer IDs in moveLayer, removeLayer, etc.

}]
}));
const layer = {id: 'c', type: 'background'};
t.stub(console, 'error');
Copy link
Contributor

Choose a reason for hiding this comment

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

Bind a handler for the error event rather than stubbing console.error (see the subsequent test for example).


const beforeIndex = before ? this._order.indexOf(before) : -1;
const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length;
if (before && beforeIndex === -1) this.fire('error', {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding to the top of the stack, return early without making changes in the case of an error.

@@ -568,7 +568,12 @@ class Style extends Evented {

layer.setEventedParent(this, {layer: {id: id}});

const index = before ? this._order.indexOf(before) : this._order.length;

const beforeIndex = before ? this._order.indexOf(before) : -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

this._order.length can never be -1, so the conditionals can be simplified:

const index = before ? this._order.indexOf(before) : this._order.length;
if (index === -1) {
    // error
}

this._order.splice(index, ...);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah good point! I still had to use if (before && index === -1) for flow to allow me to include before in the error message.

@mollymerp mollymerp force-pushed the invalid-before branch 2 times, most recently from dec81e9 to 8c022d3 Compare October 5, 2017 17:10
@mollymerp mollymerp changed the title warn if invalid 'before' argument is passed to Map#addLayer error if invalid 'before' argument is passed to Map#addLayer Oct 5, 2017
@mollymerp mollymerp merged commit dff1fa4 into master Oct 5, 2017
@mollymerp mollymerp deleted the invalid-before branch October 5, 2017 18:27
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.

Unexpected behaviour when calling "Map#addLayer" with invalid "before" argument
3 participants