| @@ -123,7 +123,7 export class ActivationContext<S extends | |||||
| 123 |
|
123 | |||
| 124 | activate<T>(d: Descriptor<S, T>, name: string) { |
|
124 | activate<T>(d: Descriptor<S, T>, name: string) { | |
| 125 | if (trace.isLogEnabled()) |
|
125 | if (trace.isLogEnabled()) | |
| 126 |
trace.log( |
|
126 | trace.log("enter {0} {1}", name, d); | |
| 127 |
|
127 | |||
| 128 | const ctx = this.enter(d, name); |
|
128 | const ctx = this.enter(d, name); | |
| 129 | const v = d.activate(ctx); |
|
129 | const v = d.activate(ctx); | |
| @@ -26,6 +26,10 const emptyLifetime: ILifetime = Object. | |||||
| 26 |
|
26 | |||
| 27 | store() { |
|
27 | store() { | |
| 28 | // does nothing |
|
28 | // does nothing | |
|
|
29 | }, | |||
|
|
30 | ||||
|
|
31 | toString() { | |||
|
|
32 | return `[object EmptyLifetime]`; | |||
| 29 |
|
|
33 | } | |
| 30 |
|
34 | |||
| 31 | }); |
|
35 | }); | |
| @@ -42,6 +46,9 const unknownLifetime: ILifetime = Objec | |||||
| 42 | }, |
|
46 | }, | |
| 43 | store() { |
|
47 | store() { | |
| 44 | throw new Error("Can't store a value in the unknown lifetime object"); |
|
48 | throw new Error("Can't store a value in the unknown lifetime object"); | |
|
|
49 | }, | |||
|
|
50 | toString() { | |||
|
|
51 | return `[object UnknownLifetime]`; | |||
| 45 | } |
|
52 | } | |
| 46 | }); |
|
53 | }); | |
| 47 |
|
54 | |||
| @@ -112,7 +119,7 export class LifetimeManager implements | |||||
| 112 | return emptyLifetime; |
|
119 | return emptyLifetime; | |
| 113 | } |
|
120 | } | |
| 114 |
|
121 | |||
| 115 |
static hierarchyLifetime() |
|
122 | static hierarchyLifetime() { | |
| 116 | let _lifetime = unknownLifetime; |
|
123 | let _lifetime = unknownLifetime; | |
| 117 | return { |
|
124 | return { | |
| 118 | initialize(context: ActivationContext<any>) { |
|
125 | initialize(context: ActivationContext<any>) { | |
| @@ -129,11 +136,14 export class LifetimeManager implements | |||||
| 129 | }, |
|
136 | }, | |
| 130 | store(item: any, cleanup?: (item: any) => void) { |
|
137 | store(item: any, cleanup?: (item: any) => void) { | |
| 131 | return _lifetime.store(item, cleanup); |
|
138 | return _lifetime.store(item, cleanup); | |
|
|
139 | }, | |||
|
|
140 | toString() { | |||
|
|
141 | return `[object HierarchyLifetime, has=${this.has()}]`; | |||
| 132 | } |
|
142 | } | |
| 133 | }; |
|
143 | }; | |
| 134 | } |
|
144 | } | |
| 135 |
|
145 | |||
| 136 |
static contextLifetime() |
|
146 | static contextLifetime() { | |
| 137 | let _lifetime = unknownLifetime; |
|
147 | let _lifetime = unknownLifetime; | |
| 138 | return { |
|
148 | return { | |
| 139 | initialize(context: ActivationContext<any>) { |
|
149 | initialize(context: ActivationContext<any>) { | |
| @@ -149,11 +159,14 export class LifetimeManager implements | |||||
| 149 | }, |
|
159 | }, | |
| 150 | store(item: any) { |
|
160 | store(item: any) { | |
| 151 | _lifetime.store(item); |
|
161 | _lifetime.store(item); | |
|
|
162 | }, | |||
|
|
163 | toString() { | |||
|
|
164 | return `[object ContextLifetime, has=${this.has()}]`; | |||
| 152 | } |
|
165 | } | |
| 153 | }; |
|
166 | }; | |
| 154 | } |
|
167 | } | |
| 155 |
|
168 | |||
| 156 |
static singletonLifetime(typeId: string) |
|
169 | static singletonLifetime(typeId: string) { | |
| 157 | argumentNotEmptyString(typeId, "typeId"); |
|
170 | argumentNotEmptyString(typeId, "typeId"); | |
| 158 | let pending = false; |
|
171 | let pending = false; | |
| 159 | return { |
|
172 | return { | |
| @@ -173,6 +186,9 export class LifetimeManager implements | |||||
| 173 | store(item: any) { |
|
186 | store(item: any) { | |
| 174 | singletons[typeId] = item; |
|
187 | singletons[typeId] = item; | |
| 175 | pending = false; |
|
188 | pending = false; | |
|
|
189 | }, | |||
|
|
190 | toString() { | |||
|
|
191 | return `[object SingletonLifetime, has=${this.has()}, typeId=${typeId}]`; | |||
| 176 | } |
|
192 | } | |
| 177 | }; |
|
193 | }; | |
| 178 | } |
|
194 | } | |
| @@ -193,6 +209,9 export class LifetimeManager implements | |||||
| 193 | }, |
|
209 | }, | |
| 194 | store(item: any) { |
|
210 | store(item: any) { | |
| 195 | _lifetime.store(item); |
|
211 | _lifetime.store(item); | |
|
|
212 | }, | |||
|
|
213 | toString() { | |||
|
|
214 | return `[object ContainerLifetime, has=${_lifetime.has()}]` | |||
| 196 | } |
|
215 | } | |
| 197 | }; |
|
216 | }; | |
| 198 | } |
|
217 | } | |
| @@ -60,4 +60,7 export class DescriptorImpl<S extends ob | |||||
| 60 | return instance; |
|
60 | return instance; | |
| 61 | } |
|
61 | } | |
| 62 |
|
62 | |||
|
|
63 | toString() { | |||
|
|
64 | return `[object DescriptorImpl, lifetime=${this._lifetime}]`; | |||
|
|
65 | } | |||
| 63 | } |
|
66 | } | |
| @@ -6,6 +6,7 import { ValueDescriptor } from "../di/V | |||||
| 6 | import { Foo } from "../mock/Foo"; |
|
6 | import { Foo } from "../mock/Foo"; | |
| 7 | import { Bar } from "../mock/Bar"; |
|
7 | import { Bar } from "../mock/Bar"; | |
| 8 | import { isNull } from "../safe"; |
|
8 | import { isNull } from "../safe"; | |
|
|
9 | import { Box } from "../mock/Box"; | |||
| 9 |
|
10 | |||
| 10 | test("Container register/resolve tests", async t => { |
|
11 | test("Container register/resolve tests", async t => { | |
| 11 | const container = new Container<{ |
|
12 | const container = new Container<{ | |
| @@ -104,3 +105,21 test("Load configuration from module", a | |||||
| 104 | t.assert(!isNull(b1._v), "bar.foo should not be null"); |
|
105 | t.assert(!isNull(b1._v), "bar.foo should not be null"); | |
| 105 |
|
106 | |||
| 106 | }); |
|
107 | }); | |
|
|
108 | ||||
|
|
109 | test("Optional dependency with child container", async t => { | |||
|
|
110 | const container = new Container<{ | |||
|
|
111 | foo?: Foo; | |||
|
|
112 | box: Box<Foo>; | |||
|
|
113 | }>(); | |||
|
|
114 | await container.fluent({ | |||
|
|
115 | box: it => it.factory($ => new Box($("foo"))) | |||
|
|
116 | }); | |||
|
|
117 | ||||
|
|
118 | const child = await container.createChildContainer() | |||
|
|
119 | .fluent({ | |||
|
|
120 | foo: it => it.factory(() => new Foo()) | |||
|
|
121 | }) | |||
|
|
122 | ||||
|
|
123 | const box = child.resolve("box"); | |||
|
|
124 | t.assert(!isNull(box.getValue()), "'foo' dependency is declared in child container"); | |||
|
|
125 | }); No newline at end of file | |||
General Comments 0
You need to be logged in to leave comments.
Login now
