1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
14 | if (k2 === undefined) k2 = k;
|
15 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
16 | }) : (function(o, m, k, k2) {
|
17 | if (k2 === undefined) k2 = k;
|
18 | o[k2] = m[k];
|
19 | }));
|
20 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
21 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
22 | }) : function(o, v) {
|
23 | o["default"] = v;
|
24 | });
|
25 | var __importStar = (this && this.__importStar) || function (mod) {
|
26 | if (mod && mod.__esModule) return mod;
|
27 | var result = {};
|
28 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
29 | __setModuleDefault(result, mod);
|
30 | return result;
|
31 | };
|
32 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
33 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
34 | return new (P || (P = Promise))(function (resolve, reject) {
|
35 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
36 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
37 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
38 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
39 | });
|
40 | };
|
41 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
42 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
43 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
44 | function verb(n) { return function (v) { return step([n, v]); }; }
|
45 | function step(op) {
|
46 | if (f) throw new TypeError("Generator is already executing.");
|
47 | while (_) try {
|
48 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
49 | if (y = 0, t) op = [op[0] & 2, t.value];
|
50 | switch (op[0]) {
|
51 | case 0: case 1: t = op; break;
|
52 | case 4: _.label++; return { value: op[1], done: false };
|
53 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
54 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
55 | default:
|
56 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
57 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
58 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
59 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
60 | if (t[2]) _.ops.pop();
|
61 | _.trys.pop(); continue;
|
62 | }
|
63 | op = body.call(thisArg, _);
|
64 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
65 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
66 | }
|
67 | };
|
68 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
69 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
70 | if (ar || !(i in from)) {
|
71 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
72 | ar[i] = from[i];
|
73 | }
|
74 | }
|
75 | return to.concat(ar || Array.prototype.slice.call(from));
|
76 | };
|
77 | Object.defineProperty(exports, "__esModule", { value: true });
|
78 | exports.resolveInstance = void 0;
|
79 | var error_msgs_1 = require("../constants/error_msgs");
|
80 | var literal_types_1 = require("../constants/literal_types");
|
81 | var METADATA_KEY = __importStar(require("../constants/metadata_keys"));
|
82 | var async_1 = require("../utils/async");
|
83 | function _resolveRequests(childRequests, resolveRequest) {
|
84 | return childRequests.reduce(function (resolvedRequests, childRequest) {
|
85 | var injection = resolveRequest(childRequest);
|
86 | var targetType = childRequest.target.type;
|
87 | if (targetType === literal_types_1.TargetTypeEnum.ConstructorArgument) {
|
88 | resolvedRequests.constructorInjections.push(injection);
|
89 | }
|
90 | else {
|
91 | resolvedRequests.propertyRequests.push(childRequest);
|
92 | resolvedRequests.propertyInjections.push(injection);
|
93 | }
|
94 | if (!resolvedRequests.isAsync) {
|
95 | resolvedRequests.isAsync = (0, async_1.isPromiseOrContainsPromise)(injection);
|
96 | }
|
97 | return resolvedRequests;
|
98 | }, { constructorInjections: [], propertyInjections: [], propertyRequests: [], isAsync: false });
|
99 | }
|
100 | function _createInstance(constr, childRequests, resolveRequest) {
|
101 | var result;
|
102 | if (childRequests.length > 0) {
|
103 | var resolved = _resolveRequests(childRequests, resolveRequest);
|
104 | var createInstanceWithInjectionsArg = __assign(__assign({}, resolved), { constr: constr });
|
105 | if (resolved.isAsync) {
|
106 | result = createInstanceWithInjectionsAsync(createInstanceWithInjectionsArg);
|
107 | }
|
108 | else {
|
109 | result = createInstanceWithInjections(createInstanceWithInjectionsArg);
|
110 | }
|
111 | }
|
112 | else {
|
113 | result = new constr();
|
114 | }
|
115 | return result;
|
116 | }
|
117 | function createInstanceWithInjections(args) {
|
118 | var _a;
|
119 | var instance = new ((_a = args.constr).bind.apply(_a, __spreadArray([void 0], args.constructorInjections, false)))();
|
120 | args.propertyRequests.forEach(function (r, index) {
|
121 | var property = r.target.identifier;
|
122 | var injection = args.propertyInjections[index];
|
123 | instance[property] = injection;
|
124 | });
|
125 | return instance;
|
126 | }
|
127 | function createInstanceWithInjectionsAsync(args) {
|
128 | return __awaiter(this, void 0, void 0, function () {
|
129 | var constructorInjections, propertyInjections;
|
130 | return __generator(this, function (_a) {
|
131 | switch (_a.label) {
|
132 | case 0: return [4, possiblyWaitInjections(args.constructorInjections)];
|
133 | case 1:
|
134 | constructorInjections = _a.sent();
|
135 | return [4, possiblyWaitInjections(args.propertyInjections)];
|
136 | case 2:
|
137 | propertyInjections = _a.sent();
|
138 | return [2, createInstanceWithInjections(__assign(__assign({}, args), { constructorInjections: constructorInjections, propertyInjections: propertyInjections }))];
|
139 | }
|
140 | });
|
141 | });
|
142 | }
|
143 | function possiblyWaitInjections(possiblePromiseinjections) {
|
144 | return __awaiter(this, void 0, void 0, function () {
|
145 | var injections, _i, possiblePromiseinjections_1, injection;
|
146 | return __generator(this, function (_a) {
|
147 | injections = [];
|
148 | for (_i = 0, possiblePromiseinjections_1 = possiblePromiseinjections; _i < possiblePromiseinjections_1.length; _i++) {
|
149 | injection = possiblePromiseinjections_1[_i];
|
150 | if (Array.isArray(injection)) {
|
151 | injections.push(Promise.all(injection));
|
152 | }
|
153 | else {
|
154 | injections.push(injection);
|
155 | }
|
156 | }
|
157 | return [2, Promise.all(injections)];
|
158 | });
|
159 | });
|
160 | }
|
161 | function _getInstanceAfterPostConstruct(constr, result) {
|
162 | var postConstructResult = _postConstruct(constr, result);
|
163 | if ((0, async_1.isPromise)(postConstructResult)) {
|
164 | return postConstructResult.then(function () { return result; });
|
165 | }
|
166 | else {
|
167 | return result;
|
168 | }
|
169 | }
|
170 | function _postConstruct(constr, instance) {
|
171 | var _a, _b;
|
172 | if (Reflect.hasMetadata(METADATA_KEY.POST_CONSTRUCT, constr)) {
|
173 | var data = Reflect.getMetadata(METADATA_KEY.POST_CONSTRUCT, constr);
|
174 | try {
|
175 | return (_b = (_a = instance)[data.value]) === null || _b === void 0 ? void 0 : _b.call(_a);
|
176 | }
|
177 | catch (e) {
|
178 | throw new Error((0, error_msgs_1.POST_CONSTRUCT_ERROR)(constr.name, e.message));
|
179 | }
|
180 | }
|
181 | }
|
182 | function _validateInstanceResolution(binding, constr) {
|
183 | if (binding.scope !== literal_types_1.BindingScopeEnum.Singleton) {
|
184 | _throwIfHandlingDeactivation(binding, constr);
|
185 | }
|
186 | }
|
187 | function _throwIfHandlingDeactivation(binding, constr) {
|
188 | var scopeErrorMessage = "Class cannot be instantiated in " + (binding.scope === literal_types_1.BindingScopeEnum.Request ?
|
189 | "request" :
|
190 | "transient") + " scope.";
|
191 | if (typeof binding.onDeactivation === "function") {
|
192 | throw new Error((0, error_msgs_1.ON_DEACTIVATION_ERROR)(constr.name, scopeErrorMessage));
|
193 | }
|
194 | if (Reflect.hasMetadata(METADATA_KEY.PRE_DESTROY, constr)) {
|
195 | throw new Error((0, error_msgs_1.PRE_DESTROY_ERROR)(constr.name, scopeErrorMessage));
|
196 | }
|
197 | }
|
198 | function resolveInstance(binding, constr, childRequests, resolveRequest) {
|
199 | _validateInstanceResolution(binding, constr);
|
200 | var result = _createInstance(constr, childRequests, resolveRequest);
|
201 | if ((0, async_1.isPromise)(result)) {
|
202 | return result.then(function (resolvedResult) { return _getInstanceAfterPostConstruct(constr, resolvedResult); });
|
203 | }
|
204 | else {
|
205 | return _getInstanceAfterPostConstruct(constr, result);
|
206 | }
|
207 | }
|
208 | exports.resolveInstance = resolveInstance;
|
209 |
|
\ | No newline at end of file |