-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (138 loc) · 4.55 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
"use strict";
/**
* Contextizer: context injection.
* Like dependency injection but done per-request.
* With new inputs every time.
*
* See test.js for example usage.
*/
function Contextizer() {
this.graph = {};
}
Contextizer.prototype = {
register(path) {
if (this.graph[path])
throw new Error(`Path "${name}" already registered with value ${JSON.stringify(this.graph[path])}.`);
return {
asInput: () => this.graph[path] = { type: 'input' },
asConstant: value => this.graph[path] = { type: 'constant', value },
asFunction: ({ deps, func, cleanup=null, params={}, cached=false }) =>
this.graph[path] = { type: 'function', deps, func, cleanup, params, cached }
};
},
execute(target, inputs={}) {
if (!this.graph[target])
throw new Error(`Target "${target}" not found.`);
let context = {};
let ordering = this._traverse(target);
ordering.forEach(([ name, deps ]) => {
let item = this.graph[name];
switch(item.type) {
case 'input':
if (!inputs.hasOwnProperty(name))
throw new Error(`Missing input ${name}.`);
context[name] = inputs[name];
break;
case 'constant':
context[name] = item.value;
break;
case 'function':
let arg = this._makeArg(context, item, deps);
context[name] = promiseProps(arg)
.then(item.func);
if (item.cached) // If cached function, convert to constant.
this.graph[name] = { type: 'constant', value: context[name] };
break;
}
});
ordering.reverse(); // In-place.
let cleanup = () => {
ordering.forEach(([ name, deps ]) => {
let item = this.graph[name];
if (!item.cleanup)
return;
let arg = this._makeArg(context, item, deps);
arg.$value = context[name];
// Block any errors to allow cleanup even if error happened.
Object.entries(arg).forEach(([ key, value ]) => arg[key] =
value instanceof Promise ? value.catch(e => null) : value);
// Execute cleanup.
promiseProps(arg)
.then(item.cleanup)
.catch(e => console.error(`Item "${name}" threw error during cleanup:`, e));
});
};
context[target].then(cleanup, cleanup);
return context[target];
},
hasTarget(target) {
return !!this.graph[target];
},
// PRIVATE HELPER METHODS
_makeArg(context, item, deps) {
let arg = {};
Object.assign(arg, item.params);
// item.deps may be shorthand, this renames the context's canonical names correctly.
item.deps.forEach((depName, i) => arg[depName] = context[deps[i]]);
return arg;
},
/* Topological sort, via post-order traversal. */
_traverse(target) {
let result = new Map();
let visited = new Set();
let path = new Set();
let visit = name => {
visited.add(name);
path.add(name);
let deps = this._deps(name);
deps.forEach(dep => {
if (!visited.has(dep))
visit(dep);
else if (path.has(dep)) {
let cycle = Array.from(path);
cycle.push(dep);
throw new Error('Dependency Cycle Found: ' + cycle.join(' -> '));
}
});
path.delete(name);
if (!result.has(name))
result.set(name, deps);
};
visit(target);
return Array.from(result.entries());
},
_deps(path) {
let item = this.graph[path];
if (!item)
throw new Error(`Missing dependency: "${path}".`);
if (!item.deps)
return [];
let namespaces = path.split('.');
return item.deps.map(dep => {
if (dep.includes('.')) { // Canonical dep path.
if ('.' === dep.charAt(0))
return dep.slice(1);
return dep;
}
// Search for dep, starting from most specific namespace.
for (let i = namespaces.length - 1; i >= 0; i--) {
let canonical = namespaces.slice(0, i).concat(dep).join('.');
if (canonical === path)
continue; // Don't let it depend on itself.
if (this.graph[canonical])
return canonical;
}
throw new Error(`Dependency "${dep}" not found for item ${path}.`);
});
}
};
const promiseProps = Promise.props || function(obj) {
let keys = Object.keys(obj);
return Promise.all(Object.values(obj))
.then(vals => {
let result = {};
vals.forEach((val, i) => result[keys[i]] = val);
return result;
});
};
module.exports = Contextizer;