-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflow.js
More file actions
140 lines (140 loc) · 3.42 KB
/
flow.js
File metadata and controls
140 lines (140 loc) · 3.42 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
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
"use strict";
var Promise = require('bluebird');
/**
* A simple flow framework by promise
*
* Example:
* new Flow([
* function() {
* return Promise.resolve('one');
* },
* function(last) {
* // last now equals 'one'
* console.log(last);
* return 'two';
* },
* function(last) {
* // last now equals 'two'
* console.log(last);
* return 'three';
* }
* ]).start().then((last) => {
* // last now equals 'three'
* console.log(last);
* });
*
* @export
* @class Flow
*/
var Flow = (function () {
/**
* Creates an instance of Flow.
*
* @param {Array<Task>} [tasks=[]] the init tasks
*
* @memberOf Flow
*/
function Flow(tasks) {
if (tasks === void 0) { tasks = []; }
this.tasks = tasks;
/**
* point to the parent flow, while this is a child flow
*
* @type {Flow}
* @memberOf Flow
*/
this.parent = null;
if (!(this instanceof Flow)) {
return new Flow(tasks);
}
}
/**
* add tasks from tail
*
* @param {...Array<Task>} tasks
* @returns
*
* @memberOf Flow
*/
Flow.prototype.append = function () {
var tasks = [];
for (var _i = 0; _i < arguments.length; _i++) {
tasks[_i - 0] = arguments[_i];
}
this.tasks = this.tasks.concat(tasks);
return this;
};
/**
* add tasks from head
*
* @param {...Array<Task>} tasks
* @returns
*
* @memberOf Flow
*/
Flow.prototype.prepend = function () {
var tasks = [];
for (var _i = 0; _i < arguments.length; _i++) {
tasks[_i - 0] = arguments[_i];
}
this.tasks = tasks.concat(this.tasks);
return this;
};
/**
* exec the task
*
* @private
* @param {Task} task a task in tasks
* @param {*} last the returned value from last task
* @returns
*
* @memberOf Flow
*/
Flow.prototype.exec = function (task, last) {
var _this = this;
this.lastValue = last;
// executor of a single task
var single = function (t) {
if (t instanceof Flow) {
// the task is a flow instance as a child flow
// specify the child flow's parent to this
t.parent = _this;
// start the child flow
return t.start(last);
}
else {
// the task is a function
// exec the function
return t.call(_this, last);
}
};
if (Array.isArray(task)) {
// task is an array
// exec each task concurrently
return Promise.map(task, function (it) {
return single(it);
});
}
else {
return single(task);
}
};
/**
* start the flow
*
* @param {*} [initValue=Promise.resolve()] the init value to the flow
* @returns {Promise<any>}
*
* @memberOf Flow
*/
Flow.prototype.start = function (initValue) {
var _this = this;
if (initValue === void 0) { initValue = Promise.resolve(); }
// exec each task one by one
return Promise.reduce(this.tasks, function (last, curr) {
return _this.exec(curr, last);
}, initValue);
};
return Flow;
}());
exports.Flow = Flow;