Browse Source

reverse initialization order, prepare for publishing

master
nitowa 2 years ago
parent
commit
a373b178c6

+ 0
- 3
.npmignore View File

1
-test
2
-js
3
-node_modules

+ 5
- 2
package.json View File

1
 {
1
 {
2
   "name": "dependjs",
2
   "name": "dependjs",
3
   "version": "0.0.1",
3
   "version": "0.0.1",
4
-  "description": "dependjs is a javascript dependency injector",
4
+  "description": "dependjs is a typescript dependency injector",
5
   "main": "./js/Index.js",
5
   "main": "./js/Index.js",
6
   "repository": {
6
   "repository": {
7
     "type": "git",
7
     "type": "git",
13
   },
13
   },
14
   "homepage": "https://gitea.nitowa.xyz/docs/dependjs",
14
   "homepage": "https://gitea.nitowa.xyz/docs/dependjs",
15
   "keywords": [
15
   "keywords": [
16
+    "typescript",
16
     "dependency injection",
17
     "dependency injection",
17
     "inversion of control"
18
     "inversion of control"
18
   ],
19
   ],
38
     "mocha": "^6.2.0"
39
     "mocha": "^6.2.0"
39
   },
40
   },
40
   "files": [
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 View File

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

+ 2
- 2
src/Injector.ts View File

1
 import 'reflect-metadata';
1
 import 'reflect-metadata';
2
 import { ERR_NO_INITIALIZE_WITH_PRIORITY, ERR_NO_INJECTION_TOKEN } from './Strings';
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
 class _Injector {
5
 class _Injector {
6
 
6
 
71
 
71
 
72
   private initializeSingletons = () => {
72
   private initializeSingletons = () => {
73
     this.singletonDefinitions
73
     this.singletonDefinitions
74
-      .sort((a, b) => (a.initializationPriority ?? 0) - (b.initializationPriority ?? 0))
74
+      .sort((a, b) => (b.initializationPriority ?? 0) - (a.initializationPriority ?? 0))
75
       .map(def => this.singletonObjects[def.ctor.name])
75
       .map(def => this.singletonObjects[def.ctor.name])
76
       .forEach(obj => obj.initialize ? obj.initialize() : undefined)
76
       .forEach(obj => obj.initialize ? obj.initialize() : undefined)
77
   }
77
   }

+ 2
- 2
src/Internals.ts View File

12
  */
12
  */
13
 export type GenericClassDecorator<T> = (target: T) => void;
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
   ctor: Type<any>                 //Object constructor to make singleton from
17
   ctor: Type<any>                 //Object constructor to make singleton from
18
 }
18
 }
19
 
19
 

+ 1
- 1
test/BasicTest/ComponentB.ts View File

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

+ 6
- 3
test/BasicTest/ComponentC.ts View File

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

+ 1
- 1
test/BasicTest/Test.ts View File

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

+ 0
- 1
test/BasicTest/TestComponent.ts View File

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

+ 2
- 2
test/InitializationTest/Test.ts View File

8
 
8
 
9
 chai.use(chaiAsPromised);
9
 chai.use(chaiAsPromised);
10
 
10
 
11
-describe('dependjs', () => {
11
+describe('InitializationTest', () => {
12
     it('initialized in the requested order', () => {
12
     it('initialized in the requested order', () => {
13
 
13
 
14
         const testComp = Injector.resolve(TestComponent)
14
         const testComp = Injector.resolve(TestComponent)
15
         const data = testComp.getData()
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
 })

Loading…
Cancel
Save