2023-03-31 00:49:45 +02:00
|
|
|
import { computedAsync, watchThrottled } from '@vueuse/core';
|
2023-03-29 20:51:48 +02:00
|
|
|
import { computed, ref, watch } from 'vue';
|
|
|
|
|
|
|
|
export { computedRefreshable, computedRefreshableAsync };
|
|
|
|
|
2023-03-31 00:49:45 +02:00
|
|
|
function computedRefreshable<T>(getter: () => T, { throttle }: { throttle?: number } = {}) {
|
2023-03-29 20:51:48 +02:00
|
|
|
const dirty = ref(true);
|
|
|
|
let value: T;
|
|
|
|
|
|
|
|
const update = () => (dirty.value = true);
|
|
|
|
|
2023-03-31 00:49:45 +02:00
|
|
|
if (throttle) {
|
|
|
|
watchThrottled(getter, update, { throttle });
|
2023-05-28 23:13:24 +02:00
|
|
|
}
|
|
|
|
else {
|
2023-03-31 00:49:45 +02:00
|
|
|
watch(getter, update);
|
|
|
|
}
|
2023-03-29 20:51:48 +02:00
|
|
|
|
|
|
|
const computedValue = computed(() => {
|
|
|
|
if (dirty.value) {
|
|
|
|
value = getter();
|
|
|
|
dirty.value = false;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [computedValue, update] as const;
|
|
|
|
}
|
|
|
|
|
|
|
|
function computedRefreshableAsync<T>(getter: () => Promise<T>, defaultValue?: T) {
|
|
|
|
const dirty = ref(true);
|
|
|
|
let value: T;
|
|
|
|
|
|
|
|
const update = () => (dirty.value = true);
|
|
|
|
|
|
|
|
watch(getter, update);
|
|
|
|
|
|
|
|
const computedValue = computedAsync(async () => {
|
|
|
|
if (dirty.value) {
|
|
|
|
value = await getter();
|
|
|
|
dirty.value = false;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}, defaultValue);
|
|
|
|
|
|
|
|
return [computedValue, update] as const;
|
|
|
|
}
|