useLazyKVMap.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import _typeof from "@babel/runtime/helpers/esm/typeof";
  2. import { watch, shallowRef } from 'vue';
  3. export default function useLazyKVMap(dataRef, childrenColumnNameRef, getRowKeyRef) {
  4. var mapCacheRef = shallowRef({});
  5. watch([dataRef, childrenColumnNameRef, getRowKeyRef], function () {
  6. var kvMap = new Map();
  7. var getRowKey = getRowKeyRef.value;
  8. var childrenColumnName = childrenColumnNameRef.value;
  9. /* eslint-disable no-inner-declarations */
  10. function dig(records) {
  11. records.forEach(function (record, index) {
  12. var rowKey = getRowKey(record, index);
  13. kvMap.set(rowKey, record);
  14. if (record && _typeof(record) === 'object' && childrenColumnName in record) {
  15. dig(record[childrenColumnName] || []);
  16. }
  17. });
  18. }
  19. /* eslint-enable */
  20. dig(dataRef.value);
  21. mapCacheRef.value = {
  22. kvMap: kvMap
  23. };
  24. }, {
  25. deep: true,
  26. immediate: true
  27. });
  28. function getRecordByKey(key) {
  29. return mapCacheRef.value.kvMap.get(key);
  30. }
  31. return [getRecordByKey];
  32. }