-
Notifications
You must be signed in to change notification settings - Fork 0
/
Injectable.js
63 lines (63 loc) · 2.81 KB
/
Injectable.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getModuleInjects = exports.Inject = exports.Injectable = void 0;
const injectsMap = new Map();
function Injectable(targetModuleNames, injectName) {
return function (constructor) {
console.log("Message from @Injectable");
injectsMap.set(injectName || constructor.name, targetModuleNames.map((targetModuleName) => ({
injectName: injectName || constructor.name,
providerName: constructor.name,
moduleName: targetModuleName,
injected: false,
})));
return constructor;
};
}
exports.Injectable = Injectable;
function Inject(injectName) {
return function (target, propertyKey, parameterIndex) {
console.log("Message from @Inject");
const paramName = getArgumentName({
class: target,
index: parameterIndex,
});
console.log({
target,
propertyKey,
parameterIndex,
moduleName: target.name,
paramName,
});
const targetModuleRefs = injectsMap.get(injectName);
if (!targetModuleRefs || !targetModuleRefs.length) {
throw new Error(`Inject ${injectName} does not have any target module reference`);
}
const targetModuleRef = targetModuleRefs.find((targetModuleRef) => targetModuleRef.moduleName === target.name);
if (!targetModuleRef) {
throw new Error(`Inject ${injectName} is not registered for module ${target.name}`);
}
if (targetModuleRef.injected) {
throw new Error(`Inject ${injectName} is already injected to module ${target.name}`);
}
targetModuleRef.injected = true;
injectsMap.set(injectName, targetModuleRefs.map((ref) => {
if (ref.moduleName === target.name) {
return Object.assign(Object.assign({}, ref), { instanceName: paramName, injected: true });
}
return ref;
}));
};
}
exports.Inject = Inject;
function getModuleInjects(moduleName) {
return Array.from(injectsMap.values()).reduce((acc, targetModuleRefs) => acc.concat(targetModuleRefs.filter((targetModuleRef) => targetModuleRef.moduleName === moduleName)), []);
}
exports.getModuleInjects = getModuleInjects;
function getArgumentName({ class: target, index, }) {
var _a, _b, _c, _d, _e, _f;
return (_f = (_e = (_d = (_c = (_b = (_a = target
.toString()
.match(/\w+\((.*)\)/)[1]) === null || _a === void 0 ? void 0 : _a.split(",")) === null || _b === void 0 ? void 0 : _b[index]) === null || _c === void 0 ? void 0 : _c.trim()) === null || _d === void 0 ? void 0 : _d.split(" ")) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.trim();
}
//# sourceMappingURL=Injectable.js.map