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
20 changes: 12 additions & 8 deletions src/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ const prefix = '_async_computed$'


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

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


Vue.mixin({ Vue.mixin(getAsyncComputedMixin(pluginOptions))
}
}

function getAsyncComputedMixin (pluginOptions = {}) {
return {
data () { data () {
return { return {
_asyncComputed: {}, _asyncComputed: {},
Expand Down Expand Up @@ -62,14 +65,14 @@ const AsyncComputed = {
} }


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


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


setAsyncState(vm, key, 'error') 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 if (pluginOptions.errorHandler === false) return


const handler = (pluginOptions.errorHandler === undefined) 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, exception: null,
update: () => { update: () => {
if (!vm._isDestroyed) { if (!vm._isDestroyed) {
Expand Down Expand Up @@ -181,6 +184,7 @@ function generateDefault (fn, pluginOptions) {
} }


export default AsyncComputed export default AsyncComputed
export { AsyncComputed as AsyncComputedPlugin, AsyncComputedMixin }


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