You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
520 B
22 lines
520 B
import type { Ref, WatchSource } from 'vue';
|
|
import { ref, watch } from 'vue';
|
|
|
|
export default function useMemo<T>(
|
|
getValue: () => T,
|
|
condition: (WatchSource<unknown> | object)[],
|
|
shouldUpdate?: (prev: any[], next: any[]) => boolean,
|
|
) {
|
|
const cacheRef: Ref<T> = ref(getValue() as any);
|
|
watch(condition, (next, pre) => {
|
|
if (shouldUpdate) {
|
|
if (shouldUpdate(next, pre)) {
|
|
cacheRef.value = getValue();
|
|
}
|
|
} else {
|
|
cacheRef.value = getValue();
|
|
}
|
|
});
|
|
|
|
return cacheRef;
|
|
}
|