-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
Move addSourceType
to global object
#3420
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5fd0053
Move add source type to global object
HarelM 3a48152
Update CHANGELOG
HarelM 47931db
Fix docs
HarelM 763f15d
Merge branch 'main' into move-add-custom-source-to-global
HarelM 473a578
Merge branch 'main' into move-add-custom-source-to-global
HarelM 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Dispatcher} from '../util/dispatcher'; | ||
import {SourceClass, addSourceType, create} from './source'; | ||
|
||
describe('addSourceType', () => { | ||
test('adds factory function without a worker url does not dispatch to worker', async () => { | ||
const sourceType = jest.fn().mockImplementation(function (id) { this.id = id; }) as SourceClass; | ||
|
||
// expect no call to load worker source | ||
const spy = jest.spyOn(Dispatcher.prototype, 'broadcast'); | ||
|
||
await addSourceType('foo', sourceType); | ||
expect(spy).not.toHaveBeenCalled(); | ||
|
||
create('id', {type: 'foo'} as any, null, null); | ||
expect(sourceType).toHaveBeenCalled(); | ||
}); | ||
|
||
test('create a custom source without an id throws', async () => { | ||
const sourceType = jest.fn() as SourceClass; | ||
|
||
// expect no call to load worker source | ||
const spy = jest.spyOn(Dispatcher.prototype, 'broadcast'); | ||
|
||
await addSourceType('foo2', sourceType); | ||
expect(spy).not.toHaveBeenCalled(); | ||
|
||
expect(() => create('id', {type: 'foo2'} as any, null, null)).toThrow(); | ||
expect(sourceType).toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers workers to load worker source code', async () => { | ||
const sourceType = function () {} as any as SourceClass; | ||
sourceType.workerSourceURL = 'worker-source.js' as any as URL; | ||
|
||
const spy = jest.spyOn(Dispatcher.prototype, 'broadcast'); | ||
|
||
await addSourceType('bar', sourceType); | ||
expect(spy).toHaveBeenCalledWith('loadWorkerSource', 'worker-source.js'); | ||
}); | ||
|
||
test('refuses to add new type over existing name', async () => { | ||
const sourceType = function () {} as any as SourceClass; | ||
await expect(addSourceType('canvas', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('geojson', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('image', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('raster', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('raster-dem', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('vector', sourceType)).rejects.toThrow(); | ||
await expect(addSourceType('video', sourceType)).rejects.toThrow(); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.
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.
Should we expose getSourceType here as well? Seems like we should.
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.
sounds right, but what would you use if for?
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.
One thing that comes to mind is allowing the source type to be changed at runtime. Allowing retrieval of the type would allow for that, without having to keep track of it outside of MapLibre. Of course, I realize changing some of the things about a source type at runtime would be dangerous...
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.
If anything, remove would be the opposite of add. but I'm not sure how to "remove" code that was added to the worker.
And yes, changing a source is super risky...
I'm not sure either way... I think this API isn't really in use as it is so complicated to get it right...
In general you can mess with the prototype of the source object without the need for this method basically.
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.
Yes, understood. I'm also perfectly fine if we leave it as-is for now. Just food for thought.