Skip to content

Commit 9c3f738

Browse files
committed
add mixin export
1 parent 6b1419c commit 9c3f738

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

Diff for: src/index.js

+42-36
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,54 @@ const AsyncComputed = {
2727
.optionMergeStrategies
2828
.asyncComputed = Vue.config.optionMergeStrategies.computed
2929

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 || {}
30+
Vue.mixin(getAsyncComputedMixin(pluginOptions))
31+
}
32+
}
4333

44-
if (!Object.keys(asyncComputed).length) return
34+
function getAsyncComputedMixin (pluginOptions) {
35+
return {
36+
data () {
37+
return {
38+
_asyncComputed: {},
39+
}
40+
},
41+
computed: {
42+
$asyncComputed () {
43+
return this.$data._asyncComputed
44+
}
45+
},
46+
beforeCreate () {
47+
const asyncComputed = this.$options.asyncComputed || {}
4548

46-
for (const key in asyncComputed) {
47-
const getter = getterFn(key, asyncComputed[key])
48-
this.$options.computed[prefix + key] = getter
49-
}
49+
if (!Object.keys(asyncComputed).length) return
5050

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-
}
62-
}
51+
for (const key in asyncComputed) {
52+
const getter = getterFn(key, asyncComputed[key])
53+
this.$options.computed[prefix + key] = getter
54+
}
6355

64-
for (const key in this.$options.asyncComputed || {}) {
65-
handleAsyncComputedPropetyChanges(this, key, pluginOptions, Vue)
56+
this.$options.data = initDataWithAsyncComputed(this.$options, pluginOptions)
57+
},
58+
created () {
59+
for (const key in this.$options.asyncComputed || {}) {
60+
const item = this.$options.asyncComputed[key],
61+
value = generateDefault.call(this, item, pluginOptions)
62+
if (isComputedLazy(item)) {
63+
silentSetLazy(this, key, value)
64+
} else {
65+
this[key] = value
6666
}
6767
}
68-
})
68+
69+
for (const key in this.$options.asyncComputed || {}) {
70+
handleAsyncComputedPropetyChanges(this, key, pluginOptions)
71+
}
72+
}
6973
}
7074
}
75+
const AsyncComputedMixin = getAsyncComputedMixin()
7176

72-
function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
77+
function handleAsyncComputedPropetyChanges (vm, key, pluginOptions) {
7378
let promiseId = 0
7479
const watcher = newPromise => {
7580
const thisPromise = ++promiseId
@@ -89,7 +94,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
8994
if (thisPromise !== promiseId) return
9095

9196
setAsyncState(vm, key, 'error')
92-
Vue.set(vm.$data._asyncComputed[key], 'exception', err)
97+
vm.$set(vm.$data._asyncComputed[key], 'exception', err)
9398
if (pluginOptions.errorHandler === false) return
9499

95100
const handler = (pluginOptions.errorHandler === undefined)
@@ -103,7 +108,7 @@ function handleAsyncComputedPropetyChanges (vm, key, pluginOptions, Vue) {
103108
}
104109
})
105110
}
106-
Vue.set(vm.$data._asyncComputed, key, {
111+
vm.$set(vm.$data._asyncComputed, key, {
107112
exception: null,
108113
update: () => {
109114
if (!vm._isDestroyed) {
@@ -181,6 +186,7 @@ function generateDefault (fn, pluginOptions) {
181186
}
182187

183188
export default AsyncComputed
189+
export { AsyncComputed as AsyncComputedPlugin, AsyncComputedMixin }
184190

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

0 commit comments

Comments
 (0)