Skip to content

Commit 1bf4d61

Browse files
Merge pull request #106 from shrpne/mixin
Allow install as mixin
2 parents 6b1419c + 34fbe70 commit 1bf4d61

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

Diff for: src/index.js

+42-38
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,58 @@ const prefix = '_async_computed$'
2121

2222
const AsyncComputed = {
2323
install (Vue, pluginOptions) {
24-
pluginOptions = pluginOptions || {}
25-
2624
Vue.config
2725
.optionMergeStrategies
2826
.asyncComputed = Vue.config.optionMergeStrategies.computed
2927

30-
Vue.mixin({
31-
data () {
32-
return {
33-
_asyncComputed: {},
34-
}
35-
},
36-
computed: {
37-
$asyncComputed () {
38-
return this.$data._asyncComputed
39-
}
40-
},
41-
beforeCreate () {
42-
const asyncComputed = this.$options.asyncComputed || {}
28+
Vue.mixin(getAsyncComputedMixin(pluginOptions))
29+
}
30+
}
31+
32+
function getAsyncComputedMixin (pluginOptions = {}) {
33+
return {
34+
data () {
35+
return {
36+
_asyncComputed: {},
37+
}
38+
},
39+
computed: {
40+
$asyncComputed () {
41+
return this.$data._asyncComputed
42+
}
43+
},
44+
beforeCreate () {
45+
const asyncComputed = this.$options.asyncComputed || {}
4346

44-
if (!Object.keys(asyncComputed).length) return
47+
if (!Object.keys(asyncComputed).length) return
4548

46-
for (const key in asyncComputed) {
47-
const getter = getterFn(key, asyncComputed[key])
48-
this.$options.computed[prefix + key] = getter
49-
}
49+
for (const key in asyncComputed) {
50+
const getter = getterFn(key, asyncComputed[key])
51+
this.$options.computed[prefix + key] = getter
52+
}
5053

51-
this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
52-
},
53-
created () {
54-
for (const key in this.$options.asyncComputed || {}) {
55-
const item = this.$options.asyncComputed[key],
56-
value = generateDefault.call(this, item, pluginOptions)
57-
if (isComputedLazy(item)) {
58-
silentSetLazy(this, key, value)
59-
} else {
60-
this[key] = value
61-
}
54+
this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
55+
},
56+
created () {
57+
for (const key in this.$options.asyncComputed || {}) {
58+
const item = this.$options.asyncComputed[key],
59+
value = generateDefault.call(this, item, pluginOptions)
60+
if (isComputedLazy(item)) {
61+
silentSetLazy(this, key, value)
62+
} else {
63+
this[key] = value
6264
}
65+
}
6366

64-
for (const key in this.$options.asyncComputed || {}) {
65-
handleAsyncComputedPropetyChanges(this, key, pluginOptions, Vue)
66-
}
67+
for (const key in this.$options.asyncComputed || {}) {
68+
handleAsyncComputedPropetyChanges(this, key, pluginOptions)
6769
}
68-
})
70+
}
6971
}
7072
}
73+
const AsyncComputedMixin = getAsyncComputedMixin()
7174

72-
function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
75+
function handleAsyncComputedPropetyChanges (vm, key, pluginOptions) {
7376
let promiseId = 0
7477
const watcher = newPromise => {
7578
const thisPromise = ++promiseId
@@ -89,7 +92,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
8992
if (thisPromise !== promiseId) return
9093

9194
setAsyncState(vm, key, 'error')
92-
Vue.set(vm.$data._asyncComputed[key], 'exception', err)
95+
vm.$set(vm.$data._asyncComputed[key], 'exception', err)
9396
if (pluginOptions.errorHandler === false) return
9497

9598
const handler = (pluginOptions.errorHandler === undefined)
@@ -103,7 +106,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
103106
}
104107
})
105108
}
106-
Vue.set(vm.$data._asyncComputed, key, {
109+
vm.$set(vm.$data._asyncComputed, key, {
107110
exception: null,
108111
update: () => {
109112
if (!vm._isDestroyed) {
@@ -181,6 +184,7 @@ function generateDefault (fn, pluginOptions) {
181184
}
182185

183186
export default AsyncComputed
187+
export { AsyncComputed as AsyncComputedPlugin, AsyncComputedMixin }
184188

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

0 commit comments

Comments
 (0)