-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useRequest custom cache support async getCache #182
Comments
能否给一个示例,否则不知道你的使用场景和需求是什么 |
因为想使用 localforage 进行数据缓存,而 localforage 的 getItem 是一个异步行为 |
或许你可以自己使用 Promise 包裹一层,这边暂时不考虑异步行为。 |
你的写法有点不对,明天我尝试还原你的使用场景给你一个答复。感谢你的支持~ |
@BreathlessWay 后续会考虑支持 |
目前 getCache 确实不支持异步的。 |
@BreathlessWay 我们将在 2.x 探索如何进行优雅的异步获取缓存 |
@BreathlessWay 经过一段时间的调研,得出一个结论:如果在内部进行异步缓存的改造,对整个 useRequest 的功能会造成比较大的破坏性。 如果针对异步的缓存,如何在现有的基础上以最小成本实现呢? 我针对现有的功能实现了一个方案,自定义程度比较高,应该可以满足你现有的需求。它的缺点也很明显,会让你的心智负担增加。 <template>
<div>
<vhp-button @click="() => toggle()">show/hidden</vhp-button>
<div v-if="state" style="padding: 16px;">
<p>{{ data ?? cacheData }}</p>
<div> cacheData: {{ cacheData }} </div>
</div>
</div>
</template>
<script lang="ts" setup>
import { useRequest, useToggle } from 'vue-hooks-plus'
const cacheKey = 'cacheKey-async-storage'
const asyncStorage = {
setItem: (key: string, value: any) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
localStorage.setItem(key, value)
resolve(value)
}, 200)
})
},
getItem: (key: string) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(localStorage.getItem(key))
}, 100)
})
},
}
const [state, { toggle }] = useToggle()
const { data: cacheData, refresh } = useRequest(
() => asyncStorage.getItem(cacheKey) as Promise<string>,
{
cacheKey,
debugKey: cacheKey,
},
)
function getUsername(refresh: VoidFunction): Promise<string> {
return new Promise(resolve => {
setTimeout(() => {
asyncStorage.setItem(cacheKey, String(Date.now())).then(() => {
refresh()
})
resolve(String(Date.now()))
}, 1000)
})
}
const { data } = useRequest(() => getUsername(refresh), {
ready: state,
cacheKey: 'cacheKey-get-username',
})
</script> |
Description
能否支持异步调用的 getCache
Suggested solution
useCachePlugin.js 中的 方法 getCache 使用 async await 调用
Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: