The library helps to build your own module system. Barely it resolves the Dependency Inversion Principle of SOLID. It is used as a barebone for an application that simplifies logic integration and its usage.
Be free and ask me questions personally on Gitter
import { ContainerLocator } = from 'instance-licator'
const config = {
test: 'configTest'
}
class CoreModel {
constructor(config) {
this.config = config
}
test() { return 'coreTest' }
}
class DbModel {
constructor(core) {
this.core = core
}
test() {
return 'dbTest'
}
}
const TYPES = {
config: Symbol('config'),
core: Symbol('core'),
db: Symbol('db')
}
const locator = new ContainerLocator()
locator
.registerValue(TYPES.config, config)
.register(TYPES.core, CoreModel, [ TYPES.config ])
.register(TYPES.db, DbModel, [TYPES.core])
const instance = locator.get(TYPES.db)
const core = locator.get(TYPES.core)
// Outputs dbTest.
console.log(instance.test(), 'dbTest')
// Outputs coreTest.
console.log(instance.core.test())
// Outputs configTest.
console.log(instance.core.config.test)
// Same instances.
console.log('Same instances - ', core == instance.core)
Also check the basic example and tests.
To run the basic example
npx babel-node ./example/basic.js