computed.js 987 B

12345678910111213141516171819202122232425262728293031323334
  1. /* eslint-disable @typescript-eslint/no-explicit-any */
  2. import deepEqual from 'fast-deep-equal';
  3. function computedData() {
  4. var _this = this;
  5. var nextData = this.computed(this.props);
  6. // 浅比较就行了
  7. var changedData = Object.keys(nextData).reduce(function (prev, item) {
  8. // 移除 _ $ 开头的保留 props
  9. if (item[0] === '_' || item[0] === '$') {
  10. return prev;
  11. }
  12. if (typeof nextData[item] === 'function') {
  13. return prev;
  14. }
  15. if (deepEqual(_this.data[item], nextData[item])) {
  16. return prev;
  17. }
  18. // eslint-disable-next-line no-param-reassign
  19. prev[item] = nextData[item];
  20. return prev;
  21. }, {});
  22. if (Object.keys(changedData).length === 0) {
  23. return;
  24. }
  25. this.setData(changedData);
  26. }
  27. export default {
  28. didMount: function () {
  29. computedData.call(this);
  30. },
  31. didUpdate: function () {
  32. computedData.call(this);
  33. },
  34. };