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

Improve layer load #623

Merged
merged 14 commits into from
Jul 4, 2018
19 changes: 14 additions & 5 deletions src/api/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,21 @@ export default class Layer {
}

_addToMGLMap(map, beforeLayerID) {
if (map.isStyleLoaded()) {
this._isSourceLoaded = false;

map.on('sourcedata', this._onMapSourcedata);
Copy link
Contributor

Choose a reason for hiding this comment

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

dont we need to bind this here?

map.on('load', this._onMapLoad.bind(this, map, beforeLayerID));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think

map.on('load', this._onMapLoad.bind(this));

is more intuitive since we only bound the context but the parameters

}

_onMapSourcedata(event) {
this._isSourceLoaded = event.isSourceLoaded;
}

_onMapLoad(map, beforeLayerID) {
if (map.isStyleLoaded() || this._isSourceLoaded) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this._isSourceLoaded is always undefined because this (pun intended) :trollface:

map.off('sourcedata', this._onMapSourcedata);
map.off('load', this._onMapLoad);
this._onMapLoaded(map, beforeLayerID);
} else {
map.on('load', () => {
this._onMapLoaded(map, beforeLayerID);
});
}
}

Expand Down
60 changes: 35 additions & 25 deletions test/unit/api/layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,48 +132,58 @@ describe('api/layer', () => {
describe('.addTo', () => {
describe('._addToMGLMap', () => {
let layer;

const sourcedataEvent = {
isSourceLoaded: false
};

const loadEvent = {};

const mapMock = {
isStyleLoaded: jasmine.createSpy('isStyleLoaded').and.returnValue(true),
on: (id, callback) => {
if (id === 'load') {
callback.call(layer, loadEvent);
}

if (id === 'sourcedata') {
callback.call(layer, sourcedataEvent);
}
},

off: () => {}
};

beforeEach(() => {
layer = new Layer('layer0', source, viz);
layer._onMapLoaded = () => { };
layer._onMapLoaded = () => {};
spyOn(layer, '_onMapLoaded');
});

it('should call onMapLoaded when the map is loaded', () => {
const mapMock = { isStyleLoaded: () => true };
layer._addToMGLMap(mapMock);
expect(layer._onMapLoaded).toHaveBeenCalledWith(mapMock, undefined);
expect(layer._onMapLoaded).toHaveBeenCalled();
});

it('should not call onMapLoaded when the map is not loaded', () => {
const mapMock = { isStyleLoaded: () => false, on: () => { } };
it('should not call onMapLoaded when the map style and source are not loaded', () => {
sourcedataEvent.isSourceLoaded = false;
mapMock.isStyleLoaded = jasmine.createSpy('isStyleLoaded').and.returnValue(false),
layer._addToMGLMap(mapMock);
expect(layer._onMapLoaded).not.toHaveBeenCalled();
});

it('should call onMapLoaded when the map `load` event is triggered', () => {
const mapMock = {
isStyleLoaded: () => false,
on: (id, callback) => {
if (id === 'load') {
callback();
}
}
};
it('should call onMapLoaded when the style is loaded but the sourcedata is not', () => {
sourcedataEvent.isSourceLoaded = false;
mapMock.isStyleLoaded = jasmine.createSpy('isStyleLoaded').and.returnValue(true),
layer._addToMGLMap(mapMock);
expect(layer._onMapLoaded).toHaveBeenCalledWith(mapMock, undefined);
expect(layer._onMapLoaded).toHaveBeenCalled();
});

it('should not call onMapLoaded when other the map event is triggered', () => {
const mapMock = {
isStyleLoaded: () => false,
on: (id, callback) => {
if (id === 'other') {
callback();
}
}
};
it('should call onMapLoaded when the style is not loaded and the sourcedata is', () => {
sourcedataEvent.isSourceLoaded = true;
mapMock.isStyleLoaded = jasmine.createSpy('isStyleLoaded').and.returnValue(false),
layer._addToMGLMap(mapMock);
expect(layer._onMapLoaded).not.toHaveBeenCalled();
expect(layer._onMapLoaded).toHaveBeenCalled();
});
});
});
Expand Down