createPropertyDescriptor.js 2.3 KB

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