import type { ComputedRef, Ref } from 'vue'; import { ref } from 'vue'; import { watchEffect } from 'vue'; import type { FlattenDataNode, Key, RawValueType } from '../interface'; /** * Return cached Key Value map with DataNode. * Only re-calculate when `flattenOptions` changed. */ export default function useKeyValueMap(flattenOptions: ComputedRef) { const cacheKeyMap: Ref> = ref(new Map()); const cacheValueMap: Ref> = ref(new Map()); watchEffect(() => { const newCacheKeyMap = new Map(); const newCacheValueMap = new Map(); // Cache options by key flattenOptions.value.forEach((dataNode: FlattenDataNode) => { newCacheKeyMap.set(dataNode.key, dataNode); newCacheValueMap.set(dataNode.data.value, dataNode); }); cacheKeyMap.value = newCacheKeyMap; cacheValueMap.value = newCacheValueMap; }); return [cacheKeyMap, cacheValueMap]; }