Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

Commit

Permalink
Update diff for v8
Browse files Browse the repository at this point in the history
- remove setConstant
- add setCenter
- add setZoom
- add setBearing
- add setPitch

Issue: #338
  • Loading branch information
scothis committed Aug 14, 2015
1 parent e860081 commit 42a2961
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 45 deletions.
64 changes: 35 additions & 29 deletions lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,47 @@ var operations = {
setLayerZoomRange: 'setLayerZoomRange',

/*
* { command: 'setConstant', args: ['constantName', value] }
* { command: 'setLayerProperty', args: ['layerId', 'prop', value] }
*/
setConstant: 'setConstant',
setLayerProperty: 'setLayerProperty',

/*
* { command: 'setSprite', args: ['spriteUrl'] }
* { command: 'setCenter', args: [[lon, lat]] }
*/
setSprite: 'setSprite',
setCenter: 'setCenter',

/*
* { command: 'setGlyphs', args: ['glyphsUrl'] }
* { command: 'setZoom', args: [zoom] }
*/
setGlyphs: 'setGlyphs',
setZoom: 'setZoom',

/*
* { command: 'setTransition', args: [transition] }
* { command: 'setBearing', args: [bearing] }
*/
setTransition: 'setTransition',
setBearing: 'setBearing',

/*
* { command: 'setLayerProperty', args: ['layerId', 'prop', value] }
* { command: 'setPitch', args: [pitch] }
*/
setLayerProperty: 'setLayerProperty'
setPitch: 'setPitch',

};
/*
* { command: 'setSprite', args: ['spriteUrl'] }
*/
setSprite: 'setSprite',

/*
* { command: 'setGlyphs', args: ['glyphsUrl'] }
*/
setGlyphs: 'setGlyphs',

function diffConstants(before, after, commands) {
before = before || {};
after = after || {};
/*
* { command: 'setTransition', args: [transition] }
*/
setTransition: 'setTransition'

var prop;
};

for (prop in before) {
if (!before.hasOwnProperty(prop)) continue;
if (!isEqual(before[prop], after[prop])) {
commands.push({ command: operations.setConstant, args: [prop, after[prop]] });
}
}
for (prop in after) {
if (!after.hasOwnProperty(prop) || before.hasOwnProperty(prop)) continue;
if (!isEqual(before[prop], after[prop])) {
commands.push({ command: operations.setConstant, args: [prop, after[prop]] });
}
}
}

function diffSources(before, after, commands) {
before = before || {};
Expand Down Expand Up @@ -276,6 +271,18 @@ function diffStyles(before, after) {
if (!isEqual(before.version, after.version)) {
return [{ command: operations.setStyle, args: [after] }];
}
if (!isEqual(before.center, after.center)) {
commands.push({ command: operations.setCenter, args: [after.center] });
}
if (!isEqual(before.zoom, after.zoom)) {
commands.push({ command: operations.setZoom, args: [after.zoom] });
}
if (!isEqual(before.bearing, after.bearing)) {
commands.push({ command: operations.setBearing, args: [after.bearing] });
}
if (!isEqual(before.pitch, after.pitch)) {
commands.push({ command: operations.setPitch, args: [after.pitch] });
}
if (!isEqual(before.sprite, after.sprite)) {
commands.push({ command: operations.setSprite, args: [after.sprite] });
}
Expand All @@ -285,7 +292,6 @@ function diffStyles(before, after) {
if (!isEqual(before.transition, after.transition)) {
commands.push({ command: operations.setTransition, args: [after.transition] });
}
diffConstants(before.constants, after.constants, commands);
diffSources(before.sources, after.sources, commands);
diffLayers(before.layers, after.layers, commands);
} catch (e) {
Expand Down
55 changes: 39 additions & 16 deletions test/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@ var t = require('tape'),
t('diff', function (t) {

t.deepEqual(diffStyles({
constants: { '@a': 1 }
layers: [{ id: 'a' }]
}, {
constants: { '@a': 1 }
layers: [{ id: 'a' }]
}), [], 'no changes');

t.deepEqual(diffStyles({
version: 6,
constants: { '@a': 1 }
}, {
version: 7,
constants: { '@a': 1 }
layers: [{ id: 'a' }]
}, {
version: 8,
layers: [{ id: 'a' }]
}), [
{ command: 'setStyle', args: [{ version: 7, constants: { '@a': 1 } }] }
{ command: 'setStyle', args: [{ version: 8, layers: [{ id: 'a' }] }] }
], 'version change');


t.deepEqual(diffStyles({
constants: { '@a': 1 }
}, {
constants: { '@b': 1 }
}), [
{ command: 'setConstant', args: ['@a', undefined] },
{ command: 'setConstant', args: ['@b', 1] }
], 'set constants');

t.deepEqual(diffStyles({
layers: [{ id: 'a' }]
}, {
Expand Down Expand Up @@ -130,5 +121,37 @@ t('diff', function (t) {
layers: [{ id: 'a', metadata: { 'mapbox:group': 'Another Name' } }]
}), [], 'ignore layer metadata');

t.deepEqual(diffStyles({
center: [0, 0]
}, {
center: [1, 1]
}), [
{ command: 'setCenter', args: [[1, 1]] }
], 'center change');

t.deepEqual(diffStyles({
zoom: 12
}, {
zoom: 15
}), [
{ command: 'setZoom', args: [15] }
], 'zoom change');

t.deepEqual(diffStyles({
bearing: 0
}, {
bearing: 180
}), [
{ command: 'setBearing', args: [180] }
], 'bearing change');

t.deepEqual(diffStyles({
pitch: 0
}, {
pitch: 1
}), [
{ command: 'setPitch', args: [1] }
], 'pitch change');

t.end();
});

0 comments on commit 42a2961

Please sign in to comment.