-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.js
159 lines (135 loc) · 4.21 KB
/
container.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
156
157
158
159
// Generated by CoffeeScript 1.6.3
(function() {
var Container, Resolver, all, defer, multimethod, promise,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__slice = [].slice;
defer = require('node-promise').defer;
promise = function(f) {
var deferred, result;
deferred = defer();
result = f(deferred.resolve, deferred.reject);
return deferred.promise;
};
/*
Given a list of promises, creates a promise that will be resolved or rejected
when all the promises on the list have either been resolved or rejected. In
case of mixed successes the result will be a rejection.
*/
all = function(promises) {
return promise(function(resolve, reject) {
var next, p, rejected, resolved, _i, _len, _results;
if (promises.length === 0) {
resolve();
}
resolved = [];
rejected = [];
next = function() {
if (resolved.length + rejected.length !== promises.length) {
return;
}
if (rejected.length === 0) {
return resolve(resolved);
} else {
return reject(rejected);
}
};
_results = [];
for (_i = 0, _len = promises.length; _i < _len; _i++) {
p = promises[_i];
_results.push(p.then(function(value) {
resolved.push(value);
return next();
}, function(error) {
rejected.push(error);
return next();
}));
}
return _results;
});
};
multimethod = function(single, multiple) {
return function() {
if (arguments.length === 1) {
return single.apply(this, arguments);
} else {
return multiple.call(this, arguments);
}
};
};
Resolver = (function() {
Resolver.prototype.resources = null;
Resolver.prototype.busy = null;
function Resolver(resources, busy) {
this.resources = resources != null ? resources : {};
this.busy = busy != null ? busy : [];
}
Resolver.prototype.has = multimethod(function(name) {
return this.resources[name] != null;
}, function(names) {
var name, _i, _len;
for (_i = 0, _len = names.length; _i < _len; _i++) {
name = names[_i];
if (this.has(name)) {
return true;
}
}
return false;
});
Resolver.prototype.get = multimethod(function(name) {
var scope,
_this = this;
if (!this.has(name)) {
throw new Error("Resource '" + name + "' not available");
}
scope = this.using(name);
return promise(function(resolve, reject) {
return _this.resources[name].call(scope, resolve, reject, scope);
});
}, function(names) {
var name, promises;
promises = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = names.length; _i < _len; _i++) {
name = names[_i];
_results.push(this.get(name));
}
return _results;
}).call(this);
return all(promises);
});
Resolver.prototype.using = function(name) {
if (__indexOf.call(this.busy, name) >= 0) {
throw new Error("Recursive definition of resource '" + name + "' detected");
}
return new Resolver(this.resources, __slice.call(this.busy).concat([name]));
};
return Resolver;
})();
module.exports = Container = (function() {
Container.prototype.resources = null;
function Container(resources) {
this.resources = resources != null ? resources : {};
}
Container.prototype.resolver = function() {
return new Resolver(this.resources);
};
Container.prototype.describe = function(name, descriptor) {
this.resources[name] = descriptor;
return this;
};
Container.prototype.set = function(name, value) {
return this.describe(name, function(result, error) {
return result(value);
});
};
Container.prototype.has = function(name) {
return this.resources[name] != null;
};
Container.prototype.get = function() {
var _ref;
return (_ref = this.resolver()).get.apply(_ref, arguments);
};
return Container;
})();
}).call(this);