1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.InstanceLoader = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const messages_1 = require("../helpers/messages");
|
6 | const internal_core_module_1 = require("./internal-core-module/internal-core-module");
|
7 | class InstanceLoader {
|
8 | constructor(container, injector, graphInspector, logger = new common_1.Logger(InstanceLoader.name, {
|
9 | timestamp: true,
|
10 | })) {
|
11 | this.container = container;
|
12 | this.injector = injector;
|
13 | this.graphInspector = graphInspector;
|
14 | this.logger = logger;
|
15 | }
|
16 | setLogger(logger) {
|
17 | this.logger = logger;
|
18 | }
|
19 | async createInstancesOfDependencies(modules = this.container.getModules()) {
|
20 | this.createPrototypes(modules);
|
21 | try {
|
22 | await this.createInstances(modules);
|
23 | }
|
24 | catch (err) {
|
25 | this.graphInspector.inspectModules(modules);
|
26 | this.graphInspector.registerPartial(err);
|
27 | throw err;
|
28 | }
|
29 | this.graphInspector.inspectModules(modules);
|
30 | }
|
31 | createPrototypes(modules) {
|
32 | modules.forEach(moduleRef => {
|
33 | this.createPrototypesOfProviders(moduleRef);
|
34 | this.createPrototypesOfInjectables(moduleRef);
|
35 | this.createPrototypesOfControllers(moduleRef);
|
36 | });
|
37 | }
|
38 | async createInstances(modules) {
|
39 | await Promise.all([...modules.values()].map(async (moduleRef) => {
|
40 | await this.createInstancesOfProviders(moduleRef);
|
41 | await this.createInstancesOfInjectables(moduleRef);
|
42 | await this.createInstancesOfControllers(moduleRef);
|
43 | const { name } = moduleRef;
|
44 | this.isModuleWhitelisted(name) &&
|
45 | this.logger.log((0, messages_1.MODULE_INIT_MESSAGE) `${name}`);
|
46 | }));
|
47 | }
|
48 | createPrototypesOfProviders(moduleRef) {
|
49 | const { providers } = moduleRef;
|
50 | providers.forEach(wrapper => this.injector.loadPrototype(wrapper, providers));
|
51 | }
|
52 | async createInstancesOfProviders(moduleRef) {
|
53 | const { providers } = moduleRef;
|
54 | const wrappers = [...providers.values()];
|
55 | await Promise.all(wrappers.map(async (item) => {
|
56 | await this.injector.loadProvider(item, moduleRef);
|
57 | this.graphInspector.inspectInstanceWrapper(item, moduleRef);
|
58 | }));
|
59 | }
|
60 | createPrototypesOfControllers(moduleRef) {
|
61 | const { controllers } = moduleRef;
|
62 | controllers.forEach(wrapper => this.injector.loadPrototype(wrapper, controllers));
|
63 | }
|
64 | async createInstancesOfControllers(moduleRef) {
|
65 | const { controllers } = moduleRef;
|
66 | const wrappers = [...controllers.values()];
|
67 | await Promise.all(wrappers.map(async (item) => {
|
68 | await this.injector.loadController(item, moduleRef);
|
69 | this.graphInspector.inspectInstanceWrapper(item, moduleRef);
|
70 | }));
|
71 | }
|
72 | createPrototypesOfInjectables(moduleRef) {
|
73 | const { injectables } = moduleRef;
|
74 | injectables.forEach(wrapper => this.injector.loadPrototype(wrapper, injectables));
|
75 | }
|
76 | async createInstancesOfInjectables(moduleRef) {
|
77 | const { injectables } = moduleRef;
|
78 | const wrappers = [...injectables.values()];
|
79 | await Promise.all(wrappers.map(async (item) => {
|
80 | await this.injector.loadInjectable(item, moduleRef);
|
81 | this.graphInspector.inspectInstanceWrapper(item, moduleRef);
|
82 | }));
|
83 | }
|
84 | isModuleWhitelisted(name) {
|
85 | return name !== internal_core_module_1.InternalCoreModule.name;
|
86 | }
|
87 | }
|
88 | exports.InstanceLoader = InstanceLoader;
|