From 8799ae315963040910ed203c750bf180c98c882b Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Thu, 14 Sep 2017 19:43:16 -0700 Subject: [PATCH 1/8] setData for geojson source on setStyle diff --- src/source/source_cache.js | 6 ++++++ src/style-spec/diff.js | 18 ++++++++++++++---- src/style/style.js | 23 ++++++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/source/source_cache.js b/src/source/source_cache.js index 1dc68cdc77b..8de0ba5aa8f 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -102,6 +102,12 @@ class SourceCache extends Evented { } } + setData(data: GeoJSON | string) { + if (this._source && this._source.setData) { + this._source.setData(data); + } + } + /** * Return true if no tile data is pending, tiles will not change unless * an additional API call is received. diff --git a/src/style-spec/diff.js b/src/style-spec/diff.js index 27d4ae6e387..b697c362834 100644 --- a/src/style-spec/diff.js +++ b/src/style-spec/diff.js @@ -43,6 +43,11 @@ const operations = { */ removeSource: 'removeSource', + /* + * { command: 'setData', args: ['sourceId'. data] } + */ + setData: 'setData', + /* * { command: 'setLayerZoomRange', args: ['layerId', 0, 22] } */ @@ -117,10 +122,15 @@ function diffSources(before, after, commands, sourcesRemoved) { if (!before.hasOwnProperty(sourceId)) { commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] }); } else if (!isEqual(before[sourceId], after[sourceId])) { - // no update command, must remove then add - commands.push({ command: operations.removeSource, args: [sourceId] }); - commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] }); - sourcesRemoved[sourceId] = true; + if (before[sourceId].type === 'geojson' && after[sourceId].type === 'geojson') { + // geojson sources use setData command to update + commands.push({ command: operations.setData, args: [sourceId, after[sourceId].data] }); + } else { + // no update command, must remove then add + commands.push({ command: operations.removeSource, args: [sourceId] }); + commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] }); + sourcesRemoved[sourceId] = true; + } } } } diff --git a/src/style/style.js b/src/style/style.js index fd4e186ef97..07c433125b5 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -42,7 +42,8 @@ const supportedDiffOperations = util.pick(diff.operations, [ 'removeSource', 'setLayerZoomRange', 'setLight', - 'setTransition' + 'setTransition', + 'setData' // 'setGlyphs', // 'setSprite', ]); @@ -500,6 +501,26 @@ class Style extends Evented { this._changed = true; } + /** + * Set the data of a source, given its id. Only available for GeoJSON sources. + * @param {string} id id of the source + * @param {GeoJSON|string} data GeoJSON source + * @throws {Error} if no source is found with the given ID + */ + + setData(id: string, data: GeoJSON | string) { + this._checkLoaded(); + + if (this.sourceCaches[id] === undefined) { + throw new Error('There no source with this ID'); + } + + const sourceCache = this.sourceCaches[id]; + + sourceCache.setData(data); + this._changed = true; + } + /** * Get a source by id. * @param {string} id id of the desired source From 39bae05871e47eb4a73d38cead4faf9a676ce971 Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Fri, 15 Sep 2017 16:22:06 -0700 Subject: [PATCH 2/8] add style tests for setData --- src/source/source.js | 1 + src/style-spec/diff.js | 2 +- src/style/style.js | 3 +- test/unit/style/style.test.js | 58 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/source/source.js b/src/source/source.js index 4bfa9eeef23..b627b0c980a 100644 --- a/src/source/source.js +++ b/src/source/source.js @@ -57,6 +57,7 @@ export interface Source { +onAdd?: (map: Map) => void; +onRemove?: (map: Map) => void; + +setData?: (data: GeoJSON | string) => Source; loadTile(tile: Tile, callback: Callback): void; +hasTile?: (coord: TileCoord) => boolean; diff --git a/src/style-spec/diff.js b/src/style-spec/diff.js index b697c362834..3fa36d8c6df 100644 --- a/src/style-spec/diff.js +++ b/src/style-spec/diff.js @@ -44,7 +44,7 @@ const operations = { removeSource: 'removeSource', /* - * { command: 'setData', args: ['sourceId'. data] } + * { command: 'setData', args: ['sourceId', data] } */ setData: 'setData', diff --git a/src/style/style.js b/src/style/style.js index 07c433125b5..06c096febbc 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -512,9 +512,10 @@ class Style extends Evented { this._checkLoaded(); if (this.sourceCaches[id] === undefined) { - throw new Error('There no source with this ID'); + throw new Error('There is no source with this ID'); } + const sourceCache = this.sourceCaches[id]; sourceCache.setData(data); diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 86c5a9c79bd..a5944349c31 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -644,6 +644,64 @@ test('Style#removeSource', (t) => { t.end(); }); +test('Style#setData', (t) => { + t.test('throw before loaded', (t) => { + const style = new Style(createStyleJSON({ + "sources": { "source-id": createGeoJSONSource() } + }), new StubMap()); + const geoJSONSourceData = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } + } + ] + }; + t.throws(() => { + style.setData('source-id', geoJSONSourceData); + }, Error, /load/i); + style.on('style.load', () => { + t.end(); + }); + }); + + t.test('throws on non-existence', (t) => { + const style = new Style(createStyleJSON(), new StubMap()), + geoJSONSourceData = { type: "FeatureCollection", "features": [] }; + style.on('style.load', () => { + t.throws(() => { + style.setData('source-id', geoJSONSourceData); + }, /There is no source with this ID/); + t.end(); + }); + }); + + t.test('sets data of source', (t) => { + const style = new Style(createStyleJSON({ + sources: {'source-id': createGeoJSONSource()} + }), new StubMap()); + const geoJSONSourceData = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } + } + ] + }; + style.on('style.load', () => { + const sourceCache = style.sourceCaches['source-id']; + t.spy(sourceCache, 'setData'); + style.setData('source-id', geoJSONSourceData); + t.ok(sourceCache.setData.calledWith(geoJSONSourceData)); + t.end(); + }); + }); + + t.end(); +}); + test('Style#addLayer', (t) => { t.test('throw before loaded', (t) => { const style = new Style(createStyleJSON(), new StubMap()), From 5cf2dff084d736f050fe6da6b2ac6d25bacc677c Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Fri, 15 Sep 2017 18:32:07 -0700 Subject: [PATCH 3/8] diff test for setData --- test/unit/style-spec/diff.test.js | 30 ++++++++++++++++++++++++++++++ test/unit/style/style.test.js | 1 + 2 files changed, 31 insertions(+) diff --git a/test/unit/style-spec/diff.test.js b/test/unit/style-spec/diff.test.js index 22e9b9972f6..286edcc05e0 100644 --- a/test/unit/style-spec/diff.test.js +++ b/test/unit/style-spec/diff.test.js @@ -111,6 +111,36 @@ t('diff', (t) => { { command: 'addSource', args: ['foo', 1] } ], 'add a source'); + t.deepEqual(diffStyles({ + sources: { + foo: { + type: 'geojson', + data: { type: 'FeatureCollection', features: [] } + } + } + }, { + sources: { + foo: { + type: 'geojson', + data: { + type: 'FeatureCollection', + features: [{ + type: 'Feature', + geometry: { type: 'Point', coordinates: [10, 20] } + }] + } + } + } + }), [ + { command: 'setData', args: ['foo', { + type: 'FeatureCollection', + features: [{ + type: 'Feature', + geometry: { type: 'Point', coordinates: [10, 20] } + }] + }]} + ], 'update a geojson source'); + t.deepEqual(diffStyles({}, { metadata: { 'mapbox:author': 'nobody' } }), [], 'ignore style metadata'); diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index a5944349c31..665e86c25bf 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -405,6 +405,7 @@ test('Style#setState', (t) => { 'setFilter', 'addSource', 'removeSource', + 'setData', 'setLayerZoomRange', 'setLight' ].forEach((method) => t.stub(style, method).callsFake(() => t.fail(`${method} called`))); From 90c6b818241a41439fbfd4788895d29da23afbf4 Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Thu, 21 Sep 2017 16:35:29 -0700 Subject: [PATCH 4/8] remove from SourceCache and test uses setState --- src/source/source_cache.js | 6 --- src/style-spec/diff.js | 8 ++-- src/style/style.js | 22 ++++------ test/unit/style-spec/diff.test.js | 2 +- test/unit/style/style.test.js | 73 ++++++++++++++++++++----------- 5 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/source/source_cache.js b/src/source/source_cache.js index 8de0ba5aa8f..1dc68cdc77b 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -102,12 +102,6 @@ class SourceCache extends Evented { } } - setData(data: GeoJSON | string) { - if (this._source && this._source.setData) { - this._source.setData(data); - } - } - /** * Return true if no tile data is pending, tiles will not change unless * an additional API call is received. diff --git a/src/style-spec/diff.js b/src/style-spec/diff.js index 3fa36d8c6df..d9f6e100781 100644 --- a/src/style-spec/diff.js +++ b/src/style-spec/diff.js @@ -44,9 +44,9 @@ const operations = { removeSource: 'removeSource', /* - * { command: 'setData', args: ['sourceId', data] } + * { command: 'setGeoJSONSourceData', args: ['sourceId', data] } */ - setData: 'setData', + setGeoJSONSourceData: 'setGeoJSONSourceData', /* * { command: 'setLayerZoomRange', args: ['layerId', 0, 22] } @@ -123,8 +123,8 @@ function diffSources(before, after, commands, sourcesRemoved) { commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] }); } else if (!isEqual(before[sourceId], after[sourceId])) { if (before[sourceId].type === 'geojson' && after[sourceId].type === 'geojson') { - // geojson sources use setData command to update - commands.push({ command: operations.setData, args: [sourceId, after[sourceId].data] }); + // geojson sources use setGeoJSONSourceData command to update + commands.push({ command: operations.setGeoJSONSourceData, args: [sourceId, after[sourceId].data] }); } else { // no update command, must remove then add commands.push({ command: operations.removeSource, args: [sourceId] }); diff --git a/src/style/style.js b/src/style/style.js index 06c096febbc..5b15e6dec67 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -43,7 +43,7 @@ const supportedDiffOperations = util.pick(diff.operations, [ 'setLayerZoomRange', 'setLight', 'setTransition', - 'setData' + 'setGeoJSONSourceData' // 'setGlyphs', // 'setSprite', ]); @@ -502,24 +502,20 @@ class Style extends Evented { } /** - * Set the data of a source, given its id. Only available for GeoJSON sources. + * Set the data of a GeoJSON source, given its id. * @param {string} id id of the source * @param {GeoJSON|string} data GeoJSON source - * @throws {Error} if no source is found with the given ID */ - - setData(id: string, data: GeoJSON | string) { + setGeoJSONSourceData(id: string, data: GeoJSON | string) { this._checkLoaded(); - if (this.sourceCaches[id] === undefined) { - throw new Error('There is no source with this ID'); - } - + assert(this.sourceCaches[id] !== undefined, 'There is no source with this ID'); + const source = this.sourceCaches[id].getSource(); - const sourceCache = this.sourceCaches[id]; - - sourceCache.setData(data); - this._changed = true; + if (source.setData) { + source.setData(data); + this._changed = true; + } } /** diff --git a/test/unit/style-spec/diff.test.js b/test/unit/style-spec/diff.test.js index 286edcc05e0..1eb780459c8 100644 --- a/test/unit/style-spec/diff.test.js +++ b/test/unit/style-spec/diff.test.js @@ -132,7 +132,7 @@ t('diff', (t) => { } } }), [ - { command: 'setData', args: ['foo', { + { command: 'setGeoJSONSourceData', args: ['foo', { type: 'FeatureCollection', features: [{ type: 'Feature', diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 665e86c25bf..b9bc6ccf471 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -457,6 +457,49 @@ test('Style#setState', (t) => { }); }); + t.test('sets GeoJSON source data if different', (t) => { + const initialState = createStyleJSON({ + "sources": { "source-id": createGeoJSONSource() } + }); + + const geoJSONSourceData = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [125.6, 10.1] + } + } + ] + }; + + const nextState = createStyleJSON({ + "sources": { + "source-id": { + "type": "geojson", + "data": geoJSONSourceData + } + } + }); + + const style = new Style(initialState); + + style.on('style.load', () => { + const geoJSONSource = style.sourceCaches['source-id'].getSource(); + t.spy(style, 'setGeoJSONSourceData'); + t.spy(geoJSONSource, 'setData'); + const didChange = style.setState(nextState); + + t.ok(style.setGeoJSONSourceData.calledWith('source-id', geoJSONSourceData)); + t.ok(geoJSONSource.setData.calledWith(geoJSONSourceData)); + t.ok(didChange); + t.same(style.stylesheet, nextState); + t.end(); + }); + }); + t.end(); }); @@ -646,7 +689,7 @@ test('Style#removeSource', (t) => { }); test('Style#setData', (t) => { - t.test('throw before loaded', (t) => { + t.test('throws before loaded', (t) => { const style = new Style(createStyleJSON({ "sources": { "source-id": createGeoJSONSource() } }), new StubMap()); @@ -660,7 +703,7 @@ test('Style#setData', (t) => { ] }; t.throws(() => { - style.setData('source-id', geoJSONSourceData); + style.setGeoJSONSourceData('source-id', geoJSONSourceData); }, Error, /load/i); style.on('style.load', () => { t.end(); @@ -672,30 +715,8 @@ test('Style#setData', (t) => { geoJSONSourceData = { type: "FeatureCollection", "features": [] }; style.on('style.load', () => { t.throws(() => { - style.setData('source-id', geoJSONSourceData); - }, /There is no source with this ID/); - t.end(); - }); - }); - - t.test('sets data of source', (t) => { - const style = new Style(createStyleJSON({ - sources: {'source-id': createGeoJSONSource()} - }), new StubMap()); - const geoJSONSourceData = { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } - } - ] - }; - style.on('style.load', () => { - const sourceCache = style.sourceCaches['source-id']; - t.spy(sourceCache, 'setData'); - style.setData('source-id', geoJSONSourceData); - t.ok(sourceCache.setData.calledWith(geoJSONSourceData)); + style.setGeoJSONSourceData('source-id', geoJSONSourceData); + }, Error, /There is no source with this ID/); t.end(); }); }); From 237d751a30ecb1d7b481f50560124467a114a20a Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Thu, 21 Sep 2017 16:35:29 -0700 Subject: [PATCH 5/8] remove from SourceCache and test uses setState --- src/source/source_cache.js | 6 --- src/style-spec/diff.js | 8 ++-- src/style/style.js | 22 ++++----- test/unit/style-spec/diff.test.js | 2 +- test/unit/style/style.test.js | 75 ++++++++++++++++++++----------- 5 files changed, 62 insertions(+), 51 deletions(-) diff --git a/src/source/source_cache.js b/src/source/source_cache.js index 8de0ba5aa8f..1dc68cdc77b 100644 --- a/src/source/source_cache.js +++ b/src/source/source_cache.js @@ -102,12 +102,6 @@ class SourceCache extends Evented { } } - setData(data: GeoJSON | string) { - if (this._source && this._source.setData) { - this._source.setData(data); - } - } - /** * Return true if no tile data is pending, tiles will not change unless * an additional API call is received. diff --git a/src/style-spec/diff.js b/src/style-spec/diff.js index 3fa36d8c6df..d9f6e100781 100644 --- a/src/style-spec/diff.js +++ b/src/style-spec/diff.js @@ -44,9 +44,9 @@ const operations = { removeSource: 'removeSource', /* - * { command: 'setData', args: ['sourceId', data] } + * { command: 'setGeoJSONSourceData', args: ['sourceId', data] } */ - setData: 'setData', + setGeoJSONSourceData: 'setGeoJSONSourceData', /* * { command: 'setLayerZoomRange', args: ['layerId', 0, 22] } @@ -123,8 +123,8 @@ function diffSources(before, after, commands, sourcesRemoved) { commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] }); } else if (!isEqual(before[sourceId], after[sourceId])) { if (before[sourceId].type === 'geojson' && after[sourceId].type === 'geojson') { - // geojson sources use setData command to update - commands.push({ command: operations.setData, args: [sourceId, after[sourceId].data] }); + // geojson sources use setGeoJSONSourceData command to update + commands.push({ command: operations.setGeoJSONSourceData, args: [sourceId, after[sourceId].data] }); } else { // no update command, must remove then add commands.push({ command: operations.removeSource, args: [sourceId] }); diff --git a/src/style/style.js b/src/style/style.js index 06c096febbc..5b15e6dec67 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -43,7 +43,7 @@ const supportedDiffOperations = util.pick(diff.operations, [ 'setLayerZoomRange', 'setLight', 'setTransition', - 'setData' + 'setGeoJSONSourceData' // 'setGlyphs', // 'setSprite', ]); @@ -502,24 +502,20 @@ class Style extends Evented { } /** - * Set the data of a source, given its id. Only available for GeoJSON sources. + * Set the data of a GeoJSON source, given its id. * @param {string} id id of the source * @param {GeoJSON|string} data GeoJSON source - * @throws {Error} if no source is found with the given ID */ - - setData(id: string, data: GeoJSON | string) { + setGeoJSONSourceData(id: string, data: GeoJSON | string) { this._checkLoaded(); - if (this.sourceCaches[id] === undefined) { - throw new Error('There is no source with this ID'); - } - + assert(this.sourceCaches[id] !== undefined, 'There is no source with this ID'); + const source = this.sourceCaches[id].getSource(); - const sourceCache = this.sourceCaches[id]; - - sourceCache.setData(data); - this._changed = true; + if (source.setData) { + source.setData(data); + this._changed = true; + } } /** diff --git a/test/unit/style-spec/diff.test.js b/test/unit/style-spec/diff.test.js index 286edcc05e0..1eb780459c8 100644 --- a/test/unit/style-spec/diff.test.js +++ b/test/unit/style-spec/diff.test.js @@ -132,7 +132,7 @@ t('diff', (t) => { } } }), [ - { command: 'setData', args: ['foo', { + { command: 'setGeoJSONSourceData', args: ['foo', { type: 'FeatureCollection', features: [{ type: 'Feature', diff --git a/test/unit/style/style.test.js b/test/unit/style/style.test.js index 665e86c25bf..101ffe785e5 100644 --- a/test/unit/style/style.test.js +++ b/test/unit/style/style.test.js @@ -405,7 +405,7 @@ test('Style#setState', (t) => { 'setFilter', 'addSource', 'removeSource', - 'setData', + 'setGeoJSONSourceData', 'setLayerZoomRange', 'setLight' ].forEach((method) => t.stub(style, method).callsFake(() => t.fail(`${method} called`))); @@ -457,6 +457,49 @@ test('Style#setState', (t) => { }); }); + t.test('sets GeoJSON source data if different', (t) => { + const initialState = createStyleJSON({ + "sources": { "source-id": createGeoJSONSource() } + }); + + const geoJSONSourceData = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [125.6, 10.1] + } + } + ] + }; + + const nextState = createStyleJSON({ + "sources": { + "source-id": { + "type": "geojson", + "data": geoJSONSourceData + } + } + }); + + const style = new Style(initialState); + + style.on('style.load', () => { + const geoJSONSource = style.sourceCaches['source-id'].getSource(); + t.spy(style, 'setGeoJSONSourceData'); + t.spy(geoJSONSource, 'setData'); + const didChange = style.setState(nextState); + + t.ok(style.setGeoJSONSourceData.calledWith('source-id', geoJSONSourceData)); + t.ok(geoJSONSource.setData.calledWith(geoJSONSourceData)); + t.ok(didChange); + t.same(style.stylesheet, nextState); + t.end(); + }); + }); + t.end(); }); @@ -646,7 +689,7 @@ test('Style#removeSource', (t) => { }); test('Style#setData', (t) => { - t.test('throw before loaded', (t) => { + t.test('throws before loaded', (t) => { const style = new Style(createStyleJSON({ "sources": { "source-id": createGeoJSONSource() } }), new StubMap()); @@ -660,7 +703,7 @@ test('Style#setData', (t) => { ] }; t.throws(() => { - style.setData('source-id', geoJSONSourceData); + style.setGeoJSONSourceData('source-id', geoJSONSourceData); }, Error, /load/i); style.on('style.load', () => { t.end(); @@ -672,30 +715,8 @@ test('Style#setData', (t) => { geoJSONSourceData = { type: "FeatureCollection", "features": [] }; style.on('style.load', () => { t.throws(() => { - style.setData('source-id', geoJSONSourceData); - }, /There is no source with this ID/); - t.end(); - }); - }); - - t.test('sets data of source', (t) => { - const style = new Style(createStyleJSON({ - sources: {'source-id': createGeoJSONSource()} - }), new StubMap()); - const geoJSONSourceData = { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } - } - ] - }; - style.on('style.load', () => { - const sourceCache = style.sourceCaches['source-id']; - t.spy(sourceCache, 'setData'); - style.setData('source-id', geoJSONSourceData); - t.ok(sourceCache.setData.calledWith(geoJSONSourceData)); + style.setGeoJSONSourceData('source-id', geoJSONSourceData); + }, Error, /There is no source with this ID/); t.end(); }); }); From ebae7f3013b1fba9d3649b9a4c5c739d8aab2fbe Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Fri, 22 Sep 2017 11:04:32 -0700 Subject: [PATCH 6/8] remove leftover --- src/source/source.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/source/source.js b/src/source/source.js index b627b0c980a..4bfa9eeef23 100644 --- a/src/source/source.js +++ b/src/source/source.js @@ -57,7 +57,6 @@ export interface Source { +onAdd?: (map: Map) => void; +onRemove?: (map: Map) => void; - +setData?: (data: GeoJSON | string) => Source; loadTile(tile: Tile, callback: Callback): void; +hasTile?: (coord: TileCoord) => boolean; From 6e1a1ba6143d49b06142c8cbe623d7c1a691a71e Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Fri, 22 Sep 2017 11:57:03 -0700 Subject: [PATCH 7/8] add setData back to Source interface --- src/source/source.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/source/source.js b/src/source/source.js index 4bfa9eeef23..b627b0c980a 100644 --- a/src/source/source.js +++ b/src/source/source.js @@ -57,6 +57,7 @@ export interface Source { +onAdd?: (map: Map) => void; +onRemove?: (map: Map) => void; + +setData?: (data: GeoJSON | string) => Source; loadTile(tile: Tile, callback: Callback): void; +hasTile?: (coord: TileCoord) => boolean; From c0f9a92b511ca08e9fc6f3435092f93068dd5f98 Mon Sep 17 00:00:00 2001 From: Zach Ernst Date: Fri, 22 Sep 2017 13:41:43 -0700 Subject: [PATCH 8/8] add flow check and validation to Style#setGeoJSONSourceData --- src/source/source.js | 1 - src/style/style.js | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/source/source.js b/src/source/source.js index b627b0c980a..4bfa9eeef23 100644 --- a/src/source/source.js +++ b/src/source/source.js @@ -57,7 +57,6 @@ export interface Source { +onAdd?: (map: Map) => void; +onRemove?: (map: Map) => void; - +setData?: (data: GeoJSON | string) => Source; loadTile(tile: Tile, callback: Callback): void; +hasTile?: (coord: TileCoord) => boolean; diff --git a/src/style/style.js b/src/style/style.js index 5b15e6dec67..ade3e56911f 100644 --- a/src/style/style.js +++ b/src/style/style.js @@ -19,6 +19,7 @@ const getSourceType = require('../source/source').getType; const setSourceType = require('../source/source').setType; const QueryFeatures = require('../source/query_features'); const SourceCache = require('../source/source_cache'); +const GeoJSONSource = require('../source/geojson_source'); const styleSpec = require('../style-spec/reference/latest'); const MapboxGLFunction = require('../style-spec/function'); const getWorkerPool = require('../util/global_worker_pool'); @@ -510,12 +511,11 @@ class Style extends Evented { this._checkLoaded(); assert(this.sourceCaches[id] !== undefined, 'There is no source with this ID'); - const source = this.sourceCaches[id].getSource(); + const geojsonSource: GeoJSONSource = (this.sourceCaches[id].getSource(): any); + assert(geojsonSource.type === 'geojson'); - if (source.setData) { - source.setData(data); - this._changed = true; - } + geojsonSource.setData(data); + this._changed = true; } /**