createPropertyDescriptor.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import ConstantProperty from "./ConstantProperty.js";
  4. function createProperty(
  5. name,
  6. privateName,
  7. subscriptionName,
  8. configurable,
  9. createPropertyCallback
  10. ) {
  11. return {
  12. configurable: configurable,
  13. get: function () {
  14. return this[privateName];
  15. },
  16. set: function (value) {
  17. const oldValue = this[privateName];
  18. const subscription = this[subscriptionName];
  19. if (defined(subscription)) {
  20. subscription();
  21. this[subscriptionName] = undefined;
  22. }
  23. const hasValue = value !== undefined;
  24. if (
  25. hasValue &&
  26. (!defined(value) || !defined(value.getValue)) &&
  27. defined(createPropertyCallback)
  28. ) {
  29. value = createPropertyCallback(value);
  30. }
  31. if (oldValue !== value) {
  32. this[privateName] = value;
  33. this._definitionChanged.raiseEvent(this, name, value, oldValue);
  34. }
  35. if (defined(value) && defined(value.definitionChanged)) {
  36. this[subscriptionName] = value.definitionChanged.addEventListener(
  37. function () {
  38. this._definitionChanged.raiseEvent(this, name, value, value);
  39. },
  40. this
  41. );
  42. }
  43. },
  44. };
  45. }
  46. function createConstantProperty(value) {
  47. return new ConstantProperty(value);
  48. }
  49. /**
  50. * Used to consistently define all DataSources graphics objects.
  51. * This is broken into two functions because the Chrome profiler does a better
  52. * job of optimizing lookups if it notices that the string is constant throughout the function.
  53. * @private
  54. */
  55. function createPropertyDescriptor(name, configurable, createPropertyCallback) {
  56. //Safari 8.0.3 has a JavaScript bug that causes it to confuse two variables and treat them as the same.
  57. //The two extra toString calls work around the issue.
  58. return createProperty(
  59. name,
  60. `_${name.toString()}`,
  61. `_${name.toString()}Subscription`,
  62. defaultValue(configurable, false),
  63. defaultValue(createPropertyCallback, createConstantProperty)
  64. );
  65. }
  66. export default createPropertyDescriptor;