CallbackProperty.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*global define*/
  2. define([
  3. '../Core/defined',
  4. '../Core/defineProperties',
  5. '../Core/DeveloperError',
  6. '../Core/Event'
  7. ], function(
  8. defined,
  9. defineProperties,
  10. DeveloperError,
  11. Event) {
  12. "use strict";
  13. /**
  14. * A {@link Property} whose value is lazily evaluated by a callback function.
  15. *
  16. * @alias CallbackProperty
  17. * @constructor
  18. *
  19. * @param {CallbackProperty~Callback} callback The function to be called when the property is evaluated.
  20. * @param {Boolean} isConstant <code>true</code> when the callback function returns the same value every time, <code>false</code> if the value will change.
  21. */
  22. var CallbackProperty = function(callback, isConstant) {
  23. this._callback = undefined;
  24. this._isConstant = undefined;
  25. this._definitionChanged = new Event();
  26. this.setCallback(callback, isConstant);
  27. };
  28. defineProperties(CallbackProperty.prototype, {
  29. /**
  30. * Gets a value indicating if this property is constant.
  31. * @memberof CallbackProperty.prototype
  32. *
  33. * @type {Boolean}
  34. * @readonly
  35. */
  36. isConstant : {
  37. get : function() {
  38. return this._isConstant;
  39. }
  40. },
  41. /**
  42. * Gets the event that is raised whenever the definition of this property changes.
  43. * The definition is changed whenever setCallback is called.
  44. * @memberof CallbackProperty.prototype
  45. *
  46. * @type {Event}
  47. * @readonly
  48. */
  49. definitionChanged : {
  50. get : function() {
  51. return this._definitionChanged;
  52. }
  53. }
  54. });
  55. /**
  56. * Gets the value of the property.
  57. *
  58. * @param {JulianDate} [time] The time for which to retrieve the value. This parameter is unused since the value does not change with respect to time.
  59. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  60. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied or is unsupported.
  61. */
  62. CallbackProperty.prototype.getValue = function(time, result) {
  63. return this._callback(time, result);
  64. };
  65. /**
  66. * Sets the callback to be used.
  67. *
  68. * @param {CallbackProperty~Callback} callback The function to be called when the property is evaluated.
  69. * @param {Boolean} isConstant <code>true</code> when the callback function returns the same value every time, <code>false</code> if the value will change.
  70. */
  71. CallbackProperty.prototype.setCallback = function(callback, isConstant) {
  72. //>>includeStart('debug', pragmas.debug);
  73. if (!defined(callback)) {
  74. throw new DeveloperError('callback is required.');
  75. }
  76. if (!defined(isConstant)) {
  77. throw new DeveloperError('isConstant is required.');
  78. }
  79. //>>includeEnd('debug');
  80. var changed = this._callback !== callback || this._isConstant !== isConstant;
  81. this._callback = callback;
  82. this._isConstant = isConstant;
  83. if (changed) {
  84. this._definitionChanged.raiseEvent(this);
  85. }
  86. };
  87. /**
  88. * Compares this property to the provided property and returns
  89. * <code>true</code> if they are equal, <code>false</code> otherwise.
  90. *
  91. * @param {Property} [other] The other property.
  92. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  93. */
  94. CallbackProperty.prototype.equals = function(other) {
  95. return this === other || (other instanceof CallbackProperty && this._callback === other._callback && this._isConstant === other._isConstant);
  96. };
  97. /**
  98. * A function that returns the value of the property.
  99. * @callback CallbackProperty~Callback
  100. *
  101. * @param {JulianDate} [time] The time for which to retrieve the value.
  102. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  103. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied or is unsupported.
  104. */
  105. return CallbackProperty;
  106. });