https://github.com/unadlib/glaive
Trying to build a new dependency module injector
https://github.com/unadlib/glaive
Last synced: 12 months ago
JSON representation
Trying to build a new dependency module injector
- Host: GitHub
- URL: https://github.com/unadlib/glaive
- Owner: unadlib
- Created: 2017-09-15T13:57:35.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T05:55:04.000Z (almost 9 years ago)
- Last Synced: 2025-03-15T09:45:02.463Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 187 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Glaive
Trying to build a new dependency module Injector
[](https://travis-ci.org/unadlib/glaive)
[](https://coveralls.io/github/unadlib/glaive?branch=master)
[](https://www.npmjs.com/package/glaive)
[](https://www.npmjs.com/package/glaive)
[](https://www.npmjs.com/package/glaive)
## Features
* Overall process async dependency module
* After/before inject process
* Functional inject & decorator inject
* Custom Injector
* Implement module initialization lifecycle
* Inheritance of module
* Inheritance of module injector
* Override of module injector
* Merge options for value provider
* Custom options distribute to module
* Custom rule distribute to dependency module
* Custom distribute module name to dependency module
* Before injection use `preInject`
* Commons mount Params use `mountParams`
* Dependencies Module `preDistribute`
## Usage
```bash
yarn add glaive // or `npm install --save glaive`
```
## Example
```javascript
import { Injector as getInjector, Module, Decorator } from "glaive"
const Injector = getInjector(class BaseModule {})
class Call extends Module {}
@Decorator({
deps: ["Environment"],
after: environment => {
console.log(environment)
},
})
class NetWork extends Module {}
class HighSpeedNetWork extends NetWork {}
class Environment extends Module {
constructor(...args){
super(...args)
this.system = 'ios'
}
}
class Storage extends Module {
constructor(...args) {
super(...args)
}
async initialize() {
await new Promise(resolve=>setTimeout(resolve, 0))
}
}
// All aysnc functions support sync.
class Phone extends Injector {
constructor(...args) {
super(...args)
this.inject([
{
module: Environment,
key: "env"
},
{
module: Storage,
deps: ['Environment'],
before: async (environment,storage) => {
console.log(environment,storage)
await new Promise(resolve=>setTimeout(resolve, 0))
}
},
{
module: NetWork,
},
{
module: Call,
deps: ['Environment','Storage','NetWork'],
after: async () => {
await new Promise(resolve=>setTimeout(resolve, 0)) // this time is after call init.
}
}
])
}
}
class Mobile extends Phone {
constructor(...args){
super(...args)
this.inject([
{
module: HighSpeedNetWork,
key: '$_HighSpeedNetWork',
deps: ['Storage']
}
])
}
}
const mobile = new Mobile({
state: 'CN',
done: done => {
console.log('\n')
console.log(`done is ${done}!\n`, mobile)
},
})
```