From c66ea58d5659257dd3fb41855e218a2458bfd45f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 25 Dec 2012 13:30:31 -0800 Subject: [PATCH] Move transients to graph (fixes #285) --- js/id/graph/entity.js | 44 +++++++++++++++++++-------------------- js/id/graph/graph.js | 13 ++++++++++++ test/spec/graph/entity.js | 10 --------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index d0ed8b6e6..a7faee1b9 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -2,13 +2,12 @@ iD.Entity = function(a, b, c) { if (!(this instanceof iD.Entity)) return new iD.Entity(a, b, c); this.tags = {}; - this.transients = {}; var sources = [a, b, c], source; for (var i = 0; i < sources.length; ++i) { source = sources[i]; for (var prop in source) { - if (prop !== 'transients' && Object.prototype.hasOwnProperty.call(source, prop)) { + if (Object.prototype.hasOwnProperty.call(source, prop)) { this[prop] = source[prop]; } } @@ -59,35 +58,34 @@ iD.Entity.prototype = { return this._updated && this.osmId() > 0; }, - intersects: function(extent, resolver) { + extent: function(resolver) { if (this.type === 'node') { - return this.loc[0] > extent[0][0] && - this.loc[0] < extent[1][0] && - this.loc[1] < extent[0][1] && - this.loc[1] > extent[1][1]; - } else if (this.type === 'way') { - var _extent = this.transients.extent; - - if (!_extent) { - _extent = this.transients.extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; + return [this.loc, this.loc]; + } else if (this.type == 'way') { + return resolver.transient(this, 'extent', function() { + var extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; for (var i = 0, l = this.nodes.length; i < l; i++) { var node = resolver.entity(this.nodes[i]); - if (node.loc[0] > _extent[0][0]) _extent[0][0] = node.loc[0]; - if (node.loc[0] < _extent[1][0]) _extent[1][0] = node.loc[0]; - if (node.loc[1] < _extent[0][1]) _extent[0][1] = node.loc[1]; - if (node.loc[1] > _extent[1][1]) _extent[1][1] = node.loc[1]; + if (node.loc[0] > extent[0][0]) extent[0][0] = node.loc[0]; + if (node.loc[0] < extent[1][0]) extent[1][0] = node.loc[0]; + if (node.loc[1] < extent[0][1]) extent[0][1] = node.loc[1]; + if (node.loc[1] > extent[1][1]) extent[1][1] = node.loc[1]; } - } - - return _extent[0][0] > extent[0][0] && - _extent[1][0] < extent[1][0] && - _extent[0][1] < extent[0][1] && - _extent[1][1] > extent[1][1]; + return extent; + }); } else { - return false; + return [[NaN, NaN], [NaN, NaN]]; } }, + intersects: function(extent, resolver) { + var _extent = this.extent(resolver); + return _extent[0][0] > extent[0][0] && + _extent[1][0] < extent[1][0] && + _extent[0][1] < extent[0][1] && + _extent[1][1] > extent[1][1]; + }, + hasInterestingTags: function() { return _.keys(this.tags).some(function (key) { return key != "attribution" && diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 5d0a15631..6786e0f7e 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -10,6 +10,8 @@ iD.Graph = function(entities) { this.entities = entities || {}; } + this.transients = {}; + if (iD.debug) { Object.freeze(this); Object.freeze(this.entities); @@ -21,6 +23,17 @@ iD.Graph.prototype = { return this.entities[id]; }, + transient: function(entity, key, fn) { + var id = entity.id, + transients = this.transients[id] || (this.transients[id] = {}); + + if (transients[key]) { + return transients[key]; + } + + return transients[key] = fn.call(entity); + }, + parentStructure: function(ways) { var nodes = {}; ways.forEach(function(w) { diff --git a/test/spec/graph/entity.js b/test/spec/graph/entity.js index ba0d6f05d..0010696fe 100644 --- a/test/spec/graph/entity.js +++ b/test/spec/graph/entity.js @@ -7,10 +7,6 @@ describe('iD.Entity', function () { it("freezes tags", function () { expect(Object.isFrozen(iD.Entity().tags)).to.be.true; }); - - it("does not freeze transients", function () { - expect(Object.isFrozen(iD.Entity().transients)).to.be.false; - }); } describe(".id", function () { @@ -57,12 +53,6 @@ describe('iD.Entity', function () { expect(attrs).to.eql({tags: {foo: 'bar'}}); }); - it("doesn't copy transients", function () { - var entity = iD.Entity(); - entity.transients['foo'] = 'bar'; - expect(entity.update({}).transients).not.to.have.property('foo'); - }); - it("doesn't copy prototype properties", function () { expect(iD.Entity().update({})).not.to.have.ownProperty('update'); });