handsontable
Version:
Handsontable is a JavaScript Spreadsheet Component available for React, Angular and Vue.
122 lines (104 loc) • 3.17 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
import jsonpatch from "../../3rdparty/jsonpatch/json-patch-duplex.mjs";
import localHooks from "../../mixins/localHooks.mjs";
import { mixin } from "../../helpers/object.mjs";
import { cleanPatches } from "./utils.mjs";
/**
* @class DataObserver
* @plugin ObserveChanges
*/
var DataObserver = /*#__PURE__*/function () {
function DataObserver(observedData) {
_classCallCheck(this, DataObserver);
/**
* Observed source data.
*
* @type {Array}
*/
this.observedData = null;
/**
* JsonPatch observer.
*
* @type {object}
*/
this.observer = null;
/**
* Flag which determines if observer is paused or not. Paused observer doesn't emit `change` hooks.
*
* @type {boolean}
* @default false
*/
this.paused = false;
this.setObservedData(observedData);
}
/**
* Set data to observe.
*
* @param {*} observedData Set an object to observe.
*/
_createClass(DataObserver, [{
key: "setObservedData",
value: function setObservedData(observedData) {
var _this = this;
if (this.observer) {
jsonpatch.unobserve(this.observedData, this.observer);
}
this.observedData = observedData;
this.observer = jsonpatch.observe(this.observedData, function (patches) {
return _this.onChange(patches);
});
}
/**
* Check if observer was paused.
*
* @returns {boolean}
*/
}, {
key: "isPaused",
value: function isPaused() {
return this.paused;
}
/**
* Pause observer (stop emitting all detected changes).
*/
}, {
key: "pause",
value: function pause() {
this.paused = true;
}
/**
* Resume observer (emit all detected changes).
*/
}, {
key: "resume",
value: function resume() {
this.paused = false;
}
/**
* JsonPatch on change listener.
*
* @private
* @param {Array} patches An array of object passed from jsonpatch.
*/
}, {
key: "onChange",
value: function onChange(patches) {
this.runLocalHooks('change', cleanPatches(patches));
}
/**
* Destroy observer instance.
*/
}, {
key: "destroy",
value: function destroy() {
jsonpatch.unobserve(this.observedData, this.observer);
this.observedData = null;
this.observer = null;
}
}]);
return DataObserver;
}();
mixin(DataObserver, localHooks);
export default DataObserver;