mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt): add clear
utility to useAsyncData
/useFetch
(#26259)
This commit is contained in:
parent
3c7e68c846
commit
02d6838293
@ -150,6 +150,7 @@ Read more about `useAsyncData`.
|
|||||||
- `data`: the result of the asynchronous function that is passed in.
|
- `data`: the result of the asynchronous function that is passed in.
|
||||||
- `pending`: a boolean indicating whether the data is still being fetched.
|
- `pending`: a boolean indicating whether the data is still being fetched.
|
||||||
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
||||||
|
- `clear`: a function that can be used to set `data` to undefined, set `error` to `null`, set `pending` to `false`, set `status` to `idle`, and mark any currently pending requests as cancelled.
|
||||||
- `error`: an error object if the data fetching failed.
|
- `error`: an error object if the data fetching failed.
|
||||||
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
||||||
|
|
||||||
|
@ -130,6 +130,7 @@ type AsyncData<DataT, ErrorT> = {
|
|||||||
pending: Ref<boolean>
|
pending: Ref<boolean>
|
||||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
|
clear: () => void
|
||||||
error: Ref<ErrorT | null>
|
error: Ref<ErrorT | null>
|
||||||
status: Ref<AsyncDataRequestStatus>
|
status: Ref<AsyncDataRequestStatus>
|
||||||
};
|
};
|
||||||
|
@ -158,6 +158,7 @@ type AsyncData<DataT, ErrorT> = {
|
|||||||
pending: Ref<boolean>
|
pending: Ref<boolean>
|
||||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
|
clear: () => void
|
||||||
error: Ref<ErrorT | null>
|
error: Ref<ErrorT | null>
|
||||||
status: Ref<AsyncDataRequestStatus>
|
status: Ref<AsyncDataRequestStatus>
|
||||||
}
|
}
|
||||||
|
@ -111,6 +111,7 @@ export interface _AsyncData<DataT, ErrorT> {
|
|||||||
pending: Ref<boolean>
|
pending: Ref<boolean>
|
||||||
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
||||||
|
clear: () => void
|
||||||
error: Ref<ErrorT | null>
|
error: Ref<ErrorT | null>
|
||||||
status: Ref<AsyncDataRequestStatus>
|
status: Ref<AsyncDataRequestStatus>
|
||||||
}
|
}
|
||||||
@ -221,8 +222,9 @@ export function useAsyncData<
|
|||||||
if (value) { return value as Promise<ResT> }
|
if (value) { return value as Promise<ResT> }
|
||||||
|
|
||||||
const promise = nuxtApp.runWithContext(_handler)
|
const promise = nuxtApp.runWithContext(_handler)
|
||||||
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
|
||||||
return promise
|
nuxtApp.ssrContext!._sharedPrerenderCache!.set(key, promise)
|
||||||
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to get default values
|
// Used to get default values
|
||||||
@ -322,6 +324,8 @@ export function useAsyncData<
|
|||||||
return nuxtApp._asyncDataPromises[key]!
|
return nuxtApp._asyncDataPromises[key]!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
asyncData.clear = () => clearNuxtDataByKey(nuxtApp, key)
|
||||||
|
|
||||||
const initialFetch = () => asyncData.refresh({ _initial: true })
|
const initialFetch = () => asyncData.refresh({ _initial: true })
|
||||||
|
|
||||||
const fetchOnServer = options.server !== false && nuxtApp.payload.serverRendered
|
const fetchOnServer = options.server !== false && nuxtApp.payload.serverRendered
|
||||||
@ -499,21 +503,29 @@ export function clearNuxtData (keys?: string | string[] | ((key: string) => bool
|
|||||||
: toArray(keys)
|
: toArray(keys)
|
||||||
|
|
||||||
for (const key of _keys) {
|
for (const key of _keys) {
|
||||||
if (key in nuxtApp.payload.data) {
|
clearNuxtDataByKey(nuxtApp, key)
|
||||||
nuxtApp.payload.data[key] = undefined
|
}
|
||||||
}
|
}
|
||||||
if (key in nuxtApp.payload._errors) {
|
|
||||||
nuxtApp.payload._errors[key] = null
|
function clearNuxtDataByKey (nuxtApp: NuxtApp, key: string): void {
|
||||||
}
|
if (key in nuxtApp.payload.data) {
|
||||||
if (nuxtApp._asyncData[key]) {
|
nuxtApp.payload.data[key] = undefined
|
||||||
nuxtApp._asyncData[key]!.data.value = undefined
|
}
|
||||||
nuxtApp._asyncData[key]!.error.value = null
|
|
||||||
nuxtApp._asyncData[key]!.pending.value = false
|
if (key in nuxtApp.payload._errors) {
|
||||||
nuxtApp._asyncData[key]!.status.value = 'idle'
|
nuxtApp.payload._errors[key] = null
|
||||||
}
|
}
|
||||||
if (key in nuxtApp._asyncDataPromises) {
|
|
||||||
nuxtApp._asyncDataPromises[key] = undefined
|
if (nuxtApp._asyncData[key]) {
|
||||||
}
|
nuxtApp._asyncData[key]!.data.value = undefined
|
||||||
|
nuxtApp._asyncData[key]!.error.value = null
|
||||||
|
nuxtApp._asyncData[key]!.pending.value = false
|
||||||
|
nuxtApp._asyncData[key]!.status.value = 'idle'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key in nuxtApp._asyncDataPromises) {
|
||||||
|
(nuxtApp._asyncDataPromises[key] as any).cancelled = true
|
||||||
|
nuxtApp._asyncDataPromises[key] = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +123,7 @@ describe('useAsyncData', () => {
|
|||||||
"status",
|
"status",
|
||||||
"execute",
|
"execute",
|
||||||
"refresh",
|
"refresh",
|
||||||
|
"clear",
|
||||||
]
|
]
|
||||||
`)
|
`)
|
||||||
expect(res instanceof Promise).toBeTruthy()
|
expect(res instanceof Promise).toBeTruthy()
|
||||||
@ -200,6 +201,18 @@ describe('useAsyncData', () => {
|
|||||||
expect(data.data.value).toMatchInlineSnapshot('"test"')
|
expect(data.data.value).toMatchInlineSnapshot('"test"')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should be clearable', async () => {
|
||||||
|
const { data, error, pending, status, clear } = await useAsyncData(() => Promise.resolve('test'))
|
||||||
|
expect(data.value).toBe('test')
|
||||||
|
|
||||||
|
clear()
|
||||||
|
|
||||||
|
expect(data.value).toBeUndefined()
|
||||||
|
expect(error.value).toBeNull()
|
||||||
|
expect(pending.value).toBe(false)
|
||||||
|
expect(status.value).toBe('idle')
|
||||||
|
})
|
||||||
|
|
||||||
it('allows custom access to a cache', async () => {
|
it('allows custom access to a cache', async () => {
|
||||||
const { data } = await useAsyncData(() => ({ val: true }), { getCachedData: () => ({ val: false }) })
|
const { data } = await useAsyncData(() => ({ val: true }), { getCachedData: () => ({ val: false }) })
|
||||||
expect(data.value).toMatchInlineSnapshot(`
|
expect(data.value).toMatchInlineSnapshot(`
|
||||||
|
Loading…
Reference in New Issue
Block a user