forked from foxbenjaminfox/vue-async-computed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
182 lines (153 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
initLazy,
isComputedLazy,
isLazyActive,
makeLazyComputed,
silentGetLazy,
silentSetLazy,
} from './lazy'
const prefix = '_async_computed$'
const DidNotUpdate = typeof Symbol === 'function' ? Symbol('did-not-update') : {}
const AsyncComputed = {
install (Vue, pluginOptions) {
pluginOptions = pluginOptions || {}
Vue.config
.optionMergeStrategies
.asyncComputed = Vue.config.optionMergeStrategies.computed
Vue.mixin({
beforeCreate () {
const asyncComputed = this.$options.asyncComputed || {}
this.$asyncComputed = {}
for (const key in this.$options.computed) {
if (this.$options.computed[key].asynchronous) {
asyncComputed[key] = this.$options.computed[key].asynchronous
}
}
if (!Object.keys(asyncComputed).length) return
this.$options.asyncComputed = asyncComputed
if (!this.$options.computed) this.$options.computed = {}
for (const key in asyncComputed) {
const getter = getterFn(key, this.$options.asyncComputed[key])
this.$options.computed[prefix + key] = getter
const getset = makeLazyComputed(key)
this.$options.computed[key].get = getset.get
this.$options.computed[key].set = getset.set
}
},
data () {
let ret = {}
const asyncComputed = this.$options.asyncComputed || {}
for (const key in asyncComputed) {
initLazy(ret, key)
}
return ret
},
created () {
for (const key in this.$options.asyncComputed || {}) {
const item = this.$options.asyncComputed[key],
value = generateDefault.call(this, item, pluginOptions)
silentSetLazy(this, key, value)
}
for (const key in this.$options.asyncComputed || {}) {
let promiseId = 0
const watcher = newPromise => {
const thisPromise = ++promiseId
if (newPromise === DidNotUpdate) {
return
}
if (!newPromise || !newPromise.then) {
newPromise = Promise.resolve(newPromise)
}
setAsyncState(this.$asyncComputed[key], 'updating')
newPromise.then(value => {
if (thisPromise !== promiseId) return
setAsyncState(this.$asyncComputed[key], 'success')
silentSetLazy(this, key, value)
}).catch(err => {
if (thisPromise !== promiseId) return
setAsyncState(this.$asyncComputed[key], 'error')
this.$asyncComputed[key].exception = err
if (pluginOptions.errorHandler === false) return
const handler = (pluginOptions.errorHandler === undefined)
? console.error.bind(console, 'Error evaluating async computed property:')
: pluginOptions.errorHandler
if (pluginOptions.useRawError) {
handler(err)
} else {
handler(err.stack)
}
})
}
this.$asyncComputed[key] = {
exception: null,
update: () => {
watcher(getterOnly(this.$options.asyncComputed[key])())
}
}
setAsyncState(this.$asyncComputed[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 getterOnly (fn) {
if (typeof fn === 'function') return fn
return fn.get
}
function getterFn (key, fn) {
if (typeof fn === 'function') return fn
let getter = fn.get
if (fn.hasOwnProperty('watch')) {
const previousGetter = getter
getter = function getter () {
fn.watch.call(this)
return previousGetter.call(this)
}
}
if (fn.hasOwnProperty('shouldUpdate')) {
const previousGetter = getter
getter = function getter () {
if (fn.shouldUpdate.call(this)) {
return previousGetter.call(this)
}
return DidNotUpdate
}
}
if (isComputedLazy(fn)) {
const nonLazy = getter
getter = function lazyGetter () {
if (isLazyActive(this, key)) {
return nonLazy.call(this)
} else {
return silentGetLazy(this, key)
}
}
}
return getter
}
function generateDefault (fn, pluginOptions) {
let defaultValue = null
if ('default' in fn) {
defaultValue = fn.default
} else if ('default' in pluginOptions) {
defaultValue = pluginOptions.default
}
if (typeof defaultValue === 'function') {
return defaultValue.call(this)
} else {
return defaultValue
}
}
export default AsyncComputed
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
// Auto install in dist mode
window.Vue.use(AsyncComputed)
}