12个原创工具(批量条码/二维码识别/在线翻译/OCR/面单设计/AI修图/网站收藏/PDF工具/图片工具/文档转换等) + 卡密授权系统(CardAuth对接,30分钟试用,换卡/撤销) + 体验优化(中文默认/收藏/最近使用/菜单展开/宽屏/品牌页脚)
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
import { computedAsync, watchThrottled } from '@vueuse/core';
|
|
import { computed, ref, watch } from 'vue';
|
|
|
|
export { computedRefreshable, computedRefreshableAsync };
|
|
|
|
function computedRefreshable<T>(getter: () => T, { throttle }: { throttle?: number } = {}) {
|
|
const dirty = ref(true);
|
|
let value: T;
|
|
|
|
const update = () => (dirty.value = true);
|
|
|
|
if (throttle) {
|
|
watchThrottled(getter, update, { throttle });
|
|
}
|
|
else {
|
|
watch(getter, update);
|
|
}
|
|
|
|
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;
|
|
}
|