useMemo.js 361 B

1234567891011121314
  1. import { ref, watch } from 'vue';
  2. export default function useMemo(getValue, condition, shouldUpdate) {
  3. const cacheRef = ref(getValue());
  4. watch(condition, (next, pre) => {
  5. if (shouldUpdate) {
  6. if (shouldUpdate(next, pre)) {
  7. cacheRef.value = getValue();
  8. }
  9. } else {
  10. cacheRef.value = getValue();
  11. }
  12. });
  13. return cacheRef;
  14. }