2020-02-04 18:36:22 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import { hasFetch, normalizeError, addLifecycleHook } from '../utils'
|
|
|
|
|
2020-02-12 15:41:23 +00:00
|
|
|
const isSsrHydration = (vm) => vm.$vnode && vm.$vnode.elm && vm.$vnode.elm.dataset && vm.$vnode.elm.dataset.fetchKey
|
2020-02-04 18:36:22 +00:00
|
|
|
const nuxtState = window.<%= globals.context %>
|
|
|
|
|
|
|
|
export default {
|
|
|
|
beforeCreate () {
|
|
|
|
if (!hasFetch(this)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._fetchDelay = typeof this.$options.fetchDelay === 'number' ? this.$options.fetchDelay : 200
|
|
|
|
|
|
|
|
Vue.util.defineReactive(this, '$fetchState', {
|
|
|
|
pending: false,
|
|
|
|
error: null,
|
|
|
|
timestamp: Date.now()
|
|
|
|
})
|
|
|
|
|
|
|
|
this.$fetch = $fetch.bind(this)
|
|
|
|
addLifecycleHook(this, 'created', created)
|
|
|
|
addLifecycleHook(this, 'beforeMount', beforeMount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function beforeMount() {
|
|
|
|
if (!this._hydrated) {
|
|
|
|
return this.$fetch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function created() {
|
|
|
|
if (!isSsrHydration(this)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hydrate component
|
|
|
|
this._hydrated = true
|
2020-02-12 15:41:23 +00:00
|
|
|
this._fetchKey = +this.$vnode.elm.dataset.fetchKey
|
|
|
|
const data = nuxtState.fetch[this._fetchKey]
|
2020-02-04 18:36:22 +00:00
|
|
|
|
|
|
|
// If fetch error
|
|
|
|
if (data && data._error) {
|
|
|
|
this.$fetchState.error = data._error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge data
|
|
|
|
for (const key in data) {
|
|
|
|
Vue.set(this.$data, key, data[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:40:39 +00:00
|
|
|
function $fetch() {
|
|
|
|
if (!this._fetchPromise) {
|
|
|
|
this._fetchPromise = $_fetch.call(this)
|
|
|
|
.then(() => { delete this._fetchPromise })
|
|
|
|
}
|
|
|
|
return this._fetchPromise
|
|
|
|
}
|
|
|
|
|
|
|
|
async function $_fetch() {
|
2020-03-18 10:30:50 +00:00
|
|
|
this.<%= globals.nuxt %>.nbFetching++
|
2020-02-04 18:36:22 +00:00
|
|
|
this.$fetchState.pending = true
|
|
|
|
this.$fetchState.error = null
|
|
|
|
this._hydrated = false
|
|
|
|
let error = null
|
|
|
|
const startTime = Date.now()
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.$options.fetch.call(this)
|
|
|
|
} catch (err) {
|
|
|
|
error = normalizeError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const delayLeft = this._fetchDelay - (Date.now() - startTime)
|
|
|
|
if (delayLeft > 0) {
|
|
|
|
await new Promise(resolve => setTimeout(resolve, delayLeft))
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$fetchState.error = error
|
|
|
|
this.$fetchState.pending = false
|
|
|
|
this.$fetchState.timestamp = Date.now()
|
|
|
|
|
2020-03-18 10:30:50 +00:00
|
|
|
this.$nextTick(() => this.<%= globals.nuxt %>.nbFetching--)
|
2020-02-04 18:36:22 +00:00
|
|
|
}
|
|
|
|
|