This repository was archived by the owner on Apr 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (72 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
const ioc = require("@owja/ioc");
/**
* We use a example service class with some properties to show
* how the OWJA! IoC library works.
*
* The `random` property is just a random float.
*
* The property one and two are referencing the service itself.
* It is a circular dependency. To make this possible they don't
* access the container directly inside of the constructor. The
* dependencies are resolved when the properties get accessed.
* This is similar to the `@inject` decorator used in es next
* environments.
*/
class MyService {
constructor() {
this.random = Math.random();
this.one = () => container.get(TYPE.ServiceOne);
this.two = () => container.get(TYPE.ServiceTwo);
}
}
/**
* This is just a `Container` instance. The object where all
* dependencies are bound to. It is possible to use more than
* one container per project.
*/
const container = new ioc.Container();
/**
* We use `Symbol` to identify our dependencies. We use the
* `TYPE` constant to keep them together.
*/
const TYPE = {
ServiceOne: Symbol("ServiceOne"),
ServiceTwo: Symbol("ServiceTwo"),
};
/**
* Now we bind the dependencies to the symbols. The first
* dependency `TYPE.ServiceOne` will get resolved every time
* `container.get(TYPE.ServiceOne)` is called. The second
* dependency `TYPE.ServiceTwo` will only get resolved once.
*/
container.bind(TYPE.ServiceOne).to(MyService);
container.bind(TYPE.ServiceTwo).to(MyService).inSingletonScope();
/**
* Now we do some output. Because of we getting a new instance
* of `MyService` with the `TYPE.ServiceOne` type each time we
* get it from the container, all random numbers will be
* differently...
*/
console.log("\nTYPE.MyService is resolved every call");
console.log(container.get(TYPE.ServiceOne).random);
console.log(container.get(TYPE.ServiceOne).random);
console.log(container.get(TYPE.ServiceOne).random);
console.log(container.get(TYPE.ServiceOne).random);
/**
* ...but because of we bound `TYPE.ServiceTwo` in singleton scope
* the next numbers are all the same.
*/
console.log("\nTYPE.MyOtherService is resolved only once (in singleton scope)");
console.log(container.get(TYPE.ServiceTwo).random);
console.log(container.get(TYPE.ServiceTwo).random);
console.log(container.get(TYPE.ServiceTwo).random);
console.log(container.get(TYPE.ServiceTwo).random);
/**
* And the last output shows just that we can access all
* dependencies in a chain. At the end we get the `random`
* value out of `TYPE.ServiceTwo` and that's why it should
* be the same than the four outputs before.
*/
console.log("\nCan do weired things ^^");
console.log(container.get(TYPE.ServiceTwo).one().two().two().one().two().random);