Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xujif/async-hooks-container
DI(dependency injection)container for JavaScript and TypeScript.
https://github.com/xujif/async-hooks-container
ioc ioc-container nodejs typescript
Last synced: 17 days ago
JSON representation
DI(dependency injection)container for JavaScript and TypeScript.
- Host: GitHub
- URL: https://github.com/xujif/async-hooks-container
- Owner: xujif
- Created: 2018-08-28T02:52:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-06T09:02:16.000Z (over 6 years ago)
- Last Synced: 2024-11-07T06:16:19.332Z (about 2 months ago)
- Topics: ioc, ioc-container, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 31.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## Important!!
## this package is rename to [@mooncake/container](https://github.com/xujif/mooncake_container)
### please use [@mooncake/container](https://github.com/xujif/mooncake_container) instead !
### please use [@mooncake/container](https://github.com/xujif/mooncake_container) instead !
### please use [@mooncake/container](https://github.com/xujif/mooncake_container) instead !***
### DI(dependency injection)container for JavaScript and TypeScript.
Only support node 9+ (suggest node 10), powered by [async_hooks](https://nodejs.org/api/async_hooks.html)## Notes
```TypeScript
// import Container class type
import { Container } from 'async-hooks-container'import container from 'async-hooks-container'
// import the global instance of Container (lazy initialize when first import)
```Example:
```TypeScript
const container = new Container()
container.aliasScope('scope1')
const t1 = new Test1(1)
container.set('test', t1)
process.nextTick(() => {
container.aliasScope('nt1')
const t11 = container.get('test') as Test1
assert.equal(t1, t11, 'should equal')
const t2 = new Test1(2)
container.set('test', t2)
assert.equal(t2, container.get('test'), 'should equal')
process.nextTick(() => {
assert(container.hasScope('scope1'))
assert(container.hasScope('nt1'))
assert.equal(t2, container.get('test'), 'should equal')
assert.equal(t1, container.get('test', 'scope1'), 'should equal')
})
})
```## API
```TypeScript
interface Container {
/**
* get instance from container
*
* @template T
* @param {Constructor} id
* @param {string} [fromScope]
* @returns {T}
* @memberof ContainerResolver
*/
get(id: Constructor, fromScope?: string): T
get(id: string | symbol, fromScope?: string): T | undefined/**
* fill prop depencies of an exist instalce
* it will ignore when target prop has value
*
* @param {*} target
* @param {string} [fromScope]
* @memberof ContainerResolver
*/
fill (target: any, fromScope?: string): void/**
* alias of bind Value
*
* @template T
* @param {ID} id
* @param {T} value
* @param {Pick} [opt]
* @memberof ContainerBinder
*/
set (id: ID, value: T, opt?: Pick): this/**
* bind a exist instalce
*
* @template T
* @param {ID} id
* @param {T} value
* @param {Pick} [opt]
* @memberof ContainerBinder
*/
bindValue (id: ID, value: T, opt?: Pick): this
/**
* bind lazy initialize instance
*
* @template T
* @param {ID} id
* @param {() => T} creater
* @param {Partial} [opt]
* @memberof ContainerBinder
*/
bind (id: ID, creater: () => T, opt?: Partial): this/**
* bind a class with factory
*
* @template T
* @param {Constructor} id
* @param {(Factory | Constructor>)} factory
* @param {Partial} [opt]
* @returns {this}
* @memberof ContainerBinder
*/
bindFactory (id: Constructor, factory: Factory | Constructor>, opt?: Partial): this/**
* bind a class
* set scope or singleton
*
* @template T
* @param {Constructor} cls
* @param {Partial} [opt]
* @memberof ContainerBinder
*/
bindClass (cls: Constructor, opt?: Partial): this
/**
* bind a class with an diffrent id
*
* @param {ID} id
* @param {Constructor} cls
* @param {Partial} [opt]
* @memberof ContainerBinder
*/
bindClassWithId (id: ID, cls: Constructor, opt?: Partial): this/**
* alias a id
*
* @param {ID} id
* @param {ID} toId
* @param {Pick} [opt]
* @memberof ContainerBinder
*/
bindAlias (id: ID, toId: ID, opt?: Pick): this/**
* alias current async scope
*
* @param {string} name
* @memberof ContainerBinder
*/
aliasScope (name: string): this/**
* detect if exist a scope
*
* @param {string} name
* @memberof ContainerBinder
*/
hasScope (name: string): boolean
/**
* scan the decoractors and auto bind class
*
* @param {Constructor} target
* @memberof ContainerBinder
*/
autoBind (target: Constructor): this
}```
## decorators#### 1.injection
```TypeScript
class Test1 {
}
// prop inject
class Test2 {
@Inject()
prop1!: Test1
}
// constructor inject
class Test3 {
prop1!: Test1
constructor(@Inject() param1: Test1) {
this.prop1 = param1
}
}
// optional inject
class Test4 {
@InjectOptional()
prop1!: Test1 // prop1 will be undefined if resolve Test1 Fail
}// custom inject action
class Test4 {
@InjectRaw((c) => c.get('id'), { required: true })
prop1!: Test1
}```
#### 2. bind with decorators
```TypeScript
@Singleton({scope:'root'}) // Singleton's default scope is 'root'
// alias as @Service({ singleton: true,scope:'any scope' })// a service will define a bind action
@Service({ singleton: true,scope:'request' })
@Alias('a',{scope:'request'}) // alias 'a' to class A
// Container.get(A) and Container.get('a') will return same result in 'request' scope@Implement(() => A1) // container.get(A) will return A1 instead
@Factory(FactoryA) // customize instance with factory
@BindAction((cls,container)=>{
// customize bind Action
const instance = new cls()
// instance.doSomeAction()
container.set(cls,instance)
})
class A {}class A1{}
class FactoryA{
create(){
const ins = new A1()
// instance.doSomeAction()
return ins
}
}
```