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

Throw an error when a used source is removed and not readded #5562

Merged
merged 4 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
6 changes: 6 additions & 0 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ class Style extends Evented {
if (this.sourceCaches[id] === undefined) {
throw new Error('There is no source with this ID');
}
for (const layerId in this._layers) {
if (this._layers[layerId].source === id) {
return this.fire('error', `Source "${id}" cannot be removed while layer "${layerId}" is using it.`);
}
}

const sourceCache = this.sourceCaches[id];
delete this.sourceCaches[id];
delete this._updatedSources[id];
Expand Down
54 changes: 48 additions & 6 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,7 @@ test('Style#loadJSON', (t) => {
sources: {
'-source-id-': { type: "vector", tiles: [] }
},
layers: [{
'id': '-layer-id-',
'type': 'circle',
'source': '-source-id-',
'source-layer': '-source-layer-'
}]
layers: []
}));

style.on('style.load', () => {
Expand All @@ -328,6 +323,12 @@ test('Style#loadJSON', (t) => {
const source = createSource();
source['vector_layers'] = [{ id: 'green' }];
style.addSource('-source-id-', source);
style.addLayer({
'id': '-layer-id-',
'type': 'circle',
'source': '-source-id-',
'source-layer': '-source-layer-'
});
style.update();
});

Expand Down Expand Up @@ -693,6 +694,47 @@ test('Style#removeSource', (t) => {
});
});

function createStyle(callback) {
const style = new Style(new StubMap());
style.loadJSON(createStyleJSON({
'sources': {
'mapbox-source': createGeoJSONSource()
},
'layers': [{
'id': 'mapbox-layer',
'type': 'circle',
'source': 'mapbox-source',
'source-layer': 'whatever'
}]
}));
style.on('style.load', () => {
style.update();
style._recalculate(1);
callback(style);
});
return style;
}

t.test('throws if source is in use', (t) => {
createStyle((style) => {
style.on('error', (event) => {
t.end();
});
style.removeSource('mapbox-source');
});
});

t.test('does not throw if source is not in use', (t) => {
createStyle((style) => {
style.on('error', (event) => {
t.fail();
});
style.removeLayer('mapbox-layer');
style.removeSource('mapbox-source');
t.end();
});
});

t.test('tears down source event forwarding', (t) => {
const style = new Style(new StubMap());
style.loadJSON(createStyleJSON());
Expand Down