Skip to content

Commit

Permalink
Merge pull request #106 from shrpne/mixin
Browse files Browse the repository at this point in the history
Allow install as mixin
  • Loading branch information
foxbenjaminfox committed Oct 15, 2020
2 parents 6b1419c + 34fbe70 commit 1bf4d61
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions src/index.js
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

0 comments on commit 1bf4d61

Please sign in to comment.