Added dependency injection
This commit is contained in:
@@ -12,7 +12,8 @@ export const Injector = new class {
|
||||
|
||||
rootInterface : Type<any>
|
||||
root : Type<any>
|
||||
rootModules : Type<any>[] = []
|
||||
rootModules: Type<any>[] = []
|
||||
modules : {ifc?: Type<any>, implementation: Type<any>}[] = []
|
||||
|
||||
moduleObjs : {[key in string] : FrontworkComponent} = {}
|
||||
|
||||
@@ -28,9 +29,11 @@ export const Injector = new class {
|
||||
return this.moduleObjs[target.name] as any
|
||||
|
||||
if(target.name === this.rootInterface.name || target.name === this.root.name){
|
||||
let modules = this.rootModules.map(m => {
|
||||
const module = new m()
|
||||
this.moduleObjs[m.name] = module
|
||||
let modules = this.modules.map(m => {
|
||||
const module = new m.implementation()
|
||||
if(m.ifc)
|
||||
this.moduleObjs[m.ifc.name] = module
|
||||
this.moduleObjs[m.implementation.name] = module
|
||||
return module
|
||||
})
|
||||
const rootobj = new this.root(modules);
|
||||
@@ -42,11 +45,9 @@ export const Injector = new class {
|
||||
if(this.moduleObjs[i.what.name])
|
||||
i.target[i.where] = this.moduleObjs[i.what.name]
|
||||
else
|
||||
this.injectionQueue.push()
|
||||
this.injectionQueue.push(i)
|
||||
}
|
||||
this.injectionQueue.forEach(i => {
|
||||
i.target[i.where] = this.moduleObjs[i.what.name]
|
||||
})
|
||||
|
||||
return rootobj
|
||||
}
|
||||
this.moduleObjs[target.name] = new target()
|
||||
|
||||
@@ -6,9 +6,12 @@ import { FrontworkComponent } from "../Types/FrontworkComponent";
|
||||
* @returns {GenericClassDecorator<Type<any>>}
|
||||
* @constructor
|
||||
*/
|
||||
export const Module = (...args) : GenericClassDecorator<Type<any>> => {
|
||||
export const Module = (ifc?: Type<any>) : GenericClassDecorator<Type<any>> => {
|
||||
return (target: Type<any>) => {
|
||||
Injector.rootModules.push(target)
|
||||
Injector.modules.push({
|
||||
ifc: ifc,
|
||||
implementation: target
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user