useLazyKVMap.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = useLazyKVMap;
  6. var _vue = require("vue");
  7. function useLazyKVMap(dataRef, childrenColumnNameRef, getRowKeyRef) {
  8. const mapCacheRef = (0, _vue.shallowRef)({});
  9. (0, _vue.watch)([dataRef, childrenColumnNameRef, getRowKeyRef], () => {
  10. const kvMap = new Map();
  11. const getRowKey = getRowKeyRef.value;
  12. const childrenColumnName = childrenColumnNameRef.value;
  13. /* eslint-disable no-inner-declarations */
  14. function dig(records) {
  15. records.forEach((record, index) => {
  16. const rowKey = getRowKey(record, index);
  17. kvMap.set(rowKey, record);
  18. if (record && typeof record === 'object' && childrenColumnName in record) {
  19. dig(record[childrenColumnName] || []);
  20. }
  21. });
  22. }
  23. /* eslint-enable */
  24. dig(dataRef.value);
  25. mapCacheRef.value = {
  26. kvMap
  27. };
  28. }, {
  29. deep: true,
  30. immediate: true
  31. });
  32. function getRecordByKey(key) {
  33. return mapCacheRef.value.kvMap.get(key);
  34. }
  35. return [getRecordByKey];
  36. }