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

Revise addSourceType to be independent of Map instance #2982

Closed
wants to merge 12 commits into from

Conversation

anandthakker
Copy link
Contributor

@anandthakker anandthakker commented Aug 11, 2016

Closes #3004

The problem: addSourceType does not quite make sense as a method on the Map (or Style) instance, because: a) Sources, being types, are really 'global'; b) practically speaking, there are problems having this as an instance method, because that approach fails to coordinate setting up the WorkerSources for other map instances.

Changes:

  • Replace Map#addSourceType and Style#addSourceType with the static Source.addType -- exported as mapboxgl.addSourceType. Leveraging the global worker pool, this method can now directly take care of registering the WorkerSource for the added source type if necessary.

Checklist

@anandthakker
Copy link
Contributor Author

I'm going to revise this a bit so that it can address #3004

@anandthakker anandthakker changed the title Allow multiple addSourceType calls with same name Revise addSourceType to be independent of Map instance Aug 15, 2016
@anandthakker
Copy link
Contributor Author

@lucaswoj ^ revised this to be a more comprehensive solution. Ready for your 👀

@anandthakker
Copy link
Contributor Author

(also edited the top of the ticket)

anandthakker pushed a commit to developmentseed/mapbox-gl-topojson that referenced this pull request Aug 15, 2016
@anandthakker
Copy link
Contributor Author

Here is a working example of the API that this change affords: https://github.com/developmentseed/mapbox-gl-topojson/blob/gh-pages/site.js

anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 16, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 16, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 16, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 16, 2016
Revise addSourceType to be independent of Map instance
@@ -202,6 +203,11 @@ var Map = module.exports = function(options) {
this.resize();

if (options.classes) this.setClasses(options.classes);
if (options.sourceTypes) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we name it customSourceTypes to reflect that we supply those additionally to the core ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me, but I also don't have a strong opinion on this.

@mourner
Copy link
Member

mourner commented Aug 17, 2016

This generally looks good to me!

anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 17, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 18, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 18, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 19, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 19, 2016
Revise addSourceType to be independent of Map instance
@anandthakker
Copy link
Contributor Author

@mourner @lucaswoj added one more small change in the last commit. I believe this is ready to merge.

anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 19, 2016
Revise addSourceType to be independent of Map instance
anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Aug 20, 2016
Revise addSourceType to be independent of Map instance
@@ -14,6 +14,7 @@ mapboxgl.Popup = require('./ui/popup');
mapboxgl.Marker = require('./ui/marker');

mapboxgl.Style = require('./style/style');
mapboxgl.Source = require('./source/source');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we expose mapboxgl.addSourceType instead of the whole Source module? There are lots of bits & pieces in Source that we don't want to be public.

@anandthakker
Copy link
Contributor Author

anandthakker commented Aug 24, 2016

Rebased, and updated to leverage the global worker pool.

Two key notes:

  1. Note the fix in 8a7807f -- this isn't really the topic of this PR (it's actually a followup to Add a minimal global worker pool #2952 ), but the issue got in my way while I was here so I figured I'd include the fix.
  2. There is an outstanding TODO here See this note about workers being created (and not destroyed) when a custom worker source is added. I think this is "good enough" for now, because (a) it is only relevant when a custom source with WorkerSource is being used and all of the map instances on the page are being remove()ed; (b) I believe this would require acrobatics like making Dispatcher-creation asynchronous and giving WorkerPool knowledge of custom sources and (c) I think Refactor "Worker", "Dispatcher", "Actor" into a general purpose "PooledWorker" utility #3034 will afford a much cleaner way to handle this anyway, so why introduce ugly code that's soon to be obsolete?

@anandthakker
Copy link
Contributor Author

@lucaswoj this should be good to go. If you have reservations about this PR or it needs more discussion, it might be worth pulling out 8a7807f into a separate one to merge sooner

anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Sep 2, 2016
Revise addSourceType to be independent of Map instance
@anandthakker
Copy link
Contributor Author

Rebased

@lucaswoj
Copy link
Contributor

lucaswoj commented Sep 14, 2016

I'm coming to see the wisdom behind GL Native's custom layer architecture. Instead of defining custom source types, we should accept custom source objects which conform to a given interface specification.

map.addCustomSource('foo', new WhizzBangSource({...}))

The benefits of this approach include

  • no need to worry about interactions between the style spec validator and custom sources
  • the ability to pass functions to custom sources (not allowed by the style spec)
  • smaller API surface / less boilerplate required to create a custom source
  • compatibility with the planned custom layer interface Support custom layer types powered by WebGL  #281
  • compatibility with GL Native's custom layer interface

One downside is that we'll need to find a way to register worker-side logic without the existence of source "types". Does this seem doable @anandthakker? Would it be more doable if we implemented #3034?

Overall, does implementing this architecture sound doable within the scope of this PR?

@anandthakker
Copy link
Contributor Author

@lucaswoj This is interesting! I actually don't think it would be too difficult to deal with registering worker-side code; we'd just need an API like mapboxgl.registerWorkerSource( url ), where the URL would be to a script pretty similar to that which is at present provided by CustomSourceType.workerSourceURL

However, one drawback to this no-types approach is that it basically seals the deal against specifying custom sources (and their dependent style layers) declaratively with style json like new Map(style: url). Personally, I think it's nice that the source type approach could enable this (after appropriately relaxing style validation).

@lucaswoj
Copy link
Contributor

lucaswoj commented Sep 15, 2016

However, one drawback to this no-types approach is that it basically seals the deal against specifying custom sources (and their dependent style layers) declaratively with style json like new Map(style: url). Personally, I think it's nice that the source type approach could enable this (after appropriately relaxing style validation).

It's a tradeoff for sure. We are giving up some ergonomics in exchange for simplicity, uniformity with GL Native, and the preservation of a tight validator. I prefer to err on the side of simplicity and see how things shake out in practice. Down the road we may revisit the "register source type" design or a design that allows embedding Source-interfaced objects in calls to Map#setStyle.

@anandthakker
Copy link
Contributor Author

It's a tradeoff for sure. We are giving up some ergonomics in exchange for a simplicity and the preservation of a tight validator. I prefer to err on the side of API simplicity (and uniformity with GL Native) and see how things shake out.

Gotcha.

I think the change from addSourceType-based to addCustomSource-based API should be reasonably manageable, but definitely makes more sense to do after the PooledWorker refactor that you're working on @lucaswoj.

anandthakker pushed a commit to developmentseed/mapbox-gl-js that referenced this pull request Sep 27, 2016
Revise addSourceType to be independent of Map instance
@lucaswoj
Copy link
Contributor

lucaswoj commented Oct 7, 2016

We are going to go a different direction for this functionality: #3004

@lucaswoj lucaswoj closed this Oct 7, 2016
@jfirebaugh jfirebaugh deleted the fix-add-source-type branch February 3, 2017 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants