2021-10-08 14:21:55 +00:00
|
|
|
import { onBeforeMount, onUnmounted, ref } from 'vue'
|
|
|
|
import type { Ref } from 'vue'
|
2021-08-27 13:30:53 +00:00
|
|
|
import { NuxtApp, useNuxtApp } from '#app'
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
export type _Transform<Input=any, Output=any> = (input: Input) => Output
|
|
|
|
|
|
|
|
export type PickFrom<T, K extends Array<string>> = T extends Record<string, any> ? Pick<T, K[number]> : T
|
2021-10-12 08:52:09 +00:00
|
|
|
export type KeysOf<T> = Array<keyof T extends string ? keyof T : string>
|
2021-10-11 22:36:50 +00:00
|
|
|
export type KeyOfRes<Transform extends _Transform> = KeysOf<ReturnType<Transform>>
|
|
|
|
|
|
|
|
export interface AsyncDataOptions<
|
|
|
|
DataT,
|
|
|
|
Transform extends _Transform<DataT, any> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<_Transform> = KeyOfRes<Transform>
|
|
|
|
> {
|
2021-01-18 12:46:19 +00:00
|
|
|
server?: boolean
|
|
|
|
defer?: boolean
|
2021-10-11 22:36:50 +00:00
|
|
|
default?: () => DataT
|
|
|
|
transform?: Transform
|
|
|
|
pick?: PickKeys
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
export interface _AsyncData<DataT> {
|
|
|
|
data: Ref<DataT>
|
2021-02-03 18:14:30 +00:00
|
|
|
pending: Ref<boolean>
|
2021-10-08 14:21:55 +00:00
|
|
|
refresh: (force?: boolean) => Promise<void>
|
2021-01-18 12:46:19 +00:00
|
|
|
error?: any
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
export type AsyncData<Data> = _AsyncData<Data> & Promise<_AsyncData<Data>>
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
const getDefault = () => null
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
export function useAsyncData<
|
|
|
|
DataT,
|
|
|
|
Transform extends _Transform<DataT> = _Transform<DataT, DataT>,
|
|
|
|
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>
|
|
|
|
> (
|
|
|
|
key: string,
|
|
|
|
handler: (ctx?: NuxtApp) => Promise<DataT>,
|
|
|
|
options: AsyncDataOptions<DataT, Transform, PickKeys> = {}
|
|
|
|
) : AsyncData<PickFrom<ReturnType<Transform>, PickKeys>> {
|
2021-10-08 14:21:55 +00:00
|
|
|
// Validate arguments
|
|
|
|
if (typeof key !== 'string') {
|
|
|
|
throw new TypeError('asyncData key must be a string')
|
|
|
|
}
|
|
|
|
if (typeof handler !== 'function') {
|
|
|
|
throw new TypeError('asyncData handler must be a function')
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Apply defaults
|
|
|
|
options = { server: true, defer: false, default: getDefault, ...options }
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Setup nuxt instance payload
|
|
|
|
const nuxt = useNuxtApp()
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Setup hook callbacks once per instance
|
|
|
|
const instance = getCurrentInstance()
|
|
|
|
if (!instance._nuxtOnBeforeMountCbs) {
|
|
|
|
const cbs = instance._nuxtOnBeforeMountCbs = []
|
|
|
|
if (instance && process.client) {
|
|
|
|
onBeforeMount(() => {
|
|
|
|
cbs.forEach((cb) => { cb() })
|
|
|
|
cbs.splice(0, cbs.length)
|
|
|
|
})
|
|
|
|
onUnmounted(() => cbs.splice(0, cbs.length))
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 09:17:18 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
const asyncData = {
|
|
|
|
data: ref(nuxt.payload.data[key] ?? options.default()),
|
|
|
|
pending: ref(true),
|
|
|
|
error: ref(null)
|
2021-10-11 22:36:50 +00:00
|
|
|
} as AsyncData<DataT>
|
2021-03-17 09:17:18 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.refresh = (force?: boolean) => {
|
|
|
|
// Avoid fetching same key more than once at a time
|
|
|
|
if (nuxt._asyncDataPromises[key] && !force) {
|
2021-04-03 10:03:20 +00:00
|
|
|
return nuxt._asyncDataPromises[key]
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.pending.value = true
|
|
|
|
// TODO: Cancel previus promise
|
|
|
|
// TODO: Handle immediate errors
|
|
|
|
nuxt._asyncDataPromises[key] = Promise.resolve(handler(nuxt))
|
|
|
|
.then((result) => {
|
2021-10-11 22:36:50 +00:00
|
|
|
if (options.transform) {
|
|
|
|
result = options.transform(result)
|
|
|
|
}
|
|
|
|
if (options.pick) {
|
|
|
|
result = pick(result, options.pick) as DataT
|
|
|
|
}
|
2021-10-08 14:21:55 +00:00
|
|
|
asyncData.data.value = result
|
|
|
|
asyncData.error.value = null
|
|
|
|
})
|
|
|
|
.catch((error: any) => {
|
|
|
|
asyncData.error.value = error
|
|
|
|
asyncData.data.value = options.default()
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
asyncData.pending.value = false
|
|
|
|
nuxt.payload.data[key] = asyncData.data.value
|
|
|
|
delete nuxt._asyncDataPromises[key]
|
|
|
|
})
|
|
|
|
return nuxt._asyncDataPromises[key]
|
|
|
|
}
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
const fetchOnServer = options.server !== false
|
|
|
|
const clientOnly = options.server === false
|
2021-01-18 12:46:19 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Server side
|
|
|
|
if (process.server && fetchOnServer) {
|
|
|
|
asyncData.refresh()
|
|
|
|
}
|
2021-04-03 10:03:20 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Client side
|
|
|
|
if (process.client) {
|
|
|
|
// 1. Hydration (server: true): no fetch
|
|
|
|
if (nuxt.isHydrating && fetchOnServer) {
|
|
|
|
asyncData.pending.value = false
|
|
|
|
}
|
|
|
|
// 2. Initial load (server: false): fetch on mounted
|
|
|
|
if (nuxt.isHydrating && clientOnly) {
|
|
|
|
// Fetch on mounted (initial load or deferred fetch)
|
|
|
|
instance._nuxtOnBeforeMountCbs.push(asyncData.refresh)
|
|
|
|
} else if (!nuxt.isHydrating) { // Navigation
|
|
|
|
if (options.defer) {
|
|
|
|
// 3. Navigation (defer: true): fetch on mounted
|
|
|
|
instance._nuxtOnBeforeMountCbs.push(asyncData.refresh)
|
|
|
|
} else {
|
|
|
|
// 4. Navigation (defer: false): await fetch
|
|
|
|
asyncData.refresh()
|
2021-01-18 12:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 09:45:37 +00:00
|
|
|
|
2021-10-08 14:21:55 +00:00
|
|
|
// Allow directly awaiting on asyncData
|
2021-10-11 22:36:50 +00:00
|
|
|
const asyncDataPromise = Promise.resolve(nuxt._asyncDataPromises[key]).then(() => asyncData) as AsyncData<DataT>
|
2021-10-08 14:21:55 +00:00
|
|
|
Object.assign(asyncDataPromise, asyncData)
|
|
|
|
|
2021-10-11 22:36:50 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return asyncDataPromise as AsyncData<DataT>
|
|
|
|
}
|
|
|
|
|
|
|
|
function pick (obj: Record<string, any>, keys: string[]) {
|
|
|
|
const newObj = {}
|
|
|
|
for (const key of keys) {
|
|
|
|
newObj[key] = obj[key]
|
|
|
|
}
|
|
|
|
return newObj
|
2021-03-01 09:45:37 +00:00
|
|
|
}
|