Skip to content

Commit

Permalink
add Map#isSourceLoaded #3691 (#4033)
Browse files Browse the repository at this point in the history
* add Map#isSourceLoaded #3691

* fix the case then Map is constructed without a style and throw an error instead of firing event #3691

* fire an error on checking if non-existant source is loaded #3691
  • Loading branch information
stepankuzmin authored and anandthakker committed Jan 23, 2017
1 parent 2b61547 commit 2793b42
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions js/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,23 @@ class Map extends Camera {
return this;
}

/**
* Returns a Boolean indicating whether the source is loaded.
*
* @param {string} id The ID of the source to be checked.
* @returns {boolean} A Boolean indicating whether the source is loaded.
*/
isSourceLoaded(id) {
const source = this.style && this.style.sourceCaches[id];
if (source === undefined) {
this.fire('error', {
error: new Error(`There is no source with ID '${id}'`)
});
return;
}
return source.loaded();
}

/**
* Adds a [custom source type](#Custom Sources), making it available for use with
* {@link Map#addSource}.
Expand Down
27 changes: 27 additions & 0 deletions test/js/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,33 @@ test('Map', (t) => {
});
});

t.test('fires an error on checking if non-existant source is loaded', (t) => {
const style = createStyle();
const map = createMap({style: style});

map.on('load', () => {
map.on('error', ({ error }) => {
t.match(error.message, /There is no source with ID/);
t.end();
});
map.isSourceLoaded('geojson');
});
});

t.test('#isSourceLoaded', (t) => {
const style = createStyle();
const map = createMap({style: style});

map.on('load', () => {
map.on('source.load', () => {
t.equal(map.isSourceLoaded('geojson'), true, 'true when loaded');
t.end();
});
map.addSource('geojson', createStyleSource());
t.equal(map.isSourceLoaded('geojson'), false, 'false before loaded');
});
});

t.test('returns the style with added layers', (t) => {
const style = createStyle();
const map = createMap({style: style});
Expand Down

0 comments on commit 2793b42

Please sign in to comment.