Bottler
Very simple ioc container for Javascript(& Typescript).
Installation
npm install bottler --save
Usage
const bottler = // or import * as bottler from "bottler" const container = bottler// or const container = new bottler.Container()
Bind simple value
container.instance"obj1", container.instance"obj2", console.logawait container.get"obj1" // {message: "this is obj1"}console.logawait container.get"obj2" // {message: "this is obj2"} console.logawait container.get"obj1" === await container.get"obj1" // trueconsole.logawait container.get"obj2" === await container.get"obj2" // true
Bind promise value
container.instance"promise1", promise1container.instance"promise2", promise2 console.logawait container.get"promise1" // {message: "this is promise1"}console.logawait container.get"promise2" // {message: "this is promise2"} console.logawait container.get"promise1" === await container.get"promise1" // trueconsole.logawait container.get"promise2" === await container.get"promise2" // true
Bind factory
container.factory"factory1",container.factory"factory2",container.factory"factory3", console.logawait container.get"factory1" // {message: "this is factory"}console.logawait container.get"factory2" // {message: "this is promise factory"}console.logawait container.get"factory3" // {message: "this is async factory"}
Bind class
container.bind"driver", Drivercontainer.bind"connection", Connection console.logconnection // Connection { driver: Driver {} }console.logconnection.driver // Driver {}
Singleton descriptor
Descriptor is very useful if you using factory or bind. this is example of singleton.
container.factory"factory.normal",container.factory"factory.singleton",.singleton container.bind"class.normal", Foocontainer.bind"class.singleton", Foo.singleton // not sameconsole.logawait container.get"factory.normal" === await container.get"factory.normal" // falseconsole.logawait container.get"class.normal" === await container.get"class.normal" // false // always sameconsole.logawait container.get"factory.singleton" === await container.get"factory.singleton" // trueconsole.logawait container.get"class.singleton" === await container.get"class.singleton" // true
After descriptor
container .factory"foo", .after console.logawait container.get"foo" // {message: "this is origin maessage. and something appended."}