ant-design-vue/components/_util/hooks/useMemo.ts

22 lines
479 B
TypeScript
Raw Normal View History

2021-06-26 01:35:40 +00:00
import type { Ref } from 'vue';
import { ref, watch } from 'vue';
2021-06-22 05:26:19 +00:00
export default function useMemo<T>(
getValue: () => T,
condition: any[],
2021-06-24 03:03:02 +00:00
shouldUpdate?: (prev: any[], next: any[]) => boolean,
2021-06-22 05:26:19 +00:00
) {
const cacheRef: Ref<T> = ref(getValue() as any);
2021-06-24 03:03:02 +00:00
watch(condition, (next, pre) => {
if (shouldUpdate) {
if (shouldUpdate(next, pre)) {
cacheRef.value = getValue();
}
} else {
2021-06-22 05:26:19 +00:00
cacheRef.value = getValue();
}
});
return cacheRef;
}