智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import defined from "../Core/defined.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import Event from "../Core/Event.js";
  4. import JulianDate from "../Core/JulianDate.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. Object.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. const timeScratch = new JulianDate();
  48. /**
  49. * Gets the value of the property.
  50. *
  51. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  52. * @param {object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  53. * @returns {object} The modified result parameter or a new instance if the result parameter was not supplied or is unsupported.
  54. */
  55. CallbackProperty.prototype.getValue = function (time, result) {
  56. if (!defined(time)) {
  57. time = JulianDate.now(timeScratch);
  58. }
  59. return this._callback(time, result);
  60. };
  61. /**
  62. * Sets the callback to be used.
  63. *
  64. * @param {CallbackProperty.Callback} callback The function to be called when the property is evaluated.
  65. * @param {boolean} isConstant <code>true</code> when the callback function returns the same value every time, <code>false</code> if the value will change.
  66. */
  67. CallbackProperty.prototype.setCallback = function (callback, isConstant) {
  68. //>>includeStart('debug', pragmas.debug);
  69. if (!defined(callback)) {
  70. throw new DeveloperError("callback is required.");
  71. }
  72. if (!defined(isConstant)) {
  73. throw new DeveloperError("isConstant is required.");
  74. }
  75. //>>includeEnd('debug');
  76. const changed =
  77. this._callback !== callback || this._isConstant !== isConstant;
  78. this._callback = callback;
  79. this._isConstant = isConstant;
  80. if (changed) {
  81. this._definitionChanged.raiseEvent(this);
  82. }
  83. };
  84. /**
  85. * Compares this property to the provided property and returns
  86. * <code>true</code> if they are equal, <code>false</code> otherwise.
  87. *
  88. * @param {Property} [other] The other property.
  89. * @returns {boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  90. */
  91. CallbackProperty.prototype.equals = function (other) {
  92. return (
  93. this === other ||
  94. (other instanceof CallbackProperty &&
  95. this._callback === other._callback &&
  96. this._isConstant === other._isConstant)
  97. );
  98. };
  99. /**
  100. * A function that returns the value of the property.
  101. * @callback CallbackProperty.Callback
  102. *
  103. * @param {JulianDate} [time=JulianDate.now()] The time for which to retrieve the value. If omitted, the current system time is used.
  104. * @param {object} [result] The object to store the value into. If omitted, the function must create and return a new instance.
  105. * @returns {object} The modified result parameter, or a new instance if the result parameter was not supplied or is unsupported.
  106. */
  107. export default CallbackProperty;