-
Notifications
You must be signed in to change notification settings - Fork 0
/
acyclic.js
128 lines (113 loc) · 4.17 KB
/
acyclic.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
(function (global, _) {
'use strict';
var acyclic = {};
acyclic.new = function () {
var graph = {};
var vertices = {},
namespaces = {},
queue = [],
initialized = false;
var namespaceIsComplete = function (namespace, vertext) {
return _.every(_.keys(namespace), function (key) {
if (namespace[key]) return namespaceIsComplete(namespace[key], vertext[key]);
return vertext[key];
});
};
var getVertex = function (name) {
var vertex = _.get(vertices, name);
var namespace = vertex && _.get(namespaces, name);
if (!namespace || namespaceIsComplete(namespace, vertex)) return vertex;
};
var throwError = function () {
var notFound = _.uniq(_.reject(_.flatten(_.map(queue, '1')), getVertex));
var getDependents = function (dependencies) {
return _.map(_.filter(queue, _.spread(function (name, _dependencies) {
return _.intersection(dependencies, _dependencies).length > 0;
})), _.spread(function (name) { return name || '[require]'; }));
};
var dependents = getDependents(notFound);
var culprits = _.difference(notFound, dependents);
if (culprits.length === 0 || _.every(culprits, function (culprit) { return _.get(namespaces, culprit); })) {
var dByN = _.reduce(queue, function (h, s) {
if (!s[0] || getVertex(s[0])) return h;
_.set(h, s[0], _.reject(s[1], getVertex));
return h;
}, {});
var paths = [];
var followPath = function (n, i) {
if (_.isArray(n)) {
paths.unshift(paths[0] + '.' + i);
_.each(n, followPath);
} else {
paths.unshift(n);
}
if (paths.length !== _.uniq(paths).length) {
throw new Error('Circular dependencies: "' + paths.join('" -> "') + '"');
}
_.each(_.get(dByN, n), followPath);
paths.shift();
};
_.each(_.keys(dByN), followPath);
} else {
dependents = getDependents(culprits);
throw new Error('Unresolveable dependenc' +
(culprits.length === 1 ? 'y' : 'ies') +
': "' + culprits.join('", "') +
'" for object' + (dependents.length === 1 ? '' : 's') +
' "' + dependents.join('", "') + '"'
);
}
};
var processQueue = function () {
var connectVertices = _.spread(function (name, dependencies, initializer) {
var resolvedDependencies = _.compact(_.map(dependencies, getVertex));
if (resolvedDependencies.length < dependencies.length) {
return false;
} else if (name) {
_.update(vertices, name, function (existingValue) {
if (existingValue) {
throw new Error('Name or namespace collision for "' + name + '"');
} else {
var vertex = initializer.apply(null, resolvedDependencies);
if (!vertex) throw new Error('Attempted to register a falsy value for "' + name + '"');
return vertex;
}
});
return true;
} else {
initializer.apply(null, resolvedDependencies);
return true;
}
});
var connected = [1];
while (connected.length > 0) connected = _.remove(queue, connectVertices);
if (queue.length) throwError();
};
graph.register = function (name, dependencies, initializer) {
if (name) _.each([namespaces, vertices], function (object) {
// Claim namespaces before initialization
_.update(object, name, _.identity);
});
queue.push([name, dependencies, initializer]);
if (initialized) processQueue();
return graph;
};
graph.require = _.partial(graph.register, false);
graph.init = function (dependencies, bootstrap) {
processQueue();
initialized = true;
if (_.isFunction(dependencies)) {
bootstrap = dependencies;
dependencies = [];
}
if (bootstrap) graph.require(dependencies, bootstrap);
return graph;
};
return graph;
};
try {
module.exports = acyclic;
} catch (e) {
global.acyclic = acyclic;
}
})(this, this._ || require('lodash'));