Sfoglia il codice sorgente

0.0.7 init order fix, add proper README

master
nitowa 2 anni fa
parent
commit
2a9ded772e
3 ha cambiato i file con 145 aggiunte e 3 eliminazioni
  1. 143
    1
      README.md
  2. 1
    1
      package.json
  3. 1
    1
      src/Injector.ts

+ 143
- 1
README.md Vedi File

@@ -1 +1,143 @@
1
-Hello World
1
+# Overview 
2
+
3
+[![Build Status](https://drone.nitowa.xyz/api/badges/npm-packages/depents/status.svg)](https://drone.nitowa.xyz/npm-packages/depents) 
4
+[![Current Version](https://img.shields.io/npm/v/depents.svg)](https://www.npmjs.com/package/depents)
5
+[![Weekly Downloads](https://img.shields.io/npm/dw/depents?color=important)](https://www.npmjs.com/package/depents)
6
+[![License Type](https://img.shields.io/npm/l/depents?color=blueviolet)](https://gitea.nitowa.xyz/docs/depents/src/branch/master/LICENSE.md)
7
+
8
+depents is a typescript dependency injection framework
9
+
10
+# How to install
11
+```
12
+npm i depents
13
+```
14
+
15
+# Quickstart (TL;DR)
16
+```typescript
17
+import {Singleton, Inject, Injector} from 'dependjs'
18
+
19
+
20
+@Singleton()
21
+class AClass{
22
+    private A = 'a'
23
+    
24
+    getA () {
25
+        return this.A
26
+    }
27
+}
28
+
29
+@Singleton()
30
+class BClass{
31
+
32
+    @Inject(AClass)
33
+    private a: AClass
34
+
35
+    getA(){
36
+        return this.a.getA()
37
+    }
38
+}
39
+
40
+
41
+console.log(Injector.resolve(BClass).getA()) //"a"
42
+
43
+```
44
+
45
+# Working with interfaces as non-default injection tokens
46
+As interfaces are not a runtime construct in javascript, classes have to be used in their place in order to work with injection resolution. 
47
+
48
+Consider this quick example:
49
+
50
+
51
+File `MyModule/Interface.ts`
52
+```typescript
53
+export abstract class IComponent{
54
+    get: () => string
55
+}
56
+```
57
+
58
+File `MyModule/Implementation.ts`
59
+```typescript
60
+import {IComponent} from 'Interface.ts'
61
+
62
+@Singleton({
63
+    interface: IComponentB,
64
+})
65
+export class ComponentB implements IComponent{
66
+    get(): string {
67
+        return "Hello!"
68
+    }
69
+}
70
+```
71
+
72
+File `Main.ts`
73
+```typescript
74
+import {IComponent} from 'MyModule/Interface.ts'
75
+import {ComponentB} from 'Module/Implementation.ts'
76
+ComponentB; //see Tree shaking pitfall below
77
+
78
+Injector.resolve(IComponent).get()
79
+```
80
+
81
+# Tree shaking pitfall
82
+TSC will attempt to exclude unused code in order to keep the compiled JS minimal. In the above example neither `Main.ts` nor `MyModule/Interface.ts` ever reference `MyModule/Implementation.ts` and it is therefore considered unused code. This will lead to the compilation output not containing the `ComponentB` class. To forcibly include `ComponentB`, it has to be referenced at least once - even if it is a senseless operation like in the above example.
83
+
84
+# Initialization
85
+This version of depents will simply call all constructors of classes marked as `Singleton` without arguments and it is therefore recommended that the user does not place initialization logic there. Alternatively, you may provide a function named `initialize` which will be called during Singleton creation.
86
+
87
+An example:
88
+
89
+```typescript
90
+@Singleton()
91
+export class Component{
92
+    private value: string
93
+
94
+    get(): string {
95
+        return this.value
96
+    }
97
+
98
+    initialize():void { //Called during object creation
99
+        this.value = "Hello!"
100
+    }
101
+}
102
+```
103
+
104
+If your code requires objects to be initialized in a particular order, you may pass an optional `initializationPriority` parameter to `@Singleton`. The respective initialize operations will be called in order of `initializationPriority` from **lowest to highest**. If omitted, the default value is 0.
105
+
106
+```typescript
107
+@Singleton({
108
+    initializationPriority: 0
109
+})
110
+export class InitializedFirst{
111
+    private value: number
112
+
113
+    get(): number {
114
+        return this.value
115
+    }
116
+
117
+    initialize():void {
118
+        this.value = 1
119
+    }
120
+}
121
+
122
+@Singleton({
123
+    initializationPriority: 1
124
+})
125
+export class InitializedSecond{
126
+    @Inject(InitializedFirst)
127
+    private otherSingleton: InitializedFirst
128
+
129
+    private value: number
130
+
131
+    get(): number {
132
+        return this.value
133
+    }
134
+
135
+    initialize():void {
136
+        this.value = 1 + this.otherSingleton.get()
137
+    }
138
+}
139
+
140
+console.log(Injector.resolve(InitializedSecond).get()) //2
141
+```
142
+
143
+# [Full documentation](https://gitea.nitowa.xyz/docs/depents)

+ 1
- 1
package.json Vedi File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "depents",
3
-  "version": "0.0.6",
3
+  "version": "0.0.7",
4 4
   "description": "depents is a typescript dependency injector",
5 5
   "main": "./js/Index.js",
6 6
   "repository": {

+ 1
- 1
src/Injector.ts Vedi File

@@ -71,7 +71,7 @@ class _Injector {
71 71
 
72 72
   private initializeSingletons = () => {
73 73
     this.singletonDefinitions
74
-      .sort((a, b) => (b.initializationPriority ?? 0) - (a.initializationPriority ?? 0))
74
+      .sort((a, b) => (a.initializationPriority ?? 0) - (b.initializationPriority ?? 0))
75 75
       .map(def => this.singletonObjects[def.ctor.name])
76 76
       .forEach(obj => obj.initialize ? obj.initialize() : undefined)
77 77
   }

Loading…
Annulla
Salva