Registrations collection stores all configurations
created for the container to resolve at runtime
function Registrations() {
Object.defineProperty(this,'_registrations',{
value: {}
,enumerable: false
,configurable: false
,writable: false
})
this.get = this.get.bind(this)
this.put = this.put.bind(this)
this.clear = this.clear.bind(this)
this.has = this.has.bind(this)
}
Registrations.RESERVED_KEYS = ['@impl']
Registrations.prototype.get = function(key) {
if(!key) {
throw new Error('key is required')
}
var model = this._registrations[key]
if(model) {
return model
}
throw new Error(key + ' is not registered.')
}
Registrations.prototype.put = function(model) {
assert(model,'model is required')
if(this.isReserved(model.key)) {
throw new Error(model.key + ' is reserved. Choose another key.')
}
this._registrations[model.key] = model
}
Registrations.prototype.has = function(key) {
return !!this._registrations[key]
}
Registrations.prototype.clear = function(){
for(var k in this._registrations) {
;(delete this._registrations[k])
}
}
Registrations.prototype.keys = function(){
return Object.keys(this._registrations)
}
Registrations.prototype.isReserved = function(key) {
return (Registrations.RESERVED_KEYS.indexOf(key) > -1)
}
Provide a executable graph of model keys that
designated startable
on them.
This orders them according to the model's inject
setting, finally appending models which don't have dependencies.
Registrations.prototype.startables = function(){
var keys = Object.keys(this._registrations)
.filter(function(key){
return !!this._registrations[key].startable
},this)
var graph = this.createGraph(keys,keys,Registrations.RESERVED_KEYS)
return graph.legal()
//reverse the ordering so that dependents can manipulate their dependencies
.reverse()
.map(this.get.bind(this))
}
Registrations.prototype.toMap = function(keys) {
return (keys || this.keys())
.map(function(key){
return this.get(key)
},this)
.reduce(function(prev,model){
prev[model.key] = model.inject
return prev
},{})
}
Registrations.prototype.createGraph = function(keys,include,exclude){
var map = this.toMap(keys)
return new DependencyGraph(map,include,exclude)
}