-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
205 lines (170 loc) · 5.76 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
var blockRegex = /^(address|blockquote|body|center|dir|div|dl|fieldset|form|h[1-6]|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|html)$/i
function isBlockLevel(name) {
return blockRegex.test(name)
}
function isEmpty(val) {
if (Array.isArray(val)) {
return val.length === 0
} else if (typeof val === 'object' && val !== null) {
return Object.keys(val).length === 0
} else if (val === null || val === undefined) {
return true
} else {
return false
}
}
function isFunction(func) {
return typeof func === 'function'
}
function createComputed(self, key) {
return function() {
return self[key].resolvedWith
}
}
module.exports = {
install: function(Vue, options) {
options = options || {}
options.getComputedName = options.getComputedName || function(vm, funcName) {
var withoutPrefix = funcName.replace(/^(fetch|get|load)/, '')
return withoutPrefix.slice(0, 1).toLowerCase() + withoutPrefix.slice(1)
}
function wrapMethod(func, vm, funcName) {
function wrapped() {
var args = [].slice.call(arguments)
vm[funcName].isCalled = true
vm[funcName].isPending = true
vm[funcName].isResolved = false
vm[funcName].isRejected = false
vm[funcName].resolvedWith = null
vm[funcName].resolvedWithSomething = false
vm[funcName].resolvedWithEmpty = false
vm[funcName].rejectedWith = null
try {
var result = func.apply(vm, args)
if (result && result.then) {
vm[funcName].promise = result.then(function(res) {
vm[funcName].isPending = false
vm[funcName].isResolved = true
vm[funcName].resolvedWith = res
var empty = isEmpty(res)
vm[funcName].resolvedWithEmpty = empty
vm[funcName].resolvedWithSomething = !empty
return res
}).catch(function(err) {
vm[funcName].isPending = false
vm[funcName].isRejected = true
vm[funcName].rejectedWith = err
if (isFunction(options.onError)) {
options.onError(err, vm[funcName].handleErrorInView, vm, funcName, args)
}
throw err
})
return vm[funcName].promise
} else {
// always return a promise for consistency
vm[funcName].promise = new Promise(function(resolve) {
vm[funcName].isPending = false
vm[funcName].isResolved = true
vm[funcName].resolvedWith = result
var empty = isEmpty(result)
vm[funcName].resolvedWithEmpty = empty
vm[funcName].resolvedWithSomething = !empty
resolve(result)
})
return vm[funcName].promise
}
} catch (err) {
// always return a promise for consistency
vm[funcName].promise = new Promise(function(resolve, reject) {
vm[funcName].isPending = false
vm[funcName].isRejected = true
vm[funcName].rejectedWith = err
if (isFunction(options.onError)) {
options.onError(err, vm[funcName].handleErrorInView, vm, funcName, args)
}
reject(err)
})
return vm[funcName].promise
}
}
return wrapped
}
Vue.component('catch-async-error', {
props: {
method: {
required: true
}
},
render: function(h) {
if (!this.error || !this.$slots || !this.$slots.default) return null
if (this.$slots.default.length === 1) {
return this.$slots.default[0]
}
var isAnyBlock = this.$slots.default.some(function(vNode) {
return isBlockLevel(vNode.tag)
})
var baseElement = isAnyBlock ? 'div' : 'span'
return h(baseElement, this.$slots.default)
},
data: function() {
return {
error: null
}
},
created: function() {
this.method.handleErrorInView = true
if (this.method.promise) {
this.catchError()
}
},
watch: {
'method.promise': 'catchError'
},
methods: {
catchError: function() {
var self = this
this.error = null
this.method.promise.catch(function(err) {
self.error = err
})
}
}
})
Vue.config.optionMergeStrategies.asyncMethods = Vue.config.optionMergeStrategies.methods
Vue.mixin({
beforeCreate: function() {
var self = this
var asyncMethods = this.$options.asyncMethods || {}
for (var key in asyncMethods) {
var func = wrapMethod(asyncMethods[key], this, key)
Vue.util.defineReactive(this, key, func)
var extra = {
promise: null,
isCalled: false,
isPending: false,
isResolved: false,
isRejected: false,
resolvedWith: null,
resolvedWithSomething: false,
resolvedWithEmpty: false,
rejectedWith: null,
handleErrorInView: false
}
for (var prop in extra) {
Vue.util.defineReactive(func, prop, extra[prop])
}
// add computed
if (options.createComputed) {
this.$options.computed = this.$options.computed || {}
var computedName = options.getComputedName(this, key)
if (!computedName || !computedName.length) {
throw new Error('Computed name for method ' + key + ' is empty, return a non zero length string')
}
this.$options.computed[computedName] = createComputed(self, key)
}
}
}
})
}
}