-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (56 loc) · 1.72 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
'use strict'
module.exports = function run (middleware) {
if (!Array.isArray(middleware)) {
middleware = [ middleware ]
}
return function (parent) {
var index = 0
var isResolved = false
var value
var context = (parent && parent.context) || {}
var parentKeys = parent ? Object.keys(parent) : []
var parentKeysLength = parentKeys.length
function loop () {
if (isResolved) {
return value // always resolve `next()` to `value`
} else if (index >= middleware.length) {
return parent && parent.next && parent.next() // chain parent
}
var args = { context: context }
for (var i = 0; i < parentKeysLength; ++i) {
args[parentKeys[i]] = parent[parentKeys[i]]
}
var nextCalled = false
var nextResult
args.next = function next () {
if (!nextCalled) {
nextCalled = true
nextResult = loop()
}
return nextResult
}
var stepResolve
var stepPromise = new Promise(function (resolve) {
stepResolve = resolve
})
args.resolve = function resolve (val) {
isResolved = true
if (stepResolve) {
if (arguments.length >= 1) value = val
stepResolve()
stepResolve = null
}
return finalPromise
}
var current = middleware[index++]
var result = current(args)
return Promise.race([ result, stepPromise ]).then(args.next)
}
var loopPromise = Promise.resolve().then(loop)
var finalPromise = loopPromise.then(function () {
var shouldResolveParent = isResolved && parent && parent.resolve
return shouldResolveParent ? parent.resolve(value) : value
})
return loopPromise
}
}