2021-06-22 05:26:19 +00:00
|
|
|
import { Ref, ref, watch } from 'vue';
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|