Parcourir la source

reverse initialization order, prepare for publishing

master
nitowa il y a 2 ans
Parent
révision
a373b178c6

+ 0
- 3
.npmignore Voir le fichier

@@ -1,3 +0,0 @@
1
-test
2
-js
3
-node_modules

+ 5
- 2
package.json Voir le fichier

@@ -1,7 +1,7 @@
1 1
 {
2 2
   "name": "dependjs",
3 3
   "version": "0.0.1",
4
-  "description": "dependjs is a javascript dependency injector",
4
+  "description": "dependjs is a typescript dependency injector",
5 5
   "main": "./js/Index.js",
6 6
   "repository": {
7 7
     "type": "git",
@@ -13,6 +13,7 @@
13 13
   },
14 14
   "homepage": "https://gitea.nitowa.xyz/docs/dependjs",
15 15
   "keywords": [
16
+    "typescript",
16 17
     "dependency injection",
17 18
     "inversion of control"
18 19
   ],
@@ -38,6 +39,8 @@
38 39
     "mocha": "^6.2.0"
39 40
   },
40 41
   "files": [
41
-    "js"
42
+    "js/Index.js",
43
+    "js/Index.d.ts",
44
+    "js/src/*"
42 45
   ]
43 46
 }

+ 1
- 0
src/Decorator.ts Voir le fichier

@@ -9,6 +9,7 @@ export function Singleton(config?: {
9 9
   interface?: Constructor<any>,
10 10
   initializationPriority?: number
11 11
 }): GenericClassDecorator<Type<any>> {
12
+
12 13
   return (clazz: Type<any>) => {
13 14
     Injector['singletonDefinitions'].push({
14 15
       initializationPriority: config ?. initializationPriority,

+ 2
- 2
src/Injector.ts Voir le fichier

@@ -1,6 +1,6 @@
1 1
 import 'reflect-metadata';
2 2
 import { ERR_NO_INITIALIZE_WITH_PRIORITY, ERR_NO_INJECTION_TOKEN } from './Strings';
3
-import { Constructor, Type, Module as SingletonDefinition, InjectionError, InjectionResolutionError } from './Internals';
3
+import { Constructor, Type, SingletonDefinition, InjectionError, InjectionResolutionError } from './Internals';
4 4
 
5 5
 class _Injector {
6 6
 
@@ -71,7 +71,7 @@ class _Injector {
71 71
 
72 72
   private initializeSingletons = () => {
73 73
     this.singletonDefinitions
74
-      .sort((a, b) => (a.initializationPriority ?? 0) - (b.initializationPriority ?? 0))
74
+      .sort((a, b) => (b.initializationPriority ?? 0) - (a.initializationPriority ?? 0))
75 75
       .map(def => this.singletonObjects[def.ctor.name])
76 76
       .forEach(obj => obj.initialize ? obj.initialize() : undefined)
77 77
   }

+ 2
- 2
src/Internals.ts Voir le fichier

@@ -12,8 +12,8 @@ export type Constructor<T> = Function & { prototype: T }
12 12
  */
13 13
 export type GenericClassDecorator<T> = (target: T) => void;
14 14
 
15
-export type Module = {
16
-  initializationPriority?: number //Priority of initializing this object after creation 
15
+export type SingletonDefinition = {
16
+  initializationPriority?: number //Priority of initializing this object after creation, lower value means higher priority; default: 0
17 17
   ctor: Type<any>                 //Object constructor to make singleton from
18 18
 }
19 19
 

+ 1
- 1
test/BasicTest/ComponentB.ts Voir le fichier

@@ -9,7 +9,7 @@ export abstract class IComponentB{
9 9
 }
10 10
 
11 11
 @Singleton({
12
-    interface: IComponentB
12
+    interface: IComponentB,
13 13
 })
14 14
 export class ComponentB implements IComponentB, Initializable{
15 15
 

+ 6
- 3
test/BasicTest/ComponentC.ts Voir le fichier

@@ -1,8 +1,11 @@
1 1
 import { Inject, Singleton } from "../../src/Decorator"
2
-import { Initializable } from "../../src/Interfaces"
3
-import { COMPONENT_B_VALUE, COMPONENT_C_VALUE } from "../CONSTANTS"
2
+import { COMPONENT_C_VALUE } from "../CONSTANTS"
4 3
 import { ComponentA } from "./ComponentA"
5 4
 
5
+export class benis{
6
+
7
+}
8
+
6 9
 export abstract class IComponentC{
7 10
     getFromA: () => string
8 11
     getFromThis: () => string
@@ -11,7 +14,7 @@ export abstract class IComponentC{
11 14
 @Singleton({
12 15
     interface: IComponentC,
13 16
 })
14
-export class ComponentC implements IComponentC{
17
+export class ComponentC{
15 18
 
16 19
     @Inject(ComponentA)
17 20
     private componentA: ComponentA

+ 1
- 1
test/BasicTest/Test.ts Voir le fichier

@@ -9,7 +9,7 @@ var chaiAsPromised = require("chai-as-promised");
9 9
 
10 10
 chai.use(chaiAsPromised);
11 11
 
12
-describe('dependjs', () => {
12
+describe('BasicTest', () => {
13 13
     it('is able to resolve linear dependencies', () => {
14 14
 
15 15
         const testComp = Injector.resolve(TestComponent)

+ 0
- 1
test/BasicTest/TestComponent.ts Voir le fichier

@@ -1,5 +1,4 @@
1 1
 import { Inject, Singleton } from "../../src/Decorator";
2
-import { Initializable } from "../../src/Interfaces";
3 2
 import {ComponentA} from "./ComponentA"
4 3
 import {IComponentB} from "./ComponentB"
5 4
 import { ComponentC } from "./ComponentC";

+ 2
- 2
test/InitializationTest/Test.ts Voir le fichier

@@ -8,12 +8,12 @@ var chaiAsPromised = require("chai-as-promised");
8 8
 
9 9
 chai.use(chaiAsPromised);
10 10
 
11
-describe('dependjs', () => {
11
+describe('InitializationTest', () => {
12 12
     it('initialized in the requested order', () => {
13 13
 
14 14
         const testComp = Injector.resolve(TestComponent)
15 15
         const data = testComp.getData()
16 16
 
17
-        expect(data).to.eql([COMPONENT_C_VALUE, COMPONENT_B_VALUE, COMPONENT_A_VALUE])
17
+        expect(data).to.eql([COMPONENT_A_VALUE, COMPONENT_B_VALUE, COMPONENT_C_VALUE])
18 18
     })
19 19
 })

Chargement…
Annuler
Enregistrer