useLazyKVMap.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var React = _interopRequireWildcard(require("react"));
  8. const useLazyKVMap = (data, childrenColumnName, getRowKey) => {
  9. const mapCacheRef = React.useRef({});
  10. function getRecordByKey(key) {
  11. var _a;
  12. if (!mapCacheRef.current || mapCacheRef.current.data !== data || mapCacheRef.current.childrenColumnName !== childrenColumnName || mapCacheRef.current.getRowKey !== getRowKey) {
  13. const kvMap = new Map();
  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. dig(data);
  24. mapCacheRef.current = {
  25. data,
  26. childrenColumnName,
  27. kvMap,
  28. getRowKey
  29. };
  30. }
  31. return (_a = mapCacheRef.current.kvMap) === null || _a === void 0 ? void 0 : _a.get(key);
  32. }
  33. return [getRecordByKey];
  34. };
  35. var _default = exports.default = useLazyKVMap;