-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathRetryRuntimeModule.js
More file actions
56 lines (52 loc) · 1.62 KB
/
RetryRuntimeModule.js
File metadata and controls
56 lines (52 loc) · 1.62 KB
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
const Template = require('webpack/lib/Template')
const RuntimeModule = require('webpack/lib/RuntimeModule')
const RetryRuntimeGlobal = '__webpack_require__.mpxR'
class RetryRuntimeModule extends RuntimeModule {
constructor () {
super('mpx retry module')
}
generate () {
const { compilation } = this
const { runtimeTemplate } = compilation
return Template.asString([
`${RetryRuntimeGlobal} = ${runtimeTemplate.basicFunction(
'fn, times, interval',
[
'times = times || 1;',
'interval = interval || 0;',
`return new Promise(${runtimeTemplate.basicFunction(
'resolve, reject',
[
Template.indent([
'var _t = 0;',
`var _retry = ${runtimeTemplate.basicFunction('', [
Template.indent([
`fn().then(resolve).catch(${runtimeTemplate.basicFunction('err', [
Template.indent([
'if (_t < times) {',
Template.indent([
'++_t;',
'interval > 0 ? setTimeout(_retry, interval) : _retry()'
]),
'} else {',
Template.indent([
'reject(err);'
]),
'}'
])
])})`
])
])};`,
'_retry();'
])
]
)})`
]
)}`
])
}
}
module.exports = {
RetryRuntimeModule,
RetryRuntimeGlobal
}