useMaxLevel.js 968 B

1234567891011121314151617181920212223242526272829303132
  1. import { shallowRef, ref, watchEffect } from 'vue';
  2. export default function useMaxLevel(keyEntities) {
  3. const maxLevel = ref(0);
  4. const levelEntities = shallowRef();
  5. watchEffect(() => {
  6. const newLevelEntities = new Map();
  7. let newMaxLevel = 0;
  8. const keyEntitiesValue = keyEntities.value || {};
  9. // Convert entities by level for calculation
  10. for (const key in keyEntitiesValue) {
  11. if (Object.prototype.hasOwnProperty.call(keyEntitiesValue, key)) {
  12. const entity = keyEntitiesValue[key];
  13. const {
  14. level
  15. } = entity;
  16. let levelSet = newLevelEntities.get(level);
  17. if (!levelSet) {
  18. levelSet = new Set();
  19. newLevelEntities.set(level, levelSet);
  20. }
  21. levelSet.add(entity);
  22. newMaxLevel = Math.max(newMaxLevel, level);
  23. }
  24. }
  25. maxLevel.value = newMaxLevel;
  26. levelEntities.value = newLevelEntities;
  27. });
  28. return {
  29. maxLevel,
  30. levelEntities
  31. };
  32. }