-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
155 lines (135 loc) · 4.08 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
// reiventing _.each() to iterate over objects
function objEach(obj, f){
for(var key in obj){
if(obj.hasOwnProperty(key)){
var v = obj[key];
f(v, key);
}
}
}
function allKeys(obj){
var ret = [];
objEach(obj, function (v, k) {
ret.push(k);
});
return ret;
}
/**
* Given an implementation of Promises, returns a function for running promise-dag graphs, which has the same behaviour as run().
*
* An implementation consists simply of 3 Promise operations:
* - Promise.resolve()
* - Promise.reject()
* - Promise.all()
*
* @param {{resolve: Function, reject: Function, all: Function}} Promiz
* @returns {Function}
*/
function implement(Promiz){
function run(steps, required) {
required = required || allKeys(steps);
var UNVISITED = 0, VISITING = 1, VISITED = 2;
var flags = {};
var promises = {};
objEach(steps, function (step, key) {
flags[key] = UNVISITED;
});
function sourcePromise(step){
var f = step[0];
if(typeof f !== 'function'){
throw new Error("The last element in a promiseDag step must be a function.");
}
var p;
try {
p = Promiz.resolve(f());
} catch (err) {
p = Promiz.reject(err);
}
return p;
}
function visit(key) {
var step = steps[key];
if(!step){
throw new Error("Missing step in graph: [" + key + "]");
}
var p;
if (flags[key] === VISITED) {
p = promises[key];
} else if (flags[key] === VISITING) {
throw new Error("Circular dependency around step [" + key + "]");
} else {
flags[key] = VISITING;
var nDeps = step.length - 1;
if (nDeps === 0) {
p = sourcePromise(step);
} else {
var deps = [];
for (var i = 0; i < nDeps; i++) { // with recursive, memoized calls, obtain the promises associated with the strings.
var s = step[i];
var dep = visit(s);
deps.push(dep);
}
var handler = step[nDeps]; // handler is the last element.
p = Promiz.all(deps).then(function (arr) {
return handler.apply(null, arr);
});
}
promises[key] = p;
flags[key] = VISITED;
}
return p;
}
var ret = {};
for(var i = 0, n = required.length; i < n; i++){
var key = required[i];
ret[key] = visit(key);
}
return ret;
}
return run;
}
var es6Factory = {
resolve: function (v) {
return Promise.resolve(v);
},
reject: function (v) {
return Promise.reject(v);
},
all: function (arr) {
return Promise.all(arr);
}
};
/**
* Given a data structure which specifies a computation as a DAG of named steps with dependencies,
* performs the computation by assembling a matching DAG of Promises,
* and returns an object mapping each step name to a Promise of the realized step.
*
* Optionally, an array of 'output' steps may be specified, in which case:
* - only the computations associated to the output steps and their dependencies will be performed
* - only the output steps Promises will be present in the returned object.
*
* A step specification consists of an array of length n+1 (n >= 0), in which:
* - the first n elements are the names of the steps on which this step depends (as strings; there may be none of them!)
* - the last element is an n-arity function, into which the realized dependencies will be injected,
* which should return either the realized value for the current step, or a Promise of that realized value, or throw an Error.
*
* Note that the graph represented by `steps` may not have circular or missing dependencies.
*
* @param steps {{}} an object mapping step names to step specifications.
* @param {Array.<string>|undefined} required
* @returns {{}} an object mapping step names to Promises.
*/
var run = implement(es6Factory);
/**
* A shorthand for 'constant' step specifications.
* @param v {*}
* @returns {*[]}
*/
function source(v){
return [function () {
return v;
}];
}
exports.run = run;
exports.implement = implement;
exports.source = source;