This commit is contained in:
Rohan Dhimal 2024-11-20 06:35:52 -05:00 committed by GitHub
commit f9b003d9cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 8 additions and 27 deletions

View File

@ -106,10 +106,6 @@ export interface AsyncDataExecuteOptions {
export interface _AsyncData<DataT, ErrorT> { export interface _AsyncData<DataT, ErrorT> {
data: Ref<DataT> data: Ref<DataT>
/**
* @deprecated Use `status` instead. This may be removed in a future major version.
*/
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 clear: () => void
@ -249,7 +245,6 @@ export function useAsyncData<
const _ref = options.deep ? ref : shallowRef const _ref = options.deep ? ref : shallowRef
nuxtApp._asyncData[key] = { nuxtApp._asyncData[key] = {
data: _ref(hasCachedData ? initialCachedData : options.default!()), data: _ref(hasCachedData ? initialCachedData : options.default!()),
pending: ref(!hasCachedData),
error: toRef(nuxtApp.payload._errors, key), error: toRef(nuxtApp.payload._errors, key),
status: ref('idle'), status: ref('idle'),
_default: options.default!, _default: options.default!,
@ -277,7 +272,6 @@ export function useAsyncData<
return Promise.resolve(cachedData) return Promise.resolve(cachedData)
} }
} }
asyncData.pending.value = true
asyncData.status.value = 'pending' asyncData.status.value = 'pending'
// TODO: Cancel previous promise // TODO: Cancel previous promise
const promise = new Promise<ResT>( const promise = new Promise<ResT>(
@ -322,8 +316,6 @@ export function useAsyncData<
.finally(() => { .finally(() => {
if ((promise as any).cancelled) { return } if ((promise as any).cancelled) { return }
asyncData.pending.value = false
delete nuxtApp._asyncDataPromises[key] delete nuxtApp._asyncDataPromises[key]
}) })
nuxtApp._asyncDataPromises[key] = promise nuxtApp._asyncDataPromises[key] = promise
@ -366,7 +358,6 @@ export function useAsyncData<
if (fetchOnServer && nuxtApp.isHydrating && (asyncData.error.value || typeof initialCachedData !== 'undefined')) { if (fetchOnServer && nuxtApp.isHydrating && (asyncData.error.value || typeof initialCachedData !== 'undefined')) {
// 1. Hydration (server: true): no fetch // 1. Hydration (server: true): no fetch
asyncData.pending.value = false
asyncData.status.value = asyncData.error.value ? 'error' : 'success' asyncData.status.value = asyncData.error.value ? 'error' : 'success'
} else if (instance && ((nuxtApp.payload.serverRendered && nuxtApp.isHydrating) || options.lazy) && options.immediate) { } else if (instance && ((nuxtApp.payload.serverRendered && nuxtApp.isHydrating) || options.lazy) && options.immediate) {
// 2. Initial load (server: false): fetch on mounted // 2. Initial load (server: false): fetch on mounted
@ -527,7 +518,6 @@ function clearNuxtDataByKey (nuxtApp: NuxtApp, key: string): void {
if (nuxtApp._asyncData[key]) { if (nuxtApp._asyncData[key]) {
nuxtApp._asyncData[key]!.data.value = nuxtApp._asyncData[key]!._default() nuxtApp._asyncData[key]!.data.value = nuxtApp._asyncData[key]!._default()
nuxtApp._asyncData[key]!.error.value = undefined nuxtApp._asyncData[key]!.error.value = undefined
nuxtApp._asyncData[key]!.pending.value = false
nuxtApp._asyncData[key]!.status.value = 'idle' nuxtApp._asyncData[key]!.status.value = 'idle'
} }

View File

@ -126,10 +126,6 @@ interface _NuxtApp {
/** @internal */ /** @internal */
_asyncData: Record<string, { _asyncData: Record<string, {
data: Ref<unknown> data: Ref<unknown>
/**
* @deprecated This may be removed in a future major version.
*/
pending: Ref<boolean>
error: Ref<Error | undefined> error: Ref<Error | undefined>
status: Ref<AsyncDataRequestStatus> status: Ref<AsyncDataRequestStatus>
/** @internal */ /** @internal */

View File

@ -1,6 +1,6 @@
<template> <template>
<div <div
v-if="!pending" v-if="status !== 'pending'"
v-text="'resolved:' + data.resolved" v-text="'resolved:' + data.resolved"
/> />
<div <div
@ -11,5 +11,5 @@
<script setup> <script setup>
useNuxtData('call') useNuxtData('call')
const { data, pending } = await useAsyncData('call', () => Promise.resolve({ resolved: true }), { server: false }) const { data, status } = await useAsyncData('call', () => Promise.resolve({ resolved: true }), { server: false })
</script> </script>

View File

@ -17,7 +17,7 @@ const ComponentDefinedInSetup = computed(() => h({
template: compTemplate.value, template: compTemplate.value,
}) as Component) }) as Component)
const { data, pending } = await useAsyncData('templates', async () => { const { data, status } = await useAsyncData('templates', async () => {
const [interactiveComponent, templateString] = await Promise.all([ const [interactiveComponent, templateString] = await Promise.all([
$fetch('/api/full-component'), $fetch('/api/full-component'),
$fetch('/api/template'), $fetch('/api/template'),
@ -54,7 +54,7 @@ const Interactive = h({
> >
{{ count }} {{ count }}
</button> </button>
<template v-if="!pending"> <template v-if="status !== 'pending'">
<Name <Name
id="name" id="name"
template="<div>I am the Name.ts component</div>" template="<div>I am the Name.ts component</div>"

View File

@ -131,7 +131,6 @@ describe('useAsyncData', () => {
expect(Object.keys(res)).toMatchInlineSnapshot(` expect(Object.keys(res)).toMatchInlineSnapshot(`
[ [
"data", "data",
"pending",
"error", "error",
"status", "status",
"execute", "execute",
@ -149,29 +148,25 @@ describe('useAsyncData', () => {
const immediate = await useAsyncData(() => Promise.resolve('test')) const immediate = await useAsyncData(() => Promise.resolve('test'))
expect(immediate.data.value).toBe('test') expect(immediate.data.value).toBe('test')
expect(immediate.status.value).toBe('success') expect(immediate.status.value).toBe('success')
expect(immediate.pending.value).toBe(false)
const nonimmediate = await useAsyncData(() => Promise.resolve('test'), { immediate: false }) const nonimmediate = await useAsyncData(() => Promise.resolve('test'), { immediate: false })
expect(nonimmediate.data.value).toBe(undefined) expect(nonimmediate.data.value).toBe(undefined)
expect(nonimmediate.status.value).toBe('idle') expect(nonimmediate.status.value).toBe('idle')
expect(nonimmediate.pending.value).toBe(true)
}) })
it('should capture errors', async () => { it('should capture errors', async () => {
const { data, error, status, pending } = await useAsyncData('error-test', () => Promise.reject(new Error('test')), { default: () => 'default' }) const { data, error, status } = await useAsyncData('error-test', () => Promise.reject(new Error('test')), { default: () => 'default' })
expect(data.value).toMatchInlineSnapshot('"default"') expect(data.value).toMatchInlineSnapshot('"default"')
expect(error.value).toMatchInlineSnapshot('[Error: test]') expect(error.value).toMatchInlineSnapshot('[Error: test]')
expect(status.value).toBe('error') expect(status.value).toBe('error')
expect(pending.value).toBe(false)
expect(useNuxtApp().payload._errors['error-test']).toMatchInlineSnapshot('[Error: test]') expect(useNuxtApp().payload._errors['error-test']).toMatchInlineSnapshot('[Error: test]')
// TODO: fix the below // TODO: fix the below
// const { data: syncedData, error: syncedError, status: syncedStatus, pending: syncedPending } = await useAsyncData('error-test', () => ({}), { immediate: false }) // const { data: syncedData, error: syncedError, status: syncedStatus } = await useAsyncData('error-test', () => ({}), { immediate: false })
// expect(syncedData.value).toEqual(null) // expect(syncedData.value).toEqual(null)
// expect(syncedError.value).toEqual(error.value) // expect(syncedError.value).toEqual(error.value)
// expect(syncedStatus.value).toEqual('idle') // expect(syncedStatus.value).toEqual('idle')
// expect(syncedPending.value).toEqual(true)
}) })
// https://github.com/nuxt/nuxt/issues/23411 // https://github.com/nuxt/nuxt/issues/23411
@ -248,9 +243,9 @@ describe('useAsyncData', () => {
it('should use default while pending', async () => { it('should use default while pending', async () => {
const promise = useAsyncData(() => Promise.resolve('test'), { default: () => 'default' }) const promise = useAsyncData(() => Promise.resolve('test'), { default: () => 'default' })
const { data, pending } = promise const { data, status } = promise
expect(pending.value).toBe(true) expect(status.value).toBe('pending')
expect(data.value).toMatchInlineSnapshot('"default"') expect(data.value).toMatchInlineSnapshot('"default"')
await promise await promise