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 duplicate layer test, enforce layer id uniqueness #6147

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ class Style extends Evented {

const id = layerObject.id;

if (this.getLayer(id)) {
this.fire('error', {error: new Error(`Layer with id "${id}" already exists on this map`)});
Copy link
Contributor

Choose a reason for hiding this comment

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

Map.addSource will throw an error when attempting to add a source with a duplicate ID. Is there a reason to fire an error event instead of throwing for addLayer ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Firing an event is correct; see #3879.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We made a decision awhile back to emit errors across the board instead of throwing #3879 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoops didn't reload and see john's comment 😬

return;
}

if (typeof layerObject.source === 'object') {
this.addSource(id, layerObject.source);
layerObject = util.clone(layerObject);
Expand Down
5 changes: 2 additions & 3 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,20 +992,19 @@ test('Style#addLayer', (t) => {
});

t.test('emits error on duplicates', (t) => {
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.

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm I guess not! left over from earlier debugging I think. removed!

const style = new Style(new StubMap());
style.loadJSON(createStyleJSON());
const layer = {id: 'background', type: 'background'};

style.on('error', (e) => {
t.deepEqual(e.layer, {id: 'background'});
t.notOk(/duplicate/.match(e.error.message));
t.match(e.error, /already exists/);
t.end();
});

style.on('style.load', () => {
style.addLayer(layer);
style.addLayer(layer);
t.end();
});
});

Expand Down