observerHandle.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  3. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  6. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  7. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  8. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  9. var equals = function equals(x, y) {
  10. if (x === y) {
  11. return true;
  12. }
  13. if (!(x instanceof Object) || !(y instanceof Object)) {
  14. return false;
  15. }
  16. if (x.constructor !== y.constructor) {
  17. return false;
  18. }
  19. for (var p in x) {
  20. if (x.hasOwnProperty(p)) {
  21. if (!y.hasOwnProperty(p)) {
  22. return false;
  23. }
  24. if (x[p] === y[p]) {
  25. continue;
  26. }
  27. if (_typeof(x[p]) !== 'object') {
  28. return false;
  29. }
  30. if (!equals(x[p], y[p])) {
  31. return false;
  32. }
  33. }
  34. }
  35. for (var _p in y) {
  36. if (y.hasOwnProperty(_p) && !x.hasOwnProperty(_p)) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. };
  42. function observerHandle(observerObj, args, that) {
  43. Object.keys(observerObj).forEach(function (obs) {
  44. if (typeof observerObj[obs] === 'function') {
  45. var props;
  46. if (args.props) {
  47. props = args.props;
  48. }
  49. if (args[0]) {
  50. props = args[0];
  51. }
  52. if (!props) {
  53. return;
  54. }
  55. if (!equals(props[obs], that.props[obs])) {
  56. observerObj[obs].call(that, that.props[obs], props[obs]);
  57. }
  58. }
  59. });
  60. }
  61. function observersHandle(observersObj, args, that) {
  62. var preData = null;
  63. if (Array.isArray(args)) {
  64. preData = args[1];
  65. } else {
  66. preData = args.props;
  67. }
  68. Object.keys(observersObj).forEach(function (obs) {
  69. var left = {};
  70. var right = {};
  71. if (obs.match(/\./)) {
  72. var _dataArr = obs.split('.');
  73. left = processChildAttr(preData, _dataArr);
  74. right = processChildAttr(that.data, _dataArr);
  75. } else {
  76. left = preData[obs];
  77. right = that.data[obs];
  78. }
  79. var dif = equals(left, right);
  80. if (!dif) {
  81. var _observersObj$obs$fn;
  82. (_observersObj$obs$fn = observersObj[obs].fn).call.apply(_observersObj$obs$fn, [that].concat(_toConsumableArray(observersObj[obs].arr)));
  83. }
  84. });
  85. }
  86. function processChildAttr(attr, arr) {
  87. var _ = attr;
  88. arr.forEach(function (name) {
  89. _ = _[name];
  90. });
  91. return _;
  92. }
  93. module.exports = {
  94. observerHandle: observerHandle,
  95. observersHandle: observersHandle
  96. };