A no-op Graph#replace should not create a new graph

This is useful for reducing graph churn.
This commit is contained in:
John Firebaugh
2013-02-05 18:23:27 -08:00
parent b62f106be8
commit 53ed9df2b7
2 changed files with 10 additions and 2 deletions
+3
View File
@@ -189,6 +189,9 @@ iD.Graph.prototype = {
},
replace: function(entity) {
if (this.entities[entity.id] === entity)
return this;
return this.update(function () {
this._updateCalculated(this.entities[entity.id], entity);
this.entities[entity.id] = entity;
+7 -2
View File
@@ -196,10 +196,16 @@ describe('iD.Graph', function() {
});
describe("#replace", function () {
it("is a no-op if the replacement is identical to the existing entity", function () {
var node = iD.Node(),
graph = iD.Graph([node]);
expect(graph.replace(node)).to.equal(graph);
});
it("returns a new graph", function () {
var node = iD.Node(),
graph = iD.Graph([node]);
expect(graph.replace(node)).not.to.equal(graph);
expect(graph.replace(node.update())).not.to.equal(graph);
});
it("doesn't modify the receiver", function () {
@@ -257,7 +263,6 @@ describe('iD.Graph', function() {
graph = iD.Graph([node, r1]);
expect(graph.replace(r1).parentRelations(node)).to.eql([r1]);
});
});
describe("#update", function () {