Creating an object that stores properties only when needed in BaseObject class

This commit is contained in:
Michał Zielański
2020-06-15 13:00:26 +02:00
parent 323a472fbb
commit 0e8f0034ab
7 changed files with 103 additions and 58 deletions
+3 -1
View File
@@ -128,7 +128,9 @@ class Feature extends BaseObject {
* @api * @api
*/ */
clone() { clone() {
const clone = new Feature(this.getProperties()); const clone = new Feature(
this.hasProperties() ? this.getProperties() : undefined
);
clone.setGeometryName(this.getGeometryName()); clone.setGeometryName(this.getGeometryName());
const geometry = this.getGeometry(); const geometry = this.getGeometry();
if (geometry) { if (geometry) {
+20 -9
View File
@@ -4,7 +4,7 @@
import Event from './events/Event.js'; import Event from './events/Event.js';
import ObjectEventType from './ObjectEventType.js'; import ObjectEventType from './ObjectEventType.js';
import Observable from './Observable.js'; import Observable from './Observable.js';
import {assign} from './obj.js'; import {assign, isEmpty} from './obj.js';
import {getUid} from './util.js'; import {getUid} from './util.js';
/** /**
@@ -94,10 +94,10 @@ class BaseObject extends Observable {
getUid(this); getUid(this);
/** /**
* @name values_
* @private * @private
* @type {!Object<string, *>} * @type {!Object<string, *>}
*/ */
this.values_ = {};
if (opt_values !== undefined) { if (opt_values !== undefined) {
this.setProperties(opt_values); this.setProperties(opt_values);
@@ -112,7 +112,7 @@ class BaseObject extends Observable {
*/ */
get(key) { get(key) {
let value; let value;
if (this.values_.hasOwnProperty(key)) { if (this.values_ && this.values_.hasOwnProperty(key)) {
value = this.values_[key]; value = this.values_[key];
} }
return value; return value;
@@ -124,7 +124,7 @@ class BaseObject extends Observable {
* @api * @api
*/ */
getKeys() { getKeys() {
return Object.keys(this.values_); return (this.values_ && Object.keys(this.values_)) || [];
} }
/** /**
@@ -133,7 +133,14 @@ class BaseObject extends Observable {
* @api * @api
*/ */
getProperties() { getProperties() {
return assign({}, this.values_); return (this.values_ && assign({}, this.values_)) || {};
}
/**
* @return {boolean} The object has properties.
*/
hasProperties() {
return !!this.values_;
} }
/** /**
@@ -156,11 +163,12 @@ class BaseObject extends Observable {
* @api * @api
*/ */
set(key, value, opt_silent) { set(key, value, opt_silent) {
const values = this.values_ || (this.values_ = {});
if (opt_silent) { if (opt_silent) {
this.values_[key] = value; values[key] = value;
} else { } else {
const oldValue = this.values_[key]; const oldValue = values[key];
this.values_[key] = value; values[key] = value;
if (oldValue !== value) { if (oldValue !== value) {
this.notify(key, oldValue); this.notify(key, oldValue);
} }
@@ -187,9 +195,12 @@ class BaseObject extends Observable {
* @api * @api
*/ */
unset(key, opt_silent) { unset(key, opt_silent) {
if (key in this.values_) { if (this.values_ && key in this.values_) {
const oldValue = this.values_[key]; const oldValue = this.values_[key];
delete this.values_[key]; delete this.values_[key];
if (isEmpty(this.values_)) {
delete this.values_;
}
if (!opt_silent) { if (!opt_silent) {
this.notify(key, oldValue); this.notify(key, oldValue);
} }
+6 -2
View File
@@ -199,6 +199,11 @@ class EsriJSON extends JSONFeature {
writeFeatureObject(feature, opt_options) { writeFeatureObject(feature, opt_options) {
opt_options = this.adaptOptions(opt_options); opt_options = this.adaptOptions(opt_options);
const object = {}; const object = {};
if (!feature.hasProperties()) {
object['attributes'] = {};
return object;
}
const properties = feature.getProperties();
const geometry = feature.getGeometry(); const geometry = feature.getGeometry();
if (geometry) { if (geometry) {
object['geometry'] = writeGeometry(geometry, opt_options); object['geometry'] = writeGeometry(geometry, opt_options);
@@ -214,9 +219,8 @@ class EsriJSON extends JSONFeature {
), ),
}); });
} }
delete properties[feature.getGeometryName()];
} }
const properties = feature.getProperties();
delete properties[feature.getGeometryName()];
if (!isEmpty(properties)) { if (!isEmpty(properties)) {
object['attributes'] = properties; object['attributes'] = properties;
} else { } else {
+24 -21
View File
@@ -212,29 +212,32 @@ class GML2 extends GMLBase {
context.serializers = {}; context.serializers = {};
context.serializers[featureNS] = {}; context.serializers[featureNS] = {};
} }
const properties = feature.getProperties();
const keys = []; const keys = [];
const values = []; const values = [];
for (const key in properties) { if (feature.hasProperties()) {
const value = properties[key]; const properties = feature.getProperties();
if (value !== null) { for (const key in properties) {
keys.push(key); const value = properties[key];
values.push(value); if (value !== null) {
if ( keys.push(key);
key == geometryName || values.push(value);
typeof (/** @type {?} */ (value).getSimplifiedGeometry) === 'function' if (
) { key == geometryName ||
if (!(key in context.serializers[featureNS])) { typeof (/** @type {?} */ (value).getSimplifiedGeometry) ===
context.serializers[featureNS][key] = makeChildAppender( 'function'
this.writeGeometryElement, ) {
this if (!(key in context.serializers[featureNS])) {
); context.serializers[featureNS][key] = makeChildAppender(
} this.writeGeometryElement,
} else { this
if (!(key in context.serializers[featureNS])) { );
context.serializers[featureNS][key] = makeChildAppender( }
writeStringTextNode } else {
); if (!(key in context.serializers[featureNS])) {
context.serializers[featureNS][key] = makeChildAppender(
writeStringTextNode
);
}
} }
} }
} }
+24 -21
View File
@@ -843,29 +843,32 @@ class GML3 extends GMLBase {
context.serializers = {}; context.serializers = {};
context.serializers[featureNS] = {}; context.serializers[featureNS] = {};
} }
const properties = feature.getProperties();
const keys = []; const keys = [];
const values = []; const values = [];
for (const key in properties) { if (feature.hasProperties()) {
const value = properties[key]; const properties = feature.getProperties();
if (value !== null) { for (const key in properties) {
keys.push(key); const value = properties[key];
values.push(value); if (value !== null) {
if ( keys.push(key);
key == geometryName || values.push(value);
typeof (/** @type {?} */ (value).getSimplifiedGeometry) === 'function' if (
) { key == geometryName ||
if (!(key in context.serializers[featureNS])) { typeof (/** @type {?} */ (value).getSimplifiedGeometry) ===
context.serializers[featureNS][key] = makeChildAppender( 'function'
this.writeGeometryElement, ) {
this if (!(key in context.serializers[featureNS])) {
); context.serializers[featureNS][key] = makeChildAppender(
} this.writeGeometryElement,
} else { this
if (!(key in context.serializers[featureNS])) { );
context.serializers[featureNS][key] = makeChildAppender( }
writeStringTextNode } else {
); if (!(key in context.serializers[featureNS])) {
context.serializers[featureNS][key] = makeChildAppender(
writeStringTextNode
);
}
} }
} }
} }
+10 -4
View File
@@ -207,16 +207,22 @@ class GeoJSON extends JSONFeature {
object.id = id; object.id = id;
} }
const geometry = feature.getGeometry(); if (!feature.hasProperties()) {
if (geometry) { return object;
object.geometry = writeGeometry(geometry, opt_options);
} }
const properties = feature.getProperties(); const properties = feature.getProperties();
delete properties[feature.getGeometryName()]; const geometry = feature.getGeometry();
if (geometry) {
object.geometry = writeGeometry(geometry, opt_options);
delete properties[feature.getGeometryName()];
}
if (!isEmpty(properties)) { if (!isEmpty(properties)) {
object.properties = properties; object.properties = properties;
} }
return object; return object;
} }
+16
View File
@@ -101,6 +101,22 @@ describe('ol.Object', function () {
}); });
}); });
describe('hasProperties', function () {
it('has no properties after creation', function () {
expect(o.hasProperties()).to.eql(false);
});
it('has properties after set', function () {
o.set('foo', 1);
expect(o.hasProperties()).to.eql(true);
});
it('has no properties after unset all', function () {
o.unset('foo');
expect(o.hasProperties()).to.eql(false);
});
});
describe('notify', function () { describe('notify', function () {
let listener1, listener2; let listener1, listener2;