better code structure in injector/decorator and implemented initialization priorities

This commit is contained in:
nitowa
2022-07-25 10:08:39 +02:00
parent 076064ad98
commit 963a3a9652
19 changed files with 240 additions and 59 deletions
+16
View File
@@ -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)
}
}
+16
View File
@@ -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)
}
}
+16
View File
@@ -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)
}
}
+19
View File
@@ -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])
})
})
+16
View File
@@ -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
}
}