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
+2 -1
View File
@@ -1,8 +1,9 @@
import { Singleton } from "../../src/Decorator";
import { Initializable } from "../../src/Interfaces";
import { COMPONENT_A_VALUE } from "../CONSTANTS";
@Singleton()
export class ComponentA{
export class ComponentA implements Initializable{
private value: string
getFromThis(): string {
+5 -2
View File
@@ -1,4 +1,5 @@
import { Inject, Singleton } from "../../src/Decorator"
import { Initializable } from "../../src/Interfaces"
import { COMPONENT_B_VALUE } from "../CONSTANTS"
import { ComponentA } from "./ComponentA"
@@ -7,8 +8,10 @@ export abstract class IComponentB{
getFromThis: () => string
}
@Singleton(IComponentB)
export class ComponentB implements IComponentB{
@Singleton({
interface: IComponentB
})
export class ComponentB implements IComponentB, Initializable{
@Inject(ComponentA)
private componentA: ComponentA
+28
View File
@@ -0,0 +1,28 @@
import { Inject, Singleton } from "../../src/Decorator"
import { Initializable } from "../../src/Interfaces"
import { COMPONENT_B_VALUE, COMPONENT_C_VALUE } from "../CONSTANTS"
import { ComponentA } from "./ComponentA"
export abstract class IComponentC{
getFromA: () => string
getFromThis: () => string
}
@Singleton({
interface: IComponentC,
})
export class ComponentC implements IComponentC{
@Inject(ComponentA)
private componentA: ComponentA
private value: string = COMPONENT_C_VALUE
getFromA(): string {
return this.componentA.getFromThis()
}
getFromThis(): string {
return this.value
}
}
+3 -2
View File
@@ -1,7 +1,7 @@
import { Injector } from '../../src/Injector'
import { TestComponent } from './TestComponent'
import { assert, expect } from 'chai';
import { COMPONENT_A_VALUE, COMPONENT_B_VALUE } from '../CONSTANTS';
import { expect } from 'chai';
import { COMPONENT_A_VALUE, COMPONENT_B_VALUE, COMPONENT_C_VALUE } from '../CONSTANTS';
var should = require('chai').should();
var chai = require("chai");
@@ -17,6 +17,7 @@ describe('dependjs', () => {
expect(testComp.getFromA()).to.be.equal(COMPONENT_A_VALUE)
expect(testComp.getAThroughB()).to.be.equal(COMPONENT_A_VALUE)
expect(testComp.getFromB()).to.be.equal(COMPONENT_B_VALUE)
expect(testComp.getFromC()).to.be.equal(COMPONENT_C_VALUE)
})
})
+8
View File
@@ -1,6 +1,8 @@
import { Inject, Singleton } from "../../src/Decorator";
import { Initializable } from "../../src/Interfaces";
import {ComponentA} from "./ComponentA"
import {IComponentB} from "./ComponentB"
import { ComponentC } from "./ComponentC";
@Singleton()
export class TestComponent{
@@ -11,6 +13,9 @@ export class TestComponent{
@Inject(ComponentA)
private componentA: ComponentA
@Inject(ComponentC)
private componentC: ComponentC
getFromA(): string{
return this.componentA.getFromThis()
}
@@ -23,4 +28,7 @@ export class TestComponent{
return this.compoenntB.getFromThis()
}
getFromC(): string{
return this.componentC.getFromThis()
}
}
+2 -1
View File
@@ -1,2 +1,3 @@
export const COMPONENT_A_VALUE = "ComponentA"
export const COMPONENT_B_VALUE = "ComponentB"
export const COMPONENT_B_VALUE = "ComponentB"
export const COMPONENT_C_VALUE = "ComponentC"
+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
}
}