dataObserver.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. import jsonpatch from './../../../lib/jsonpatch/json-patch-duplex';
  4. import localHooks from '../../mixins/localHooks';
  5. import { mixin } from '../../helpers/object';
  6. import { cleanPatches } from './utils';
  7. /**
  8. * @class DataObserver
  9. * @plugin ObserveChanges
  10. */
  11. var DataObserver = function () {
  12. function DataObserver(observedData) {
  13. _classCallCheck(this, DataObserver);
  14. /**
  15. * Observed source data.
  16. *
  17. * @type {Array}
  18. */
  19. this.observedData = null;
  20. /**
  21. * JsonPatch observer.
  22. *
  23. * @type {Object}
  24. */
  25. this.observer = null;
  26. /**
  27. * Flag which determines if observer is paused or not. Paused observer doesn't emit `change` hooks.
  28. *
  29. * @type {Boolean}
  30. * @default false
  31. */
  32. this.paused = false;
  33. this.setObservedData(observedData);
  34. }
  35. /**
  36. * Set data to observe.
  37. *
  38. * @param {*} observedData
  39. */
  40. _createClass(DataObserver, [{
  41. key: 'setObservedData',
  42. value: function setObservedData(observedData) {
  43. var _this = this;
  44. if (this.observer) {
  45. jsonpatch.unobserve(this.observedData, this.observer);
  46. }
  47. this.observedData = observedData;
  48. this.observer = jsonpatch.observe(this.observedData, function (patches) {
  49. return _this.onChange(patches);
  50. });
  51. }
  52. /**
  53. * Check if observer was paused.
  54. *
  55. * @returns {Boolean}
  56. */
  57. }, {
  58. key: 'isPaused',
  59. value: function isPaused() {
  60. return this.paused;
  61. }
  62. /**
  63. * Pause observer (stop emitting all detected changes).
  64. */
  65. }, {
  66. key: 'pause',
  67. value: function pause() {
  68. this.paused = true;
  69. }
  70. /**
  71. * Resume observer (emit all detected changes).
  72. */
  73. }, {
  74. key: 'resume',
  75. value: function resume() {
  76. this.paused = false;
  77. }
  78. /**
  79. * JsonPatch on change listener.
  80. *
  81. * @private
  82. * @param {Array} patches An array of object passed from jsonpatch.
  83. */
  84. }, {
  85. key: 'onChange',
  86. value: function onChange(patches) {
  87. this.runLocalHooks('change', cleanPatches(patches));
  88. }
  89. /**
  90. * Destroy observer instance.
  91. */
  92. }, {
  93. key: 'destroy',
  94. value: function destroy() {
  95. jsonpatch.unobserve(this.observedData, this.observer);
  96. this.observedData = null;
  97. this.observer = null;
  98. }
  99. }]);
  100. return DataObserver;
  101. }();
  102. mixin(DataObserver, localHooks);
  103. export default DataObserver;