NotificationLatch/src/container.js

31 lines
587 B
JavaScript

/*
* Original concept and code by Magnus Tovslid:
* https://medium.com/@magnusjt/ioc-container-in-nodejs-e7aea8a89600
*/
class Container
{
constructor()
{
this.services = {};
}
register(name, factory)
{
Object.defineProperty(this, name, {
get: () =>
{
if(!this.services.hasOwnProperty(name))
this.services[name] = factory(this);
return this.services[name];
},
configurable: true,
enumerable: true
});
return this;
}
}
module.exports = Container;