reverse initialization order, prepare for publishing
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
test
|
||||
js
|
||||
node_modules
|
||||
+5
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "dependjs",
|
||||
"version": "0.0.1",
|
||||
"description": "dependjs is a javascript dependency injector",
|
||||
"description": "dependjs is a typescript dependency injector",
|
||||
"main": "./js/Index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -13,6 +13,7 @@
|
||||
},
|
||||
"homepage": "https://gitea.nitowa.xyz/docs/dependjs",
|
||||
"keywords": [
|
||||
"typescript",
|
||||
"dependency injection",
|
||||
"inversion of control"
|
||||
],
|
||||
@@ -38,6 +39,8 @@
|
||||
"mocha": "^6.2.0"
|
||||
},
|
||||
"files": [
|
||||
"js"
|
||||
"js/Index.js",
|
||||
"js/Index.d.ts",
|
||||
"js/src/*"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export function Singleton(config?: {
|
||||
interface?: Constructor<any>,
|
||||
initializationPriority?: number
|
||||
}): GenericClassDecorator<Type<any>> {
|
||||
|
||||
return (clazz: Type<any>) => {
|
||||
Injector['singletonDefinitions'].push({
|
||||
initializationPriority: config ?. initializationPriority,
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
import 'reflect-metadata';
|
||||
import { ERR_NO_INITIALIZE_WITH_PRIORITY, ERR_NO_INJECTION_TOKEN } from './Strings';
|
||||
import { Constructor, Type, Module as SingletonDefinition, InjectionError, InjectionResolutionError } from './Internals';
|
||||
import { Constructor, Type, SingletonDefinition, InjectionError, InjectionResolutionError } from './Internals';
|
||||
|
||||
class _Injector {
|
||||
|
||||
@@ -71,7 +71,7 @@ class _Injector {
|
||||
|
||||
private initializeSingletons = () => {
|
||||
this.singletonDefinitions
|
||||
.sort((a, b) => (a.initializationPriority ?? 0) - (b.initializationPriority ?? 0))
|
||||
.sort((a, b) => (b.initializationPriority ?? 0) - (a.initializationPriority ?? 0))
|
||||
.map(def => this.singletonObjects[def.ctor.name])
|
||||
.forEach(obj => obj.initialize ? obj.initialize() : undefined)
|
||||
}
|
||||
|
||||
+2
-2
@@ -12,8 +12,8 @@ export type Constructor<T> = Function & { prototype: T }
|
||||
*/
|
||||
export type GenericClassDecorator<T> = (target: T) => void;
|
||||
|
||||
export type Module = {
|
||||
initializationPriority?: number //Priority of initializing this object after creation
|
||||
export type SingletonDefinition = {
|
||||
initializationPriority?: number //Priority of initializing this object after creation, lower value means higher priority; default: 0
|
||||
ctor: Type<any> //Object constructor to make singleton from
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ export abstract class IComponentB{
|
||||
}
|
||||
|
||||
@Singleton({
|
||||
interface: IComponentB
|
||||
interface: IComponentB,
|
||||
})
|
||||
export class ComponentB implements IComponentB, Initializable{
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Inject, Singleton } from "../../src/Decorator"
|
||||
import { Initializable } from "../../src/Interfaces"
|
||||
import { COMPONENT_B_VALUE, COMPONENT_C_VALUE } from "../CONSTANTS"
|
||||
import { COMPONENT_C_VALUE } from "../CONSTANTS"
|
||||
import { ComponentA } from "./ComponentA"
|
||||
|
||||
export class benis{
|
||||
|
||||
}
|
||||
|
||||
export abstract class IComponentC{
|
||||
getFromA: () => string
|
||||
getFromThis: () => string
|
||||
@@ -11,7 +14,7 @@ export abstract class IComponentC{
|
||||
@Singleton({
|
||||
interface: IComponentC,
|
||||
})
|
||||
export class ComponentC implements IComponentC{
|
||||
export class ComponentC{
|
||||
|
||||
@Inject(ComponentA)
|
||||
private componentA: ComponentA
|
||||
|
||||
@@ -9,7 +9,7 @@ var chaiAsPromised = require("chai-as-promised");
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
describe('dependjs', () => {
|
||||
describe('BasicTest', () => {
|
||||
it('is able to resolve linear dependencies', () => {
|
||||
|
||||
const testComp = Injector.resolve(TestComponent)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Inject, Singleton } from "../../src/Decorator";
|
||||
import { Initializable } from "../../src/Interfaces";
|
||||
import {ComponentA} from "./ComponentA"
|
||||
import {IComponentB} from "./ComponentB"
|
||||
import { ComponentC } from "./ComponentC";
|
||||
|
||||
@@ -8,12 +8,12 @@ var chaiAsPromised = require("chai-as-promised");
|
||||
|
||||
chai.use(chaiAsPromised);
|
||||
|
||||
describe('dependjs', () => {
|
||||
describe('InitializationTest', () => {
|
||||
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])
|
||||
expect(data).to.eql([COMPONENT_A_VALUE, COMPONENT_B_VALUE, COMPONENT_C_VALUE])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user