Repository navigation
Conversation
A user should never attempt to mutate the style before it has loaded. Throw an error when a mutation is attempted.
Adds `style.json()` which returns the stylesheet as a pure JSON object that is equivalent to the stylesheet necessary to recreate the style object. Mutations to the style object are reflected by subsequent calls to `.json()` Issue: #1341
Mutation operations on a Style instance are designed to be atomic.
Depending on the operation, there is a significant amount of work that
must be preformed to flush and recreate state. This work is wasted when
multiple mutation methods are invokes sequentially.
Batching these mutations allows deferring this work until the batch is
complete and deduping the work.
style.batch(function() {
style.addLayer(layer1);
style.addLayer(layer2);
...
style.addLayer(layerN);
});
Internally, calls to four methods are deferred:
- style._groupLayers (will only fire once)
- style._broadcastLayers (will only fire once)
- style._reloadSource (will fire once per source)
- style.fire (each call will fire)
A casual benchmark shows the time to add 80 layers dropping from ~800 ms
to ~20 ms when run as a batch.
Issue: #1341
Two plain JS stylesheets are accepted as inputs, the output is an array
of operations necessary to bring the first stylesheet to be equivalent to
the second.
var operations = styleDiff.diff(oldStylesheet, stylesheet);
Operations are a command and args. A command is the name of the method on
the style to invoke with the args. Operations are JSON serializable.
The diff for a stylesheet can be apply to a style object via patch. Patch
will throw if it is unable to fully apply the diff.
styleDiff.patch(map.style, operations);
Issue: #1341
Previously, calls to map.setStyle() created a new Style instance. Creating a new Style instance is an expensive operation that abandons the current layers and sources. For simple updates, this is very wasteful. Instead of creating a Style object for every call of map.setStyle() we now diff the new style with the current style and apply the delta within a batch update. Situations where the non-incremental setStyle is used: - initial style for a map - the new stylesheet is undefined - the new stylesheet is already a Style instance - the current style is not done loading - the style diff cannot be applied incrementally Issue: #1341
|
I really like this API: The parts I'm concerned about with this PR as it stands are twofold:
|
|
I agree around keeping two models in sync. I was worried the rich objects would introduce noise into the diff. I'll play around with generating a plain object model from the rich objects to see how feasible it is. Inverting the mutation methods to leverage the batch is interesting. It would end up being more code, but should be cleaner. I'll play with it. |
|
Exporting the json stylesheet from a rich object is mostly possible, however, it will create significant noise in the diff. Layout and paint layer properties are particularly tricky as they don't distinguish between explicit, default and constant values. It's worth cleaning up, but will take a good chunk of effort. Should creating a unified model block this work? |
|
Let's take the "throw on style mutation before load" and |
|
Closing -- let's revisit diffing |
This PR contains five discreet commits:
Issue: #1341