feat: add asyncData and improve reactivity (#174)

* feat: add asyncData and improve reactivity

* chore: lint fix
This commit is contained in:
Sébastien Chopin 2021-03-01 10:45:37 +01:00 committed by GitHub
parent 040e14f2b6
commit 5248c61ed0
2 changed files with 17 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { Ref, ref, toRef, onMounted, watch, getCurrentInstance, onUnmounted } from 'vue' import { Ref, ref, onMounted, watch, getCurrentInstance, onUnmounted } from 'vue'
import { Nuxt, useNuxt } from 'nuxt/app' import { Nuxt, useNuxt } from 'nuxt/app'
import { useData } from './data' import { useData } from './data'
@ -58,7 +58,12 @@ export function useAsyncData (defaults?: AsyncDataOptions) {
if (_handler instanceof Promise) { if (_handler instanceof Promise) {
// Let user resolve if request is promise // Let user resolve if request is promise
// TODO: handle error // TODO: handle error
data[key] = await _handler const result = await _handler
if (!data[key]) {
data[key] = result
} else {
Object.assign(data[key], result)
}
pending.value = false pending.value = false
} else { } else {
// Invalid request // Invalid request
@ -76,10 +81,14 @@ export function useAsyncData (defaults?: AsyncDataOptions) {
} }
// 2. Initial load (server: false): fetch on mounted // 2. Initial load (server: false): fetch on mounted
if (nuxt.isHydrating && !options.server) { if (nuxt.isHydrating && !options.server) {
// Force tracking it
data[key] = {}
// Fetch on mounted (initial load or deferred fetch) // Fetch on mounted (initial load or deferred fetch)
onMountedCbs.push(fetch) onMountedCbs.push(fetch)
} else if (!nuxt.isHydrating) { } else if (!nuxt.isHydrating) {
if (options.defer) { if (options.defer) {
// Force tracking it
data[key] = {}
// 3. Navigation (defer: true): fetch on mounted // 3. Navigation (defer: true): fetch on mounted
onMountedCbs.push(fetch) onMountedCbs.push(fetch)
} else { } else {
@ -95,11 +104,14 @@ export function useAsyncData (defaults?: AsyncDataOptions) {
if (process.server && !clientOnly) { if (process.server && !clientOnly) {
await fetch() await fetch()
} }
return { return {
data: toRef<any, string>(data, key), data: data[key],
pending, pending,
refresh: fetch refresh: fetch
} }
} }
} }
export function asyncData<T = any> (handler: AsyncDataFn<T>, options?: AsyncDataOptions): Promise<AsyncDataObj<T>> {
return useAsyncData()(handler, options)
}

View File

@ -1,3 +1,3 @@
export { useAsyncData } from './asyncData' export { useAsyncData, asyncData } from './asyncData'
export { useData } from './data' export { useData } from './data'
export { useHydration } from './hydrate' export { useHydration } from './hydrate'