reverse initialization order, prepare for publishing

This commit is contained in:
nitowa
2022-10-16 02:45:49 +02:00
parent 963a3a9652
commit a373b178c6
10 changed files with 20 additions and 17 deletions
-3
View File
@@ -1,3 +0,0 @@
test
js
node_modules
+5 -2
View File
@@ -1,7 +1,7 @@
{ {
"name": "dependjs", "name": "dependjs",
"version": "0.0.1", "version": "0.0.1",
"description": "dependjs is a javascript dependency injector", "description": "dependjs is a typescript dependency injector",
"main": "./js/Index.js", "main": "./js/Index.js",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -13,6 +13,7 @@
}, },
"homepage": "https://gitea.nitowa.xyz/docs/dependjs", "homepage": "https://gitea.nitowa.xyz/docs/dependjs",
"keywords": [ "keywords": [
"typescript",
"dependency injection", "dependency injection",
"inversion of control" "inversion of control"
], ],
@@ -38,6 +39,8 @@
"mocha": "^6.2.0" "mocha": "^6.2.0"
}, },
"files": [ "files": [
"js" "js/Index.js",
"js/Index.d.ts",
"js/src/*"
] ]
} }
+1
View File
@@ -9,6 +9,7 @@ export function Singleton(config?: {
interface?: Constructor<any>, interface?: Constructor<any>,
initializationPriority?: number initializationPriority?: number
}): GenericClassDecorator<Type<any>> { }): GenericClassDecorator<Type<any>> {
return (clazz: Type<any>) => { return (clazz: Type<any>) => {
Injector['singletonDefinitions'].push({ Injector['singletonDefinitions'].push({
initializationPriority: config ?. initializationPriority, initializationPriority: config ?. initializationPriority,
+2 -2
View File
@@ -1,6 +1,6 @@
import 'reflect-metadata'; import 'reflect-metadata';
import { ERR_NO_INITIALIZE_WITH_PRIORITY, ERR_NO_INJECTION_TOKEN } from './Strings'; 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 { class _Injector {
@@ -71,7 +71,7 @@ class _Injector {
private initializeSingletons = () => { private initializeSingletons = () => {
this.singletonDefinitions 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]) .map(def => this.singletonObjects[def.ctor.name])
.forEach(obj => obj.initialize ? obj.initialize() : undefined) .forEach(obj => obj.initialize ? obj.initialize() : undefined)
} }
+2 -2
View File
@@ -12,8 +12,8 @@ export type Constructor<T> = Function & { prototype: T }
*/ */
export type GenericClassDecorator<T> = (target: T) => void; export type GenericClassDecorator<T> = (target: T) => void;
export type Module = { export type SingletonDefinition = {
initializationPriority?: number //Priority of initializing this object after creation 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 ctor: Type<any> //Object constructor to make singleton from
} }
+1 -1
View File
@@ -9,7 +9,7 @@ export abstract class IComponentB{
} }
@Singleton({ @Singleton({
interface: IComponentB interface: IComponentB,
}) })
export class ComponentB implements IComponentB, Initializable{ export class ComponentB implements IComponentB, Initializable{
+6 -3
View File
@@ -1,8 +1,11 @@
import { Inject, Singleton } from "../../src/Decorator" import { Inject, Singleton } from "../../src/Decorator"
import { Initializable } from "../../src/Interfaces" import { COMPONENT_C_VALUE } from "../CONSTANTS"
import { COMPONENT_B_VALUE, COMPONENT_C_VALUE } from "../CONSTANTS"
import { ComponentA } from "./ComponentA" import { ComponentA } from "./ComponentA"
export class benis{
}
export abstract class IComponentC{ export abstract class IComponentC{
getFromA: () => string getFromA: () => string
getFromThis: () => string getFromThis: () => string
@@ -11,7 +14,7 @@ export abstract class IComponentC{
@Singleton({ @Singleton({
interface: IComponentC, interface: IComponentC,
}) })
export class ComponentC implements IComponentC{ export class ComponentC{
@Inject(ComponentA) @Inject(ComponentA)
private componentA: ComponentA private componentA: ComponentA
+1 -1
View File
@@ -9,7 +9,7 @@ var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised); chai.use(chaiAsPromised);
describe('dependjs', () => { describe('BasicTest', () => {
it('is able to resolve linear dependencies', () => { it('is able to resolve linear dependencies', () => {
const testComp = Injector.resolve(TestComponent) const testComp = Injector.resolve(TestComponent)
-1
View File
@@ -1,5 +1,4 @@
import { Inject, Singleton } from "../../src/Decorator"; import { Inject, Singleton } from "../../src/Decorator";
import { Initializable } from "../../src/Interfaces";
import {ComponentA} from "./ComponentA" import {ComponentA} from "./ComponentA"
import {IComponentB} from "./ComponentB" import {IComponentB} from "./ComponentB"
import { ComponentC } from "./ComponentC"; import { ComponentC } from "./ComponentC";
+2 -2
View File
@@ -8,12 +8,12 @@ var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised); chai.use(chaiAsPromised);
describe('dependjs', () => { describe('InitializationTest', () => {
it('initialized in the requested order', () => { it('initialized in the requested order', () => {
const testComp = Injector.resolve(TestComponent) const testComp = Injector.resolve(TestComponent)
const data = testComp.getData() 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])
}) })
}) })