-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
314 lines (283 loc) · 8.23 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @fileoverview A simple dependency injection framework.
*
* Goals:
* - Handle function, constructor, and constant injectors
* - Allow asynchronous injectors
* - Easy forking for multiple environments
* - Allow specifying dependencies by name (robot leg problem)
* - Unfilfilled dependencies are failures, not null
* - Allow easy debugging of dependency graph
*/
const immutable = require('immutable')
/**
* A registry for holding all dependency providers.
*
* Registries are immutable and should generally be global to an application,
* though will also often be forked for different environments, e.g., a unit
* testing path is likely to provide different low-level data libraries.
*
* TKTK This is analagous to the Graph() in Shepherd.
*/
class Registry {
/**
* @param {?immutable.Map<string, Provider>} opt_providers
*/
constructor(opt_providers) {
this._providers = opt_providers || new immutable.Map()
}
/**
* @param {string} name
* @param {Function} Ctor
* @param {?Object} opt_options
* @return {Registry}
*/
ctor(name, Ctor, opt_options) {
assert(typeof Ctor == 'function',
`Cannot provide nonfunction for constructor provider ${name}`)
const deps = getDependencyNames(Ctor)
return this._add(name, 'CTOR', Ctor, deps, opt_options)
}
/**
* @param {string} name
* @param {any} constant
* @return {Registry}
*/
constant(name, constant) {
assert(constant != null,
`Cannot provide null for constant provider ${name}`)
return this._add(name, 'CONSTANT', constant, [])
}
/**
* @param {string} name
* @param {Function} fn
* @param {?Object} opt_options
* @return {Registry}
*/
fn(name, fn, opt_options) {
assert(typeof fn == 'function',
`Cannot provide nonfunction for function provider ${name}`)
const dependencies = getDependencyNames(fn)
return this._add(name, 'FN', fn, dependencies, opt_options)
}
/**
* @param {string} name
* @param {ProviderType} type
* @param {any} factory
* @param {Array<string>} deps
* @param {Object} options
* @return {Registry}
*/
_add(name, type, factory, deps, options={}) {
let mappedDeps = deps
if (options.using) {
mappedDeps = deps.map(d => options.using[d] || d)
}
const provider = new Provider(name, type, factory, mappedDeps, {
isCacheable: !!options.cache
})
return new Registry(this._providers.set(name, provider))
}
/**
* @return {Injector}
*/
buildInjector() {
// TODO validate graph with top-sort
return new Injector(this._providers)
}
/**
* @param {string} name
* @return {Promise<any>}
*/
build(name) {
return this.buildInjector().build(name)
}
}
/**
* Parse the dependency names out of a function provider.
*
* First checks for named dependencies in the `$ij` field, used in minified
* contexts. Otherwise attempts to parse the dependencies by name from the
* function signature, accounting for ES6 classes.
*
* @param {Function} fn
* @return {Array<string>}
*/
function getDependencyNames(fn) {
if (fn.$ij && Array.isArray(fn.$ij)) return fn.$ij
const fnStr = fn.toString()
let keywordIdx = 0
// If this is an ES6 class
if (fnStr.startsWith('class')) {
const ctorMatch = /\bconstructor\b/.exec(fnStr)
// Empty default constructor
if (!ctorMatch) return []
keywordIdx = ctorMatch.index
}
const leftParenIdx = fnStr.indexOf('(', keywordIdx)
const rightParenIdx = fnStr.indexOf(')', keywordIdx)
const params = fnStr.substring(leftParenIdx + 1, rightParenIdx).trim()
if (!params) return []
return params.split(',').map(p => p.trim())
}
/** The descriptor of a provider. */
class Provider {
constructor(name, type, factory, deps, opts={}) {
/**
* The global name of the Provider as it exists in the Registry.
* @type {string}
*/
this.name = name
/**
* The type of this provider, used in determining how to build itself.
* @type {ProviderType}
*/
this.type = type
/**
* The actual value of the provider, which can be anything
* @type {any}
*/
this.factory = factory
/**
* The list of dependencies required by this Provider, identified by their
* global names in the Registry.
* @type {Array<string>}
*/
this.dependencies = deps
/**
* Whether this Provider's result can be globally cached.
* @type {boolean}
*/
this.isCacheable = !!opts.isCacheable
Object.seal(this)
}
}
/**
* An injector for building dependencies from a registry.
*
* Injectors have state and will typically live for the lifecycle of an
* application. It handles caching singleton dependencies, e.g., a database
* connection, but will build non-cacheable dependencies once per build call.
*
* Injectors must be valid at the time of instantiation, and so should only be
* built with Registry#buildInjector().
*
* TKTK This is analagous to a Builder() in Shepherd.
*/
class Injector {
constructor(providers) {
/** @private {immutable.Map<string, Provider>} */
this._providers = providers
/** @private {immutable.Map<string, any>} */
this._cache = new immutable.Map()
}
/**
* Build a dependency from the registry.
*
* This method will build a dependency (and its subdependencies) anew each
* time it's called unless the dependency has specifically indicated that it
* is cacheable. However it will not build a dependency more than once per
* call, even if the same dependency is required multiple times.
*
* @param {string} name
* @param {Promise<any>}
*/
build(name) {
if (!this._providers.has(name)) {
return Promise.reject(new Error(`Provider not found for "${name}"`))
}
const provider = this._providers.get(name)
return this._build(provider, name)
.then((results) => results.get(provider.name))
.catch((err) => {
if (err instanceof ProviderNotFound) {
err.parents.push(name)
const chain = err.parents.join(' → ')
throw new Error(err.message + `(in dependency chain: ${chain})`)
}
throw err
})
}
/**
* @param {Provider} provider
* @return {Promise<immutable.Map<string, any>>}
*/
_build(provider) {
if (this._cache.has(provider.name)) {
return Promise.resolve(this._cache)
}
const childPromises = provider.dependencies.map((depName) => {
const depProvider = this._providers.get(depName)
if (!depProvider) {
// TODO clean up API for parents
const err = new ProviderNotFound(`Provider not found for "${depName}"`)
err.parents = [provider.name]
return Promise.reject(err)
}
return this._build(depProvider)
.catch((err) => {
if (err instanceof ProviderNotFound) {
err.parents.push(provider.name)
}
throw err
})
})
return Promise.all(childPromises)
.then((depResults) => {
const results = new immutable.Map().merge(...depResults)
return this._buildProvider(provider, results)
.then((result) => {
if (provider.isCacheable) {
this._cache = this._cache.set(provider.name, result)
}
return results.set(provider.name, result)
})
})
}
/**
* @param {Provider} provider
* @param {immutable.Map<string, any>} results
* @return {Promise<any>}
*/
_buildProvider(provider, results) {
const args = provider.dependencies.map(d => results.get(d))
switch (provider.type) {
case 'CONSTANT':
return Promise.resolve(provider.factory)
case 'CTOR':
return Promise.resolve(new provider.factory(...args))
case 'FN':
return Promise.resolve(provider.factory(...args))
}
}
}
class ProviderNotFound extends Error {
/**
* @param {string} name
* @return {ProviderNotFound}
*/
addParent(name) {
if (!this._parents) this._parents = [name]
else this._parents.push(name)
return this
}
/** @return {Array<string>} */
get parents() {
return this._parents || []
}
}
/**
* @param {T|?T} val
* @param {string} msg
* @return {T}
* @template {T}
*/
function assert(val, msg) {
if (!val) {
throw new Error(msg)
}
return val
}
module.exports = {
Registry,
}