PropertyBag.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import ConstantProperty from "./ConstantProperty.js";
  6. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  7. import Property from "./Property.js";
  8. /**
  9. * A {@link Property} whose value is a key-value mapping of property names to the computed value of other properties.
  10. *
  11. * @alias PropertyBag
  12. * @implements Record<string, any>
  13. * @constructor
  14. *
  15. * @param {Object} [value] An object, containing key-value mapping of property names to properties.
  16. * @param {Function} [createPropertyCallback] A function that will be called when the value of any of the properties in value are not a Property.
  17. */
  18. function PropertyBag(value, createPropertyCallback) {
  19. this._propertyNames = [];
  20. this._definitionChanged = new Event();
  21. if (defined(value)) {
  22. this.merge(value, createPropertyCallback);
  23. }
  24. }
  25. Object.defineProperties(PropertyBag.prototype, {
  26. /**
  27. * Gets the names of all properties registered on this instance.
  28. * @memberof PropertyBag.prototype
  29. * @type {Array}
  30. */
  31. propertyNames: {
  32. get: function () {
  33. return this._propertyNames;
  34. },
  35. },
  36. /**
  37. * Gets a value indicating if this property is constant. This property
  38. * is considered constant if all property items in this object are constant.
  39. * @memberof PropertyBag.prototype
  40. *
  41. * @type {Boolean}
  42. * @readonly
  43. */
  44. isConstant: {
  45. get: function () {
  46. const propertyNames = this._propertyNames;
  47. for (let i = 0, len = propertyNames.length; i < len; i++) {
  48. if (!Property.isConstant(this[propertyNames[i]])) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. },
  54. },
  55. /**
  56. * Gets the event that is raised whenever the set of properties contained in this
  57. * object changes, or one of the properties itself changes.
  58. *
  59. * @memberof PropertyBag.prototype
  60. *
  61. * @type {Event}
  62. * @readonly
  63. */
  64. definitionChanged: {
  65. get: function () {
  66. return this._definitionChanged;
  67. },
  68. },
  69. });
  70. /**
  71. * Determines if this object has defined a property with the given name.
  72. *
  73. * @param {String} propertyName The name of the property to check for.
  74. *
  75. * @returns {Boolean} True if this object has defined a property with the given name, false otherwise.
  76. */
  77. PropertyBag.prototype.hasProperty = function (propertyName) {
  78. return this._propertyNames.indexOf(propertyName) !== -1;
  79. };
  80. function createConstantProperty(value) {
  81. return new ConstantProperty(value);
  82. }
  83. /**
  84. * Adds a property to this object.
  85. *
  86. * @param {String} propertyName The name of the property to add.
  87. * @param {*} [value] The value of the new property, if provided.
  88. * @param {Function} [createPropertyCallback] A function that will be called when the value of this new property is set to a value that is not a Property.
  89. *
  90. * @exception {DeveloperError} "propertyName" is already a registered property.
  91. */
  92. PropertyBag.prototype.addProperty = function (
  93. propertyName,
  94. value,
  95. createPropertyCallback
  96. ) {
  97. const propertyNames = this._propertyNames;
  98. //>>includeStart('debug', pragmas.debug);
  99. if (!defined(propertyName)) {
  100. throw new DeveloperError("propertyName is required.");
  101. }
  102. if (propertyNames.indexOf(propertyName) !== -1) {
  103. throw new DeveloperError(
  104. `${propertyName} is already a registered property.`
  105. );
  106. }
  107. //>>includeEnd('debug');
  108. propertyNames.push(propertyName);
  109. Object.defineProperty(
  110. this,
  111. propertyName,
  112. createPropertyDescriptor(
  113. propertyName,
  114. true,
  115. defaultValue(createPropertyCallback, createConstantProperty)
  116. )
  117. );
  118. if (defined(value)) {
  119. this[propertyName] = value;
  120. }
  121. this._definitionChanged.raiseEvent(this);
  122. };
  123. /**
  124. * Removed a property previously added with addProperty.
  125. *
  126. * @param {String} propertyName The name of the property to remove.
  127. *
  128. * @exception {DeveloperError} "propertyName" is not a registered property.
  129. */
  130. PropertyBag.prototype.removeProperty = function (propertyName) {
  131. const propertyNames = this._propertyNames;
  132. const index = propertyNames.indexOf(propertyName);
  133. //>>includeStart('debug', pragmas.debug);
  134. if (!defined(propertyName)) {
  135. throw new DeveloperError("propertyName is required.");
  136. }
  137. if (index === -1) {
  138. throw new DeveloperError(`${propertyName} is not a registered property.`);
  139. }
  140. //>>includeEnd('debug');
  141. this._propertyNames.splice(index, 1);
  142. delete this[propertyName];
  143. this._definitionChanged.raiseEvent(this);
  144. };
  145. /**
  146. * Gets the value of this property. Each contained property will be evaluated at the given time, and the overall
  147. * result will be an object, mapping property names to those values.
  148. *
  149. * @param {JulianDate} time The time for which to retrieve the value.
  150. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  151. * Note that any properties in result which are not part of this PropertyBag will be left as-is.
  152. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  153. */
  154. PropertyBag.prototype.getValue = function (time, result) {
  155. //>>includeStart('debug', pragmas.debug);
  156. if (!defined(time)) {
  157. throw new DeveloperError("time is required.");
  158. }
  159. //>>includeEnd('debug');
  160. if (!defined(result)) {
  161. result = {};
  162. }
  163. const propertyNames = this._propertyNames;
  164. for (let i = 0, len = propertyNames.length; i < len; i++) {
  165. const propertyName = propertyNames[i];
  166. result[propertyName] = Property.getValueOrUndefined(
  167. this[propertyName],
  168. time,
  169. result[propertyName]
  170. );
  171. }
  172. return result;
  173. };
  174. /**
  175. * Assigns each unassigned property on this object to the value
  176. * of the same property on the provided source object.
  177. *
  178. * @param {Object} source The object to be merged into this object.
  179. * @param {Function} [createPropertyCallback] A function that will be called when the value of any of the properties in value are not a Property.
  180. */
  181. PropertyBag.prototype.merge = function (source, createPropertyCallback) {
  182. //>>includeStart('debug', pragmas.debug);
  183. if (!defined(source)) {
  184. throw new DeveloperError("source is required.");
  185. }
  186. //>>includeEnd('debug');
  187. const propertyNames = this._propertyNames;
  188. const sourcePropertyNames = defined(source._propertyNames)
  189. ? source._propertyNames
  190. : Object.keys(source);
  191. for (let i = 0, len = sourcePropertyNames.length; i < len; i++) {
  192. const name = sourcePropertyNames[i];
  193. const targetProperty = this[name];
  194. const sourceProperty = source[name];
  195. //Custom properties that are registered on the source must also be added to this.
  196. if (targetProperty === undefined && propertyNames.indexOf(name) === -1) {
  197. this.addProperty(name, undefined, createPropertyCallback);
  198. }
  199. if (sourceProperty !== undefined) {
  200. if (targetProperty !== undefined) {
  201. if (defined(targetProperty) && defined(targetProperty.merge)) {
  202. targetProperty.merge(sourceProperty);
  203. }
  204. } else if (
  205. defined(sourceProperty) &&
  206. defined(sourceProperty.merge) &&
  207. defined(sourceProperty.clone)
  208. ) {
  209. this[name] = sourceProperty.clone();
  210. } else {
  211. this[name] = sourceProperty;
  212. }
  213. }
  214. }
  215. };
  216. function propertiesEqual(a, b) {
  217. const aPropertyNames = a._propertyNames;
  218. const bPropertyNames = b._propertyNames;
  219. const len = aPropertyNames.length;
  220. if (len !== bPropertyNames.length) {
  221. return false;
  222. }
  223. for (let aIndex = 0; aIndex < len; ++aIndex) {
  224. const name = aPropertyNames[aIndex];
  225. const bIndex = bPropertyNames.indexOf(name);
  226. if (bIndex === -1) {
  227. return false;
  228. }
  229. if (!Property.equals(a[name], b[name])) {
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. /**
  236. * Compares this property to the provided property and returns
  237. * <code>true</code> if they are equal, <code>false</code> otherwise.
  238. *
  239. * @param {Property} [other] The other property.
  240. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  241. */
  242. PropertyBag.prototype.equals = function (other) {
  243. return (
  244. this === other || //
  245. (other instanceof PropertyBag && //
  246. propertiesEqual(this, other))
  247. );
  248. };
  249. export default PropertyBag;