CallbackProperty.js 4.1 KB

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