-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.js
230 lines (229 loc) · 7.83 KB
/
Builder.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var Builder = function Builder(container, promisedRequire)
{
this.container = container;
this.loading = null;
this.import = promisedRequire;
this.clear();
// this.container.set(
// "this",
// function()
// {
// return this;
// }
// );
}
Object.assign(
Builder.prototype,
{
use: function(filters)
{
if(typeof filters === "function") {
filters = [filters];
}
// console.log(this.filters);
this.filters = this.filters.concat(filters);
return this;
},
clear: function()
{
this.filters = [];
return this;
},
get: function(key)
{
const container = this.container;
var service;
if(this.loading != null) {
service = this.loading.then(
function()
{
// this.loading = null;
return container.get(key);
}.bind(this)
);
} else {
service = container.get(key);
}
// TODO: Should always be a promise? Unless within a service definition argument?
if(service instanceof Promise) {
service.catch(
function(e)
{
// TODO: is there a better way to do this?
e.message = e.message + "\n @" + key + "";
}
);
}
return service;
},
set: function(key, value)
{
this.container.set.apply(this.container, arguments);
return this;
},
has: function(key)
{
return this.container.has.apply(this.container, arguments);
},
tag: function()
{
this.container.tag.apply(this.container, arguments);
return this;
},
getTagged: function(key)
{
return this.container.getTagged(key);
},
run: function(key, args)
{
return this.get(key).then(
function(service)
{
if(typeof service === "function") {
return service.apply(null, args)
}
}
);
},
// TODO: if I can run stuff I should be able to instantiate also?
// instantiate: function()
// {
// this.get(key).then(function(service){ service.apply(null, args) });
// return this;
// },
build: function(config)
{
// TODO: I think this should push onto loading for multiple .build calls?
this.loading = this.load.apply(this, arguments).catch(
function(e)
{
throw e;
}
)
return this;
},
applyFilters: function(service, id, services)
{
const container = this;
const type = typeof service;
if(
service == null ||
type !== "object" ||
(
(
service.constructor != Object && id != "imports" //TODO: We shouldn't know about imports
)
)
) {
this.container.set(
id,
function()
{
return Promise.resolve(service)
}
);
return Promise.resolve(services);
}
if(this.filters.length == 0) {
return Promise.resolve(services);
}
const first = this.filters[0](container, service, id, services);
return this.filters.reduce(
function(prev, filter, i, arr)
{
return prev.then(
function(definitions)
{
services = definitions || services;
const result = filter(container, services[id], id, services);
if(result == null) {
return services;
}
return result;
}
)
},
first instanceof Promise ? first : Promise.resolve(first)
);
},
processKey: function(index, key, services)
{
return this.applyFilters(services[key], key, services).then(
function(services)
{
const keys = Object.keys(services);
const next = index + 1;
if(next < keys.length) {
return this.processKey(next, keys[next], services);
}
return services;
}.bind(this)
)
},
_load: function(services)
{
//callable?
if(typeof services.__esModule !== "undefined" && services.__esModule === true
&& typeof services.default !== "undefined"
) {
services = services.default;
}
if(typeof services === "function") {
services = services(this);
}
// TODO: You should be able to use promises resolving to objects
var process = function(services)
{
if(services == null || typeof services !== "object") {
throw new Error("Service definitions should be an object, a function returning an object (or a !!promise resolving to an object!!)")
}
var index = 0;
const next = Object.keys(services)[index];
return this.processKey(index, next, services).then(
function()
{
return this;
}.bind(this)
)
}
if(services instanceof Promise) {
return services.then(
process.bind(this)
);
}
return process.bind(this)(services)
},
load: function(services)
{
if(typeof services == "string") {
// const systems = this.getTagged("dom.system.import");
// if(!systems || systems.length < 1) {
// throw new Error("A System.import implementation is not defined, tag a callable dom.system.import service that implements `import` to use load")
// }
// const System_import = systems[systems.length - 1];
// if(typeof System_import !== "function") {
// throw new Error("A System.import implementation is not defined but is not callable, tag a callable dom.system.import service that implements `import` to use load")
// }
if(typeof this.import !== "function") {
throw new Error("This builder doesn't know how to load");
// throw new Error("A System.import implementation is not defined but is not callable, tag a callable dom.system.import service that implements `import` to use load")
}
return this.import(
services
).then(
function(services)
{
return this._load(services);
}.bind(this)
);
} else {
return this._load(services);
}
},
getContainer: function()
{
return this.container;
}
}
);
module.exports = Builder;