computed.js 1.1 KB

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