-
Notifications
You must be signed in to change notification settings - Fork 14
/
decorators.ts
95 lines (74 loc) · 2.63 KB
/
decorators.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { interfaces } from "inversify";
const INJECTION = Symbol.for("INJECTION");
function _proxyGetter(
proto: any,
key: string,
resolve: () => any,
doCache: boolean
) {
function getter() {
if (doCache && !Reflect.hasMetadata(INJECTION, this, key)) {
Reflect.defineMetadata(INJECTION, resolve(), this, key);
}
if (Reflect.hasMetadata(INJECTION, this, key)) {
return Reflect.getMetadata(INJECTION, this, key);
} else {
return resolve();
}
}
function setter(newVal: any) {
Reflect.defineMetadata(INJECTION, newVal, this, key);
}
Object.defineProperty(proto, key, {
configurable: true,
enumerable: true,
get: getter,
set: setter
});
}
function makePropertyInjectDecorator(container: interfaces.Container, doCache: boolean) {
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>) {
return function(proto: any, key: string): void {
let resolve = () => {
return container.get(serviceIdentifier);
};
_proxyGetter(proto, key, resolve, doCache);
};
};
}
function makePropertyInjectNamedDecorator(container: interfaces.Container, doCache: boolean) {
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>, named: string) {
return function(proto: any, key: string): void {
let resolve = () => {
return container.getNamed(serviceIdentifier, named);
};
_proxyGetter(proto, key, resolve, doCache);
};
};
}
function makePropertyInjectTaggedDecorator(container: interfaces.Container, doCache: boolean) {
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>, key: string, value: any) {
return function(proto: any, propertyName: string): void {
let resolve = () => {
return container.getTagged(serviceIdentifier, key, value);
};
_proxyGetter(proto, propertyName , resolve, doCache);
};
};
}
function makePropertyMultiInjectDecorator(container: interfaces.Container, doCache: boolean) {
return function(serviceIdentifier: interfaces.ServiceIdentifier<any>) {
return function(proto: any, key: string): void {
let resolve = () => {
return container.getAll(serviceIdentifier);
};
_proxyGetter(proto, key, resolve, doCache);
};
};
}
export {
makePropertyInjectDecorator,
makePropertyMultiInjectDecorator,
makePropertyInjectTaggedDecorator,
makePropertyInjectNamedDecorator
};