Skip to content

Allow install as mixin #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 42 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,58 @@ const prefix = '_async_computed$'

const AsyncComputed = {
install (Vue, pluginOptions) {
pluginOptions = pluginOptions || {}

Vue.config
.optionMergeStrategies
.asyncComputed = Vue.config.optionMergeStrategies.computed

Vue.mixin({
data () {
return {
_asyncComputed: {},
}
},
computed: {
$asyncComputed () {
return this.$data._asyncComputed
}
},
beforeCreate () {
const asyncComputed = this.$options.asyncComputed || {}
Vue.mixin(getAsyncComputedMixin(pluginOptions))
}
}

function getAsyncComputedMixin (pluginOptions = {}) {
return {
data () {
return {
_asyncComputed: {},
}
},
computed: {
$asyncComputed () {
return this.$data._asyncComputed
}
},
beforeCreate () {
const asyncComputed = this.$options.asyncComputed || {}

if (!Object.keys(asyncComputed).length) return
if (!Object.keys(asyncComputed).length) return

for (const key in asyncComputed) {
const getter = getterFn(key, asyncComputed[key])
this.$options.computed[prefix + key] = getter
}
for (const key in asyncComputed) {
const getter = getterFn(key, asyncComputed[key])
this.$options.computed[prefix + key] = getter
}

this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
},
created () {
for (const key in this.$options.asyncComputed || {}) {
const item = this.$options.asyncComputed[key],
value = generateDefault.call(this, item, pluginOptions)
if (isComputedLazy(item)) {
silentSetLazy(this, key, value)
} else {
this[key] = value
}
this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
},
created () {
for (const key in this.$options.asyncComputed || {}) {
const item = this.$options.asyncComputed[key],
value = generateDefault.call(this, item, pluginOptions)
if (isComputedLazy(item)) {
silentSetLazy(this, key, value)
} else {
this[key] = value
}
}

for (const key in this.$options.asyncComputed || {}) {
handleAsyncComputedPropetyChanges(this, key, pluginOptions, Vue)
}
for (const key in this.$options.asyncComputed || {}) {
handleAsyncComputedPropetyChanges(this, key, pluginOptions)
}
})
}
}
}
const AsyncComputedMixin = getAsyncComputedMixin()

function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
function handleAsyncComputedPropetyChanges (vm, key, pluginOptions) {
let promiseId = 0
const watcher = newPromise => {
const thisPromise = ++promiseId
Expand All @@ -89,7 +92,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
if (thisPromise !== promiseId) return

setAsyncState(vm, key, 'error')
Vue.set(vm.$data._asyncComputed[key], 'exception', err)
vm.$set(vm.$data._asyncComputed[key], 'exception', err)
if (pluginOptions.errorHandler === false) return

const handler = (pluginOptions.errorHandler === undefined)
Expand All @@ -103,7 +106,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
}
})
}
Vue.set(vm.$data._asyncComputed, key, {
vm.$set(vm.$data._asyncComputed, key, {
exception: null,
update: () => {
if (!vm._isDestroyed) {
Expand Down Expand Up @@ -181,6 +184,7 @@ function generateDefault (fn, pluginOptions) {
}

export default AsyncComputed
export { AsyncComputed as AsyncComputedPlugin, AsyncComputedMixin }

/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
Expand Down