Skip to content

Commit 05d3c2b

Browse files
author
Umed Khudoiberdiev
committed
made Handler to accept a container instance
1 parent 8018b49 commit 05d3c2b

File tree

8 files changed

+15
-11
lines changed

8 files changed

+15
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
* deprecated `Require` decorator. Use es6 imports instead or named services
99
* inherited classes don't need to be decorated with `@Service` decorator
1010
* other small api changes
11+
* now `Handler`'s `value` accepts a container which requests the value

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ For example:
441441
export function Logger() {
442442
return function(object: Object, propertyName: string, index?: number) {
443443
const logger = new ConsoleLogger();
444-
Container.registerHandler({ object, propertyName, index, value: () => logger });
444+
Container.registerHandler({ object, propertyName, index, value: containerInstance => logger });
445445
};
446446
}
447447

sample/sample9-custom-decorator/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import {ConsoleLogger} from "./ConsoleLogger";
44
export function Logger() {
55
return function(object: Object, propertyName: string, index?: number) {
66
const logger = new ConsoleLogger();
7-
Container.registerHandler({ object, propertyName, index, value: () => logger });
7+
Container.registerHandler({ object, propertyName, index, value: containerInstance => logger });
88
};
99
}

src/ContainerInstance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export class ContainerInstance {
282282
return paramTypes.map((paramType, index) => {
283283
const paramHandler = Container.handlers.find(handler => handler.object === type && handler.index === index);
284284
if (paramHandler)
285-
return paramHandler.value();
285+
return paramHandler.value(this);
286286

287287
if (paramType && paramType.name && !this.isTypePrimitive(paramType.name)) {
288288
return this.get(paramType);
@@ -312,7 +312,7 @@ export class ContainerInstance {
312312
enumerable: true,
313313
writable: true,
314314
configurable: true,
315-
value: handler.value()
315+
value: handler.value(this)
316316
});
317317
});
318318
}

src/decorators/Inject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string|Token<any>
3030
object: target,
3131
propertyName: propertyName,
3232
index: index,
33-
value: () => {
33+
value: containerInstance => {
3434
let identifier: any;
3535
if (typeof typeOrName === "string") {
3636
identifier = typeOrName;
@@ -45,7 +45,7 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string|Token<any>
4545
if (identifier === Object)
4646
throw new CannotInjectError(target, propertyName);
4747

48-
return Container.get<any>(identifier);
48+
return containerInstance.get<any>(identifier);
4949
}
5050
});
5151
};

src/decorators/Require.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function Require(name: string) {
1313
object: target,
1414
propertyName: propertyName,
1515
index: index,
16-
value: () => require(name)
16+
value: containerInstance => require(name)
1717
});
1818
};
1919
}

src/types/Handler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {ContainerInstance} from "../ContainerInstance";
2+
13
/**
24
* Used to register special "handler" which will be executed on a service class during its initialization.
35
* It can be used to create custom decorators and set/replace service class properties and constructor parameters.
@@ -23,7 +25,8 @@ export interface Handler {
2325

2426
/**
2527
* Factory function that produces value that will be set to class property or constructor parameter.
28+
* Accepts container instance which requested the value.
2629
*/
27-
value: () => any;
30+
value: (container: ContainerInstance) => any;
2831

2932
}

test/Container.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ describe("Container", function() {
202202
Container.registerHandler({
203203
object: ExtraService,
204204
index: 0,
205-
value: () => 777
205+
value: containerInstance => 777
206206
});
207207

208208
Container.registerHandler({
209209
object: ExtraService,
210210
index: 1,
211-
value: () => "hello parameter"
211+
value: containerInstance => "hello parameter"
212212
});
213213

214214
Container.get(ExtraService).luckyNumber.should.be.equal(777);
@@ -223,7 +223,7 @@ describe("Container", function() {
223223
Container.registerHandler({
224224
object: target,
225225
propertyName: propertyName,
226-
value: () => value
226+
value: containerInstance => value
227227
});
228228
};
229229
}

0 commit comments

Comments
 (0)