Skip to content

Fixed reactivity of the $asyncComputed properties #58

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 1 commit into from
Jan 8, 2019
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
36 changes: 21 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ const AsyncComputed = {
.asyncComputed = Vue.config.optionMergeStrategies.computed

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

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

if (!this.$options.computed) this.$options.computed = {}

this.$options.computed.$asyncComputed = () => this.$data._asyncComputed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I had to implement a little hack here using _asyncComputed in $data and a corresponding $asyncComputed computed property to maintain backwards compatibility. This is necessary since Vue does not proxy properties that start with _ or $.

Alternatives, if backwards compatibility is not needed are:

  1. Rename $asyncComputed as simply asyncComputed.
  2. Keep only $asyncComputed in data instead of _asyncComputed, drop the unnecessary computed property and document that it should be accessed as this.$data.$asyncComputed.


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

for (const key in asyncComputed) {
const getter = getterFn(key, this.$options.asyncComputed[key])
this.$options.computed[prefix + key] = getter
Expand Down Expand Up @@ -74,17 +80,17 @@ const AsyncComputed = {
if (!newPromise || !newPromise.then) {
newPromise = Promise.resolve(newPromise)
}
setAsyncState(this.$asyncComputed[key], 'updating')
setAsyncState(this, key, 'updating')

newPromise.then(value => {
if (thisPromise !== promiseId) return
setAsyncState(this.$asyncComputed[key], 'success')
setAsyncState(this, key, 'success')
this[key] = value
}).catch(err => {
if (thisPromise !== promiseId) return

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

const handler = (pluginOptions.errorHandler === undefined)
Expand All @@ -98,25 +104,25 @@ const AsyncComputed = {
}
})
}
this.$asyncComputed[key] = {
Vue.set(this.$data._asyncComputed, key, {
exception: null,
update: () => {
watcher(getterOnly(this.$options.asyncComputed[key])())
}
}
setAsyncState(this.$asyncComputed[key], 'updating')
})
setAsyncState(this, key, 'updating')
this.$watch(prefix + key, watcher, { immediate: true })
}
}
})
}
}

function setAsyncState (stateObject, state) {
stateObject.state = state
stateObject.updating = state === 'updating'
stateObject.error = state === 'error'
stateObject.success = state === 'success'
function setAsyncState (vm, stateObject, state) {
vm.$set(vm.$data._asyncComputed[stateObject], 'state', state)
vm.$set(vm.$data._asyncComputed[stateObject], 'updating', state === 'updating')
vm.$set(vm.$data._asyncComputed[stateObject], 'error', state === 'error')
vm.$set(vm.$data._asyncComputed[stateObject], 'success', state === 'success')
}

function getterOnly (fn) {
Expand Down