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

Wait to start loading source assets until onAdd is called #3761

Merged
merged 5 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion js/source/geojson_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class GeoJSONSource extends Evented {
this.reparseOverscaled = true;

this.dispatcher = dispatcher;
this.setEventedParent(eventedParent);

this._data = options.data;

Expand Down Expand Up @@ -98,8 +99,9 @@ class GeoJSONSource extends Evented {
log: false
}
}, options.workerOptions);
}

this.setEventedParent(eventedParent);
load() {
this.fire('dataloading', {dataType: 'source'});
this._updateWorkerData((err) => {
if (err) {
Expand All @@ -112,6 +114,7 @@ class GeoJSONSource extends Evented {
}

onAdd(map) {
this.load();
this.map = map;
}

Expand Down
12 changes: 7 additions & 5 deletions js/source/image_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ class ImageSource extends Evented {
this.tileSize = 512;

this.setEventedParent(eventedParent);
this.fire('dataloading', {dataType: 'source'});

this._load(options);
this.options = options;
}

_load(options) {
this.url = options.url;
load() {
this.fire('dataloading', {dataType: 'source'});

this.url = this.options.url;

ajax.getImage(options.url, (err, image) => {
ajax.getImage(this.options.url, (err, image) => {
if (err) return this.fire('error', {error: err});

this.image = image;
Expand All @@ -81,6 +82,7 @@ class ImageSource extends Evented {
}

onAdd(map) {
this.load();
this.map = map;
if (this.image) {
this.setCoordinates(this.coordinates);
Expand Down
8 changes: 6 additions & 2 deletions js/source/raster_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ class RasterTileSource extends Evented {
super();
this.id = id;
this.dispatcher = dispatcher;
this.setEventedParent(eventedParent);

this.minzoom = 0;
this.maxzoom = 22;
this.roundZoom = true;
this.scheme = 'xyz';
this.tileSize = 512;
this._loaded = false;
this.options = options;
util.extend(this, util.pick(options, ['url', 'scheme', 'tileSize']));
}

this.setEventedParent(eventedParent);
load() {
this.fire('dataloading', {dataType: 'source'});
loadTileJSON(options, (err, tileJSON) => {
loadTileJSON(this.options, (err, tileJSON) => {
if (err) {
return this.fire('error', err);
}
Expand All @@ -34,6 +37,7 @@ class RasterTileSource extends Evented {
}

onAdd(map) {
this.load();
this.map = map;
}

Expand Down
1 change: 0 additions & 1 deletion js/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const sourceTypes = {
*/
exports.create = function(id, source, dispatcher, eventedParent) {
source = new sourceTypes[source.type](id, source, dispatcher, eventedParent);
source.setEventedParent(eventedParent);

if (source.id !== id) {
throw new Error(`Expected Source id to be ${id} instead of ${source.id}`);
Expand Down
5 changes: 2 additions & 3 deletions js/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class SourceCache extends Evented {
this.id = id;
this.dispatcher = dispatcher;

this._source = Source.create(id, options, dispatcher, this);

this.on('source.load', function() {
if (this.map && this._source.onAdd) { this._source.onAdd(this.map); }
this._sourceLoaded = true;
});

Expand All @@ -47,6 +44,8 @@ class SourceCache extends Evented {
}
});

this._source = Source.create(id, options, dispatcher, this);

this._tiles = {};
this._cache = new Cache(0, this.unloadTile.bind(this));

Expand Down
6 changes: 5 additions & 1 deletion js/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class VectorTileSource extends Evented {
}

this.setEventedParent(eventedParent);
}

load() {
this.fire('dataloading', {dataType: 'source'});

loadTileJSON(options, (err, tileJSON) => {
loadTileJSON(this._options, (err, tileJSON) => {
if (err) {
this.fire('error', err);
return;
Expand All @@ -42,6 +45,7 @@ class VectorTileSource extends Evented {
}

onAdd(map) {
this.load();
this.map = map;
}

Expand Down
5 changes: 4 additions & 1 deletion js/source/video_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class VideoSource extends ImageSource {
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
this.roundZoom = true;
this.options = options;
}

_load(options) {
load() {
const options = this.options;
this.urls = options.urls;

ajax.getVideo(options.urls, (err, video) => {
Expand Down Expand Up @@ -82,6 +84,7 @@ class VideoSource extends ImageSource {

onAdd(map) {
if (this.map) return;
this.load();
this.map = map;
if (this.video) {
this.video.play();
Expand Down
3 changes: 2 additions & 1 deletion js/style/image_sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class SpritePosition {

class ImageSprite extends Evented {

constructor(base) {
constructor(base, eventedParent) {
super();
this.base = base;
this.retina = browser.devicePixelRatio > 1;
this.setEventedParent(eventedParent);

const format = this.retina ? '@2x' : '';

Expand Down
3 changes: 1 addition & 2 deletions js/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ class Style extends Evented {
}

if (stylesheet.sprite) {
this.sprite = new ImageSprite(stylesheet.sprite);
this.sprite.setEventedParent(this);
this.sprite = new ImageSprite(stylesheet.sprite, this);
}

this.glyphSource = new GlyphSource(stylesheet.glyphs);
Expand Down
15 changes: 13 additions & 2 deletions test/js/source/geojson_source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ test('GeoJSONSource#setData', (t) => {
source.on('data', t.end);
source.setData({});
});
source.load();
});

t.test('fires "dataloading" event', (t) => {
Expand All @@ -74,6 +75,7 @@ test('GeoJSONSource#setData', (t) => {
source.on('dataloading', t.end);
source.setData({});
});
source.load();
});

t.end();
Expand Down Expand Up @@ -115,7 +117,7 @@ test('GeoJSONSource#update', (t) => {
};

/* eslint-disable no-new */
new GeoJSONSource('id', {data: {}}, mockDispatcher);
new GeoJSONSource('id', {data: {}}, mockDispatcher).load();
});

t.test('forwards geojson-vt options with worker request', (t) => {
Expand All @@ -137,7 +139,7 @@ test('GeoJSONSource#update', (t) => {
maxzoom: 10,
tolerance: 0.25,
buffer: 16
}, mockDispatcher);
}, mockDispatcher).load();
});

t.test('fires "source.load"', (t) => {
Expand All @@ -152,6 +154,8 @@ test('GeoJSONSource#update', (t) => {
source.on('source.load', () => {
t.end();
});

source.load();
});

t.test('fires "error"', (t) => {
Expand All @@ -167,6 +171,8 @@ test('GeoJSONSource#update', (t) => {
t.equal(err.error, 'error');
t.end();
});

source.load();
});

t.test('sends loadData request to dispatcher after data update', (t) => {
Expand All @@ -189,6 +195,8 @@ test('GeoJSONSource#update', (t) => {
source.setData({});
source.loadTile(new Tile(new TileCoord(0, 0, 0), 512), () => {});
});

source.load();
});

t.end();
Expand All @@ -198,6 +206,7 @@ test('GeoJSONSource#serialize', (t) => {

t.test('serialize source with inline data', (t) => {
const source = new GeoJSONSource('id', {data: hawkHill}, mockDispatcher);
source.load();
t.deepEqual(source.serialize(), {
type: 'geojson',
data: hawkHill
Expand All @@ -207,6 +216,7 @@ test('GeoJSONSource#serialize', (t) => {

t.test('serialize source with url', (t) => {
const source = new GeoJSONSource('id', {data: 'local://data.json'}, mockDispatcher);
source.load();
t.deepEqual(source.serialize(), {
type: 'geojson',
data: 'local://data.json'
Expand All @@ -216,6 +226,7 @@ test('GeoJSONSource#serialize', (t) => {

t.test('serialize source with updated data', (t) => {
const source = new GeoJSONSource('id', {data: {}}, mockDispatcher);
source.load();
source.setData(hawkHill);
t.deepEqual(source.serialize(), {
type: 'geojson',
Expand Down
Loading