-
Notifications
You must be signed in to change notification settings - Fork 26
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
Improve layer load #623
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35775b2
Load map depending on the data type of the event
elenatorro 2a6bcb7
Fix specs
elenatorro 2f9a9da
Add specs to test when dataType is source
elenatorro 5e21827
Listen both load and sourcedata event
elenatorro d22c842
Fix typo
elenatorro 9ec494d
Remove unused variable
elenatorro 462dc1f
Revert changes and use map._loaded to add a layer
elenatorro 380aaff
Merge branch 'master' into 604-improve-layer-load
elenatorro baa2abe
Load the map if the styles have been properly loaded
elenatorro 4cffafe
Remove redundant check
elenatorro cf34620
Remove unnecessary checks
elenatorro f706ab4
Merge branch 'master' into 604-improve-layer-load
elenatorro 9b78363
Do not use bind
elenatorro 0d97a26
Check the given error
elenatorro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,51 +131,104 @@ describe('api/layer', () => { | |
|
||
describe('.addTo', () => { | ||
describe('._addToMGLMap', () => { | ||
let layer; | ||
beforeEach(() => { | ||
layer = new Layer('layer0', source, viz); | ||
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); | ||
}); | ||
|
||
it('should not call onMapLoaded when the map is not loaded', () => { | ||
const mapMock = { isStyleLoaded: () => false, on: () => { } }; | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onMapLoaded when the map `load` event is triggered', () => { | ||
describe('and dataType is style', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when datatype is style ? |
||
|
||
let layer; | ||
let eventId = 'data'; | ||
const event = { | ||
dataType: 'style' | ||
}; | ||
|
||
const mapMock = { | ||
isStyleLoaded: () => false, | ||
on: (id, callback) => { | ||
if (id === 'load') { | ||
callback(); | ||
isStyleLoaded: jasmine.createSpy('isStyleLoaded').and.returnValue(true), | ||
areTilesLoaded: jasmine.createSpy('areTilesLoaded').and.returnValue(true), | ||
once: (id, callback) => { | ||
if (id === eventId) { | ||
callback(event); | ||
} | ||
} | ||
}; | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).toHaveBeenCalledWith(mapMock, undefined); | ||
|
||
beforeEach(() => { | ||
layer = new Layer('layer0', source, viz); | ||
layer._onMapLoaded = () => { }; | ||
spyOn(layer, '_onMapLoaded'); | ||
}); | ||
|
||
it('should call onMapLoaded when the map is loaded', () => { | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onMapLoaded when the map is not loaded', () => { | ||
mapMock.isStyleLoaded = jasmine.createSpy('isStyleLoaded').and.returnValue(false), | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onMapLoaded when the map `data` event is triggered', () => { | ||
mapMock.isStyleLoaded = jasmine.createSpy('isStyleLoaded').and.returnValue(true), | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onMapLoaded when other map event is triggered', () => { | ||
eventId = 'other'; | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should not call onMapLoaded when other the map event is triggered', () => { | ||
describe('and dataType is source', () => { | ||
|
||
let layer; | ||
let eventId = 'data'; | ||
const event = { | ||
dataType: 'source' | ||
}; | ||
|
||
const mapMock = { | ||
isStyleLoaded: () => false, | ||
on: (id, callback) => { | ||
if (id === 'other') { | ||
callback(); | ||
isStyleLoaded: jasmine.createSpy('isStyleLoaded').and.returnValue(false), | ||
areTilesLoaded: jasmine.createSpy('areTilesLoaded').and.returnValue(true), | ||
once: (id, callback) => { | ||
if (id === eventId) { | ||
callback(event); | ||
} | ||
} | ||
}; | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
|
||
beforeEach(() => { | ||
layer = new Layer('layer0', source, viz); | ||
layer._onMapLoaded = () => { }; | ||
spyOn(layer, '_onMapLoaded'); | ||
}); | ||
|
||
it('should call onMapLoaded when the map is loaded', () => { | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onMapLoaded when the map is not loaded', () => { | ||
mapMock.areTilesLoaded = jasmine.createSpy('areTilesLoaded').and.returnValue(false), | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onMapLoaded when the map `data` event is triggered', () => { | ||
mapMock.areTilesLoaded = jasmine.createSpy('areTilesLoaded').and.returnValue(true), | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call onMapLoaded when other map event is triggered', () => { | ||
eventId = 'other'; | ||
layer._addToMGLMap(mapMock); | ||
expect(layer._onMapLoaded).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
}); | ||
|
||
describe('.getFeaturesAtPosition', () => { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why does this work? Does it work? I thought that MGL required the style to be loaded before the
addLayer
method worksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MGL requires the style to be loaded if the data event type is sytle. About the
data
event, after reading the documentation I think it is the only change that needs to be listened, and only the first time (that is why I am usingonce
).https://www.mapbox.com/mapbox-gl-js/api/#map.event:data
The examples in the editor work, but I agree it needs further testing.