Skip to content

Commit 7aad337

Browse files
author
Umed Khudoiberdiev
committed
added javascript support
1 parent f7676bd commit 7aad337

File tree

7 files changed

+51
-6
lines changed

7 files changed

+51
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {BeanFactory} from "./BeanFactory";
2+
import {SugarFactory} from "./SugarFactory";
3+
import {WaterFactory} from "./WaterFactory";
4+
5+
export class CoffeeMaker {
6+
7+
constructor(container) {
8+
this.beanFactory = container.get(BeanFactory);
9+
this.sugarFactory = container.get(SugarFactory);
10+
this.waterFactory = container.get(WaterFactory);
11+
this.authorizationToken = container.get("authorizationToken");
12+
}
13+
14+
make() {
15+
this.beanFactory.create();
16+
this.sugarFactory.create();
17+
this.waterFactory.create();
18+
19+
console.log("coffee is made. Authorization token: " + this.authorizationToken);
20+
}
21+
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class SugarFactory {
2+
3+
create() {
4+
console.log("sugar created");
5+
}
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class WaterFactory {
2+
3+
create() {
4+
console.log("water created");
5+
}
6+
7+
}

sample/sample4-javascript/app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import "reflect-metadata";
2+
import {Container} from "../../src/index";
3+
import {CoffeeMaker} from "./CoffeeMaker";
4+
5+
Container.of("my-container").set("authorizationToken", "!@#$%^&*");
6+
let coffeeMaker = Container.of("my-container").get(CoffeeMaker);
7+
console.log(coffeeMaker);
8+
coffeeMaker.make();

sample/sample4-require/app.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ContainerInstance.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ export class ContainerInstance {
292292
throw new MissingProvidedServiceTypeError(identifier);
293293

294294
params.unshift(null);
295+
296+
// if there are no constructor parameters in the class then pass a container instance into it
297+
// this allows us to support javascript where we don't have decorators and emitted metadata about dependencies
298+
// need to be injected, and user can use provided container to get instances he needs
299+
if (params.length === 1)
300+
params.push(this);
301+
295302
service.value = new (type.bind.apply(type, params))();
296303
}
297304

0 commit comments

Comments
 (0)