-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (99 loc) · 2.66 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
/**
* Revert.
*
* Revert controls a series of steps called a chain. Each step must complete successfully for the chain to succeed.
* If one fails, the whole chain is undone.
*
* @author Luc Martel.
*/
module.exports = {
Step,
Chain
}
let states = {
ok: 0,
error: 1
}
/**
* Function constructor that instanciates one Step.
*
* @param {Function} upPromise Function returning a Promise run dutring the up stage.
* @param {Function} downPromise Function returning a Promise run dutring the down stage.
* @param {String} name Name of the Step (used in the stack).
* @param {Object} parameters List of parameters passed to the up and down functions
*
*/
function Step (upPromise, downPromise, name, parameters = { up: {}, down: {} }) {
// Transitions
this.upPromise = upPromise
this.downPromise = downPromise
this.name = name
this.parameters = parameters
// Were the functions called ?
this.upped = false
// State
this.state = states.ok
this.continue = true
}
/**
* Function constructor a step chain.
*/
function Chain () {
// Steps array
this.stack = []
this.steps = []
this.currentStepIndex = 0
}
Chain.prototype.add = function (step) {
this.steps.push(step)
}
Chain.prototype.push = function (stepIndex, type, result) {
this.stack.push({
stepIndex,
type,
result
})
}
Chain.prototype.up = async function () {
const numSteps = this.steps.length
let mustExit = false
while ((this.currentStepIndex < numSteps) && (!mustExit)) {
// console.log(this.currentStepIndex)
const currentStep = this.steps[this.currentStepIndex]
// Await on the current step promise
const result = await currentStep.upPromise(currentStep.parameters.up)
// Keep its execution result on the stack
this.push(this.currentStepIndex, 'up', result)
if (typeof result === 'boolean') {
if (result) {
// Promise was resolved to true
this.currentStepIndex++
} else {
// Promise was resolved to false
mustExit = true
}
} else {
// Promise was resolved to an object
mustExit = true
}
}
return {
asFailed: mustExit
}
}
Chain.prototype.down = async function () {
while (this.currentStepIndex >= 0) {
const currentStep = this.steps[this.currentStepIndex]
const result = await currentStep.downPromise(currentStep.parameters.down)
this.push(this.currentStepIndex, 'down', result)
this.currentStepIndex--
}
}
Chain.prototype.run = async function () {
const { asFailed } = await this.up()
// If we didn't get to the end of the chain
if (asFailed) {
await this.down()
}
return this.stack
}