CallbackProperty.js 3.6 KB

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