Add separate proxy() method to container

singleton
garrettmills 4 years ago
parent b47d20939c
commit dba9fee204
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E

@ -66,13 +66,7 @@ class Container {
* @returns {module:flitter-di/src/Service~Service|undefined} - the service instance or service container proxy
*/
service(service = false) {
if ( service === false ) {
return new Proxy({}, {
get: (obj, prop) => {
return this.service(prop)
}
})
}
if ( service === false ) return this.proxy()
if ( !this.definitions[service] ) {
throw new Error('No such service registered with this container: '+service)
@ -95,6 +89,14 @@ class Container {
return this.instances[service]
}
proxy() {
return new Proxy({}, {
get: (what, name) => {
return this.service(name)
}
})
}
/**
* Register a class definition as a service. When requested, the service
* for this class will be created from the class' instance.

@ -129,6 +129,20 @@ describe('the inversion of control container', function() {
})
})
describe('proxy function', function() {
it('should return a proxy container that can resolve services', function() {
class TestService {}
const name = Math.random().toString(36).substring(7)
const defs = {}
defs[name] = TestService
const container = new Container(defs)
const services = container.proxy()
expect(util.types.isProxy(services)).to.be.true
expect(services[name]).to.be.an.instanceof(TestService)
})
})
describe('register function', function() {
it('should be a function', function() {
const container = new Container()

Loading…
Cancel
Save