better code structure in injector/decorator and implemented initialization priorities
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { Inject, Singleton } from "../../src/Decorator"
|
||||
import { Initializable } from "../../src/Interfaces"
|
||||
import { COMPONENT_A_VALUE } from "../CONSTANTS"
|
||||
import { TestComponent } from "./TestComponent"
|
||||
|
||||
@Singleton({
|
||||
initializationPriority: 3
|
||||
})
|
||||
export class ComponentA implements Initializable{
|
||||
@Inject(TestComponent)
|
||||
private testComponent: TestComponent
|
||||
|
||||
initialize(): void{
|
||||
this.testComponent.pushData(COMPONENT_A_VALUE)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Inject, Singleton } from "../../src/Decorator"
|
||||
import { Initializable } from "../../src/Interfaces"
|
||||
import { COMPONENT_B_VALUE } from "../CONSTANTS"
|
||||
import { TestComponent } from "./TestComponent"
|
||||
|
||||
@Singleton({
|
||||
initializationPriority: 2
|
||||
})
|
||||
export class ComponentB implements Initializable{
|
||||
@Inject(TestComponent)
|
||||
private testComponent: TestComponent
|
||||
|
||||
initialize(): void{
|
||||
this.testComponent.pushData(COMPONENT_B_VALUE)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Inject, Singleton } from "../../src/Decorator"
|
||||
import { Initializable } from "../../src/Interfaces"
|
||||
import { COMPONENT_C_VALUE } from "../CONSTANTS"
|
||||
import { TestComponent } from "./TestComponent"
|
||||
|
||||
@Singleton({
|
||||
initializationPriority: 1
|
||||
})
|
||||
export class ComponentC implements Initializable{
|
||||
@Inject(TestComponent)
|
||||
private testComponent: TestComponent
|
||||
|
||||
initialize(): void{
|
||||
this.testComponent.pushData(COMPONENT_C_VALUE)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { expect } from 'chai';
|
||||
import { Injector } from '../../src/Injector'
|
||||
import { COMPONENT_A_VALUE, COMPONENT_B_VALUE, COMPONENT_C_VALUE } from '../CONSTANTS';
|
||||
import { TestComponent } from './TestComponent'
|
||||
|
||||
var chai = require("chai");
|
||||
var chaiAsPromised = require("chai-as-promised");
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
describe('dependjs', () => {
|
||||
it('initialized in the requested order', () => {
|
||||
|
||||
const testComp = Injector.resolve(TestComponent)
|
||||
const data = testComp.getData()
|
||||
|
||||
expect(data).to.eql([COMPONENT_C_VALUE, COMPONENT_B_VALUE, COMPONENT_A_VALUE])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Singleton } from "../../src/Decorator";
|
||||
|
||||
@Singleton()
|
||||
export class TestComponent{
|
||||
|
||||
private data: string[] = []
|
||||
|
||||
pushData(str: string){
|
||||
this.data.push(str)
|
||||
}
|
||||
|
||||
getData(){
|
||||
return this.data
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user