1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('tslib')) :
|
3 | typeof define === 'function' && define.amd ? define(['exports', 'tslib'], factory) :
|
4 | (global = global || self, factory(global['injection-js'] = {}, global.tslib));
|
5 | }(this, (function (exports, tslib) { 'use strict';
|
6 |
|
7 | var globalScope;
|
8 | if (typeof window === 'undefined') {
|
9 | if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
|
10 |
|
11 | globalScope = self;
|
12 | }
|
13 | else {
|
14 | globalScope = global;
|
15 | }
|
16 | }
|
17 | else {
|
18 | globalScope = window;
|
19 | }
|
20 |
|
21 |
|
22 | var _global = globalScope;
|
23 | function stringify(token) {
|
24 | if (typeof token === 'string') {
|
25 | return token;
|
26 | }
|
27 | if (token == null) {
|
28 | return '' + token;
|
29 | }
|
30 | if (token.overriddenName) {
|
31 | return "" + token.overriddenName;
|
32 | }
|
33 | if (token.name) {
|
34 | return "" + token.name;
|
35 | }
|
36 | var res = token.toString();
|
37 | var newLineIndex = res.indexOf('\n');
|
38 | return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var _nextClassId = 0;
|
49 | var Reflect = _global['Reflect'];
|
50 | function extractAnnotation(annotation) {
|
51 | if (typeof annotation === 'function' && annotation.hasOwnProperty('annotation')) {
|
52 |
|
53 | annotation = annotation.annotation;
|
54 | }
|
55 | return annotation;
|
56 | }
|
57 | function applyParams(fnOrArray, key) {
|
58 | if (fnOrArray === Object || fnOrArray === String || fnOrArray === Function || fnOrArray === Number || fnOrArray === Array) {
|
59 | throw new Error("Can not use native " + stringify(fnOrArray) + " as constructor");
|
60 | }
|
61 | if (typeof fnOrArray === 'function') {
|
62 | return fnOrArray;
|
63 | }
|
64 | if (Array.isArray(fnOrArray)) {
|
65 | var annotations = fnOrArray;
|
66 | var annoLength = annotations.length - 1;
|
67 | var fn = fnOrArray[annoLength];
|
68 | if (typeof fn !== 'function') {
|
69 | throw new Error("Last position of Class method array must be Function in key " + key + " was '" + stringify(fn) + "'");
|
70 | }
|
71 | if (annoLength !== fn.length) {
|
72 | throw new Error("Number of annotations (" + annoLength + ") does not match number of arguments (" + fn.length + ") in the function: " + stringify(fn));
|
73 | }
|
74 | var paramsAnnotations = [];
|
75 | for (var i = 0, ii = annotations.length - 1; i < ii; i++) {
|
76 | var paramAnnotations = [];
|
77 | paramsAnnotations.push(paramAnnotations);
|
78 | var annotation = annotations[i];
|
79 | if (Array.isArray(annotation)) {
|
80 | for (var j = 0; j < annotation.length; j++) {
|
81 | paramAnnotations.push(extractAnnotation(annotation[j]));
|
82 | }
|
83 | }
|
84 | else if (typeof annotation === 'function') {
|
85 | paramAnnotations.push(extractAnnotation(annotation));
|
86 | }
|
87 | else {
|
88 | paramAnnotations.push(annotation);
|
89 | }
|
90 | }
|
91 | Reflect.defineMetadata('parameters', paramsAnnotations, fn);
|
92 | return fn;
|
93 | }
|
94 | throw new Error("Only Function or Array is supported in Class definition for key '" + key + "' is '" + stringify(fnOrArray) + "'");
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | function Class(clsDef) {
|
180 | var constructor = applyParams(clsDef.hasOwnProperty('constructor') ? clsDef.constructor : undefined, 'constructor');
|
181 | var proto = constructor.prototype;
|
182 | if (clsDef.hasOwnProperty('extends')) {
|
183 | if (typeof clsDef.extends === 'function') {
|
184 | constructor.prototype = proto = Object.create(clsDef.extends.prototype);
|
185 | }
|
186 | else {
|
187 | throw new Error("Class definition 'extends' property must be a constructor function was: " + stringify(clsDef.extends));
|
188 | }
|
189 | }
|
190 | for (var key in clsDef) {
|
191 | if (key !== 'extends' && key !== 'prototype' && clsDef.hasOwnProperty(key)) {
|
192 | proto[key] = applyParams(clsDef[key], key);
|
193 | }
|
194 | }
|
195 | if (this && this.annotations instanceof Array) {
|
196 | Reflect.defineMetadata('annotations', this.annotations, constructor);
|
197 | }
|
198 | var constructorName = constructor['name'];
|
199 | if (!constructorName || constructorName === 'constructor') {
|
200 | constructor['overriddenName'] = "class" + _nextClassId++;
|
201 | }
|
202 | return constructor;
|
203 | }
|
204 | /**
|
205 | * @suppress {globalThis}
|
206 | */
|
207 | function makeDecorator(name, props, parentClass, chainFn) {
|
208 | var metaCtor = makeMetadataCtor([props]);
|
209 | function DecoratorFactory(objOrType) {
|
210 | if (!(Reflect && Reflect.getOwnMetadata)) {
|
211 | throw 'reflect-metadata shim is required when using class decorators';
|
212 | }
|
213 | if (this instanceof DecoratorFactory) {
|
214 | metaCtor.call(this, objOrType);
|
215 | return this;
|
216 | }
|
217 | var annotationInstance = new DecoratorFactory(objOrType);
|
218 | var chainAnnotation = typeof this === 'function' && Array.isArray(this.annotations) ? this.annotations : [];
|
219 | chainAnnotation.push(annotationInstance);
|
220 | var TypeDecorator = function TypeDecorator(cls) {
|
221 | var annotations = Reflect.getOwnMetadata('annotations', cls) || [];
|
222 | annotations.push(annotationInstance);
|
223 | Reflect.defineMetadata('annotations', annotations, cls);
|
224 | return cls;
|
225 | };
|
226 | TypeDecorator.annotations = chainAnnotation;
|
227 | TypeDecorator.Class = Class;
|
228 | if (chainFn)
|
229 | chainFn(TypeDecorator);
|
230 | return TypeDecorator;
|
231 | }
|
232 | if (parentClass) {
|
233 | DecoratorFactory.prototype = Object.create(parentClass.prototype);
|
234 | }
|
235 | DecoratorFactory.prototype.toString = function () { return "@" + name; };
|
236 | DecoratorFactory.annotationCls = DecoratorFactory;
|
237 | return DecoratorFactory;
|
238 | }
|
239 | function makeMetadataCtor(props) {
|
240 | return function ctor() {
|
241 | var _this = this;
|
242 | var args = [];
|
243 | for (var _i = 0; _i < arguments.length; _i++) {
|
244 | args[_i] = arguments[_i];
|
245 | }
|
246 | props.forEach(function (prop, i) {
|
247 | var argVal = args[i];
|
248 | if (Array.isArray(prop)) {
|
249 |
|
250 | _this[prop[0]] = argVal === undefined ? prop[1] : argVal;
|
251 | }
|
252 | else {
|
253 | for (var propName in prop) {
|
254 | _this[propName] = argVal && argVal.hasOwnProperty(propName) ? argVal[propName] : prop[propName];
|
255 | }
|
256 | }
|
257 | });
|
258 | };
|
259 | }
|
260 | function makeParamDecorator(name, props, parentClass) {
|
261 | var metaCtor = makeMetadataCtor(props);
|
262 | function ParamDecoratorFactory() {
|
263 | var _a;
|
264 | var args = [];
|
265 | for (var _i = 0; _i < arguments.length; _i++) {
|
266 | args[_i] = arguments[_i];
|
267 | }
|
268 | if (this instanceof ParamDecoratorFactory) {
|
269 | metaCtor.apply(this, args);
|
270 | return this;
|
271 | }
|
272 | var annotationInstance = new ((_a = ParamDecoratorFactory).bind.apply(_a, tslib.__spreadArrays([void 0], args)))();
|
273 | ParamDecorator.annotation = annotationInstance;
|
274 | return ParamDecorator;
|
275 | function ParamDecorator(cls, unusedKey, index) {
|
276 | var parameters = Reflect.getOwnMetadata('parameters', cls) || [];
|
277 |
|
278 |
|
279 | while (parameters.length <= index) {
|
280 | parameters.push(null);
|
281 | }
|
282 | parameters[index] = parameters[index] || [];
|
283 | parameters[index].push(annotationInstance);
|
284 | Reflect.defineMetadata('parameters', parameters, cls);
|
285 | return cls;
|
286 | }
|
287 | }
|
288 | if (parentClass) {
|
289 | ParamDecoratorFactory.prototype = Object.create(parentClass.prototype);
|
290 | }
|
291 | ParamDecoratorFactory.prototype.toString = function () { return "@" + name; };
|
292 | ParamDecoratorFactory.annotationCls = ParamDecoratorFactory;
|
293 | return ParamDecoratorFactory;
|
294 | }
|
295 | function makePropDecorator(name, props, parentClass) {
|
296 | var metaCtor = makeMetadataCtor(props);
|
297 | function PropDecoratorFactory() {
|
298 | var _a;
|
299 | var args = [];
|
300 | for (var _i = 0; _i < arguments.length; _i++) {
|
301 | args[_i] = arguments[_i];
|
302 | }
|
303 | if (this instanceof PropDecoratorFactory) {
|
304 | metaCtor.apply(this, args);
|
305 | return this;
|
306 | }
|
307 | var decoratorInstance = new ((_a = PropDecoratorFactory).bind.apply(_a, tslib.__spreadArrays([void 0], args)))();
|
308 | return function PropDecorator(target, name) {
|
309 | var meta = Reflect.getOwnMetadata('propMetadata', target.constructor) || {};
|
310 | meta[name] = (meta.hasOwnProperty(name) && meta[name]) || [];
|
311 | meta[name].unshift(decoratorInstance);
|
312 | Reflect.defineMetadata('propMetadata', meta, target.constructor);
|
313 | };
|
314 | }
|
315 | if (parentClass) {
|
316 | PropDecoratorFactory.prototype = Object.create(parentClass.prototype);
|
317 | }
|
318 | PropDecoratorFactory.prototype.toString = function () { return "@" + name; };
|
319 | PropDecoratorFactory.annotationCls = PropDecoratorFactory;
|
320 | return PropDecoratorFactory;
|
321 | }
|
322 |
|
323 | |
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | |
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 | var Inject = makeParamDecorator('Inject', [['token', undefined]]);
|
337 | |
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | var Optional = makeParamDecorator('Optional', []);
|
344 | |
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | var Injectable = makeDecorator('Injectable', []);
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | var Self = makeParamDecorator('Self', []);
|
358 | |
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | var SkipSelf = makeParamDecorator('SkipSelf', []);
|
365 | |
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | var Host = makeParamDecorator('Host', []);
|
372 |
|
373 | |
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | |
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | function forwardRef(forwardRefFn) {
|
393 | forwardRefFn.__forward_ref__ = forwardRef;
|
394 | forwardRefFn.toString = function () {
|
395 | return stringify(this());
|
396 | };
|
397 | return forwardRefFn;
|
398 | }
|
399 | |
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | function resolveForwardRef(type) {
|
412 | if (typeof type === 'function' && type.hasOwnProperty('__forward_ref__') && type.__forward_ref__ === forwardRef) {
|
413 | return type();
|
414 | }
|
415 | else {
|
416 | return type;
|
417 | }
|
418 | }
|
419 |
|
420 | |
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | var _THROW_IF_NOT_FOUND = new Object();
|
428 | var THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
429 |
|
430 | var _NullInjector = (function () {
|
431 | function _NullInjector() {
|
432 | }
|
433 | _NullInjector.prototype.get = function (token, notFoundValue) {
|
434 | if (notFoundValue === void 0) { notFoundValue = _THROW_IF_NOT_FOUND; }
|
435 | if (notFoundValue === _THROW_IF_NOT_FOUND) {
|
436 | throw new Error("No provider for " + stringify(token) + "!");
|
437 | }
|
438 | return notFoundValue;
|
439 | };
|
440 | return _NullInjector;
|
441 | }());
|
442 | |
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 | var Injector = (function () {
|
463 | function Injector() {
|
464 | }
|
465 | Injector.THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
466 | Injector.NULL = new _NullInjector();
|
467 | return Injector;
|
468 | }());
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 | var ERROR_ORIGINAL_ERROR = 'ngOriginalError';
|
478 | function getOriginalError(error) {
|
479 | return error[ERROR_ORIGINAL_ERROR];
|
480 | }
|
481 | function wrappedError(message, originalError) {
|
482 | var msg = message + " caused by: " + (originalError instanceof Error ? originalError.message : originalError);
|
483 | var error = Error(msg);
|
484 | error[ERROR_ORIGINAL_ERROR] = originalError;
|
485 | return error;
|
486 | }
|
487 |
|
488 | |
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 | function findFirstClosedCycle(keys) {
|
496 | var res = [];
|
497 | for (var i = 0; i < keys.length; ++i) {
|
498 | if (res.indexOf(keys[i]) > -1) {
|
499 | res.push(keys[i]);
|
500 | return res;
|
501 | }
|
502 | res.push(keys[i]);
|
503 | }
|
504 | return res;
|
505 | }
|
506 | function constructResolvingPath(keys) {
|
507 | if (keys.length > 1) {
|
508 | var reversed = findFirstClosedCycle(keys.slice().reverse());
|
509 | var tokenStrs = reversed.map(function (k) { return stringify(k.token); });
|
510 | return ' (' + tokenStrs.join(' -> ') + ')';
|
511 | }
|
512 | return '';
|
513 | }
|
514 | function injectionError(injector, key, constructResolvingMessage, originalError) {
|
515 | var error = (originalError ? wrappedError('', originalError) : Error());
|
516 | error.addKey = addKey;
|
517 | error.keys = [key];
|
518 | error.injectors = [injector];
|
519 | error.constructResolvingMessage = constructResolvingMessage;
|
520 | error.message = error.constructResolvingMessage();
|
521 | error[ERROR_ORIGINAL_ERROR] = originalError;
|
522 | return error;
|
523 | }
|
524 | function addKey(injector, key) {
|
525 | this.injectors.push(injector);
|
526 | this.keys.push(key);
|
527 | this.message = this.constructResolvingMessage();
|
528 | }
|
529 | |
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 | function noProviderError(injector, key) {
|
544 | return injectionError(injector, key, function () {
|
545 | var first = stringify(this.keys[0].token);
|
546 | return "No provider for " + first + "!" + constructResolvingPath(this.keys);
|
547 | });
|
548 | }
|
549 | |
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 | function cyclicDependencyError(injector, key) {
|
566 | return injectionError(injector, key, function () {
|
567 | return "Cannot instantiate cyclic dependency!" + constructResolvingPath(this.keys);
|
568 | });
|
569 | }
|
570 | |
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 | function instantiationError(injector, originalException, originalStack, key) {
|
597 | return injectionError(injector, key, function () {
|
598 | var first = stringify(this.keys[0].token);
|
599 | return getOriginalError(this).message + ": Error during instantiation of " + first + "!" + constructResolvingPath(this.keys) + ".";
|
600 | }, originalException);
|
601 | }
|
602 | |
603 |
|
604 |
|
605 |
|
606 |
|
607 |
|
608 |
|
609 |
|
610 |
|
611 |
|
612 | function invalidProviderError(provider) {
|
613 | return Error("Invalid provider - only instances of Provider and Type are allowed, got: " + provider);
|
614 | }
|
615 | |
616 |
|
617 |
|
618 |
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 | function noAnnotationError(typeOrFunc, params) {
|
645 | var signature = [];
|
646 | for (var i = 0, ii = params.length; i < ii; i++) {
|
647 | var parameter = params[i];
|
648 | if (!parameter || parameter.length === 0) {
|
649 | signature.push('?');
|
650 | }
|
651 | else {
|
652 | signature.push(parameter.map(stringify).join(' '));
|
653 | }
|
654 | }
|
655 | return Error("Cannot resolve all parameters for '" +
|
656 | stringify(typeOrFunc) +
|
657 | "'(" +
|
658 | signature.join(', ') +
|
659 | '). ' +
|
660 | "Make sure that all the parameters are decorated with Inject or have valid type annotations and that '" +
|
661 | stringify(typeOrFunc) +
|
662 | "' is decorated with Injectable.");
|
663 | }
|
664 | |
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 | function outOfBoundsError(index) {
|
679 | return Error("Index " + index + " is out-of-bounds.");
|
680 | }
|
681 |
|
682 | |
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 |
|
694 | function mixingMultiProvidersWithRegularProvidersError(provider1, provider2) {
|
695 | return Error("Cannot mix multi providers and regular providers, got: " + provider1 + " " + provider2);
|
696 | }
|
697 |
|
698 | |
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 | |
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 | var ReflectiveKey = (function () {
|
722 | |
723 |
|
724 |
|
725 | function ReflectiveKey(token, id) {
|
726 | this.token = token;
|
727 | this.id = id;
|
728 | if (!token) {
|
729 | throw new Error('Token must be defined!');
|
730 | }
|
731 | }
|
732 | Object.defineProperty(ReflectiveKey.prototype, "displayName", {
|
733 | |
734 |
|
735 |
|
736 | get: function () {
|
737 | return stringify(this.token);
|
738 | },
|
739 | enumerable: false,
|
740 | configurable: true
|
741 | });
|
742 | |
743 |
|
744 |
|
745 | ReflectiveKey.get = function (token) {
|
746 |
|
747 | return _globalKeyRegistry.get(resolveForwardRef(token));
|
748 | };
|
749 | Object.defineProperty(ReflectiveKey, "numberOfKeys", {
|
750 | |
751 |
|
752 |
|
753 | get: function () {
|
754 |
|
755 | return _globalKeyRegistry.numberOfKeys;
|
756 | },
|
757 | enumerable: false,
|
758 | configurable: true
|
759 | });
|
760 | return ReflectiveKey;
|
761 | }());
|
762 | |
763 |
|
764 |
|
765 | var KeyRegistry = (function () {
|
766 | function KeyRegistry() {
|
767 | this._allKeys = new Map();
|
768 | }
|
769 | KeyRegistry.prototype.get = function (token) {
|
770 | if (token instanceof ReflectiveKey)
|
771 | return token;
|
772 | if (this._allKeys.has(token)) {
|
773 | return this._allKeys.get(token);
|
774 | }
|
775 | var newKey = new ReflectiveKey(token, ReflectiveKey.numberOfKeys);
|
776 | this._allKeys.set(token, newKey);
|
777 | return newKey;
|
778 | };
|
779 | Object.defineProperty(KeyRegistry.prototype, "numberOfKeys", {
|
780 | get: function () {
|
781 | return this._allKeys.size;
|
782 | },
|
783 | enumerable: false,
|
784 | configurable: true
|
785 | });
|
786 | return KeyRegistry;
|
787 | }());
|
788 | var _globalKeyRegistry = new KeyRegistry();
|
789 |
|
790 | |
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 | |
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 | var Type = Function;
|
808 | function isType(v) {
|
809 | return typeof v === 'function';
|
810 | }
|
811 |
|
812 | |
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | |
820 |
|
821 |
|
822 | var DELEGATE_CTOR = /^function\s+\S+\(\)\s*{[\s\S]+\.apply\(this,\s*arguments\)/;
|
823 | var ReflectionCapabilities = (function () {
|
824 | function ReflectionCapabilities(reflect) {
|
825 | this._reflect = reflect || _global['Reflect'];
|
826 | }
|
827 | ReflectionCapabilities.prototype.isReflectionEnabled = function () {
|
828 | return true;
|
829 | };
|
830 | ReflectionCapabilities.prototype.factory = function (t) {
|
831 | return function () {
|
832 | var args = [];
|
833 | for (var _i = 0; _i < arguments.length; _i++) {
|
834 | args[_i] = arguments[_i];
|
835 | }
|
836 | return new (t.bind.apply(t, tslib.__spreadArrays([void 0], args)))();
|
837 | };
|
838 | };
|
839 |
|
840 | ReflectionCapabilities.prototype._zipTypesAndAnnotations = function (paramTypes, paramAnnotations) {
|
841 | var result;
|
842 | if (typeof paramTypes === 'undefined') {
|
843 | result = new Array(paramAnnotations.length);
|
844 | }
|
845 | else {
|
846 | result = new Array(paramTypes.length);
|
847 | }
|
848 | for (var i = 0; i < result.length; i++) {
|
849 |
|
850 |
|
851 |
|
852 | if (typeof paramTypes === 'undefined') {
|
853 | result[i] = [];
|
854 |
|
855 | }
|
856 | else if (paramTypes[i] != Object) {
|
857 | result[i] = [paramTypes[i]];
|
858 | }
|
859 | else {
|
860 | result[i] = [];
|
861 | }
|
862 | if (paramAnnotations && paramAnnotations[i] != null) {
|
863 | result[i] = result[i].concat(paramAnnotations[i]);
|
864 | }
|
865 | }
|
866 | return result;
|
867 | };
|
868 | ReflectionCapabilities.prototype._ownParameters = function (type, parentCtor) {
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 | if (DELEGATE_CTOR.exec(type.toString())) {
|
877 | return null;
|
878 | }
|
879 |
|
880 | if (type.parameters && type.parameters !== parentCtor.parameters) {
|
881 | return type.parameters;
|
882 | }
|
883 |
|
884 | var tsickleCtorParams = type.ctorParameters;
|
885 | if (tsickleCtorParams && tsickleCtorParams !== parentCtor.ctorParameters) {
|
886 |
|
887 |
|
888 | var ctorParameters = typeof tsickleCtorParams === 'function' ? tsickleCtorParams() : tsickleCtorParams;
|
889 | var paramTypes = ctorParameters.map(function (ctorParam) { return ctorParam && ctorParam.type; });
|
890 | var paramAnnotations = ctorParameters.map(function (ctorParam) { return ctorParam && convertTsickleDecoratorIntoMetadata(ctorParam.decorators); });
|
891 | return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
|
892 | }
|
893 |
|
894 | if (this._reflect != null && this._reflect.getOwnMetadata != null) {
|
895 | var paramAnnotations = this._reflect.getOwnMetadata('parameters', type);
|
896 | var paramTypes = this._reflect.getOwnMetadata('design:paramtypes', type);
|
897 | if (paramTypes || paramAnnotations) {
|
898 | return this._zipTypesAndAnnotations(paramTypes, paramAnnotations);
|
899 | }
|
900 | }
|
901 |
|
902 |
|
903 |
|
904 |
|
905 | return new Array(type.length).fill(undefined);
|
906 | };
|
907 | ReflectionCapabilities.prototype.parameters = function (type) {
|
908 |
|
909 |
|
910 | if (!isType(type)) {
|
911 | return [];
|
912 | }
|
913 | var parentCtor = getParentCtor(type);
|
914 | var parameters = this._ownParameters(type, parentCtor);
|
915 | if (!parameters && parentCtor !== Object) {
|
916 | parameters = this.parameters(parentCtor);
|
917 | }
|
918 | return parameters || [];
|
919 | };
|
920 | ReflectionCapabilities.prototype._ownAnnotations = function (typeOrFunc, parentCtor) {
|
921 |
|
922 | if (typeOrFunc.annotations && typeOrFunc.annotations !== parentCtor.annotations) {
|
923 | var annotations = typeOrFunc.annotations;
|
924 | if (typeof annotations === 'function' && annotations.annotations) {
|
925 | annotations = annotations.annotations;
|
926 | }
|
927 | return annotations;
|
928 | }
|
929 |
|
930 | if (typeOrFunc.decorators && typeOrFunc.decorators !== parentCtor.decorators) {
|
931 | return convertTsickleDecoratorIntoMetadata(typeOrFunc.decorators);
|
932 | }
|
933 |
|
934 | if (this._reflect && this._reflect.getOwnMetadata) {
|
935 | return this._reflect.getOwnMetadata('annotations', typeOrFunc);
|
936 | }
|
937 | return null;
|
938 | };
|
939 | ReflectionCapabilities.prototype.annotations = function (typeOrFunc) {
|
940 | if (!isType(typeOrFunc)) {
|
941 | return [];
|
942 | }
|
943 | var parentCtor = getParentCtor(typeOrFunc);
|
944 | var ownAnnotations = this._ownAnnotations(typeOrFunc, parentCtor) || [];
|
945 | var parentAnnotations = parentCtor !== Object ? this.annotations(parentCtor) : [];
|
946 | return parentAnnotations.concat(ownAnnotations);
|
947 | };
|
948 | ReflectionCapabilities.prototype._ownPropMetadata = function (typeOrFunc, parentCtor) {
|
949 |
|
950 | if (typeOrFunc.propMetadata && typeOrFunc.propMetadata !== parentCtor.propMetadata) {
|
951 | var propMetadata = typeOrFunc.propMetadata;
|
952 | if (typeof propMetadata === 'function' && propMetadata.propMetadata) {
|
953 | propMetadata = propMetadata.propMetadata;
|
954 | }
|
955 | return propMetadata;
|
956 | }
|
957 |
|
958 | if (typeOrFunc.propDecorators && typeOrFunc.propDecorators !== parentCtor.propDecorators) {
|
959 | var propDecorators_1 = typeOrFunc.propDecorators;
|
960 | var propMetadata_1 = {};
|
961 | Object.keys(propDecorators_1).forEach(function (prop) {
|
962 | propMetadata_1[prop] = convertTsickleDecoratorIntoMetadata(propDecorators_1[prop]);
|
963 | });
|
964 | return propMetadata_1;
|
965 | }
|
966 |
|
967 | if (this._reflect && this._reflect.getOwnMetadata) {
|
968 | return this._reflect.getOwnMetadata('propMetadata', typeOrFunc);
|
969 | }
|
970 | return null;
|
971 | };
|
972 | ReflectionCapabilities.prototype.propMetadata = function (typeOrFunc) {
|
973 | if (!isType(typeOrFunc)) {
|
974 | return {};
|
975 | }
|
976 | var parentCtor = getParentCtor(typeOrFunc);
|
977 | var propMetadata = {};
|
978 | if (parentCtor !== Object) {
|
979 | var parentPropMetadata_1 = this.propMetadata(parentCtor);
|
980 | Object.keys(parentPropMetadata_1).forEach(function (propName) {
|
981 | propMetadata[propName] = parentPropMetadata_1[propName];
|
982 | });
|
983 | }
|
984 | var ownPropMetadata = this._ownPropMetadata(typeOrFunc, parentCtor);
|
985 | if (ownPropMetadata) {
|
986 | Object.keys(ownPropMetadata).forEach(function (propName) {
|
987 | var decorators = [];
|
988 | if (propMetadata.hasOwnProperty(propName)) {
|
989 | decorators.push.apply(decorators, propMetadata[propName]);
|
990 | }
|
991 | decorators.push.apply(decorators, ownPropMetadata[propName]);
|
992 | propMetadata[propName] = decorators;
|
993 | });
|
994 | }
|
995 | return propMetadata;
|
996 | };
|
997 | ReflectionCapabilities.prototype.hasLifecycleHook = function (type, lcProperty) {
|
998 | return type instanceof Type && lcProperty in type.prototype;
|
999 | };
|
1000 | ReflectionCapabilities.prototype.getter = function (name) {
|
1001 | return new Function('o', 'return o.' + name + ';');
|
1002 | };
|
1003 | ReflectionCapabilities.prototype.setter = function (name) {
|
1004 | return new Function('o', 'v', 'return o.' + name + ' = v;');
|
1005 | };
|
1006 | ReflectionCapabilities.prototype.method = function (name) {
|
1007 | var functionBody = "if (!o." + name + ") throw new Error('\"" + name + "\" is undefined');\n return o." + name + ".apply(o, args);";
|
1008 | return new Function('o', 'args', functionBody);
|
1009 | };
|
1010 |
|
1011 | ReflectionCapabilities.prototype.importUri = function (type) {
|
1012 |
|
1013 | if (typeof type === 'object' && type['filePath']) {
|
1014 | return type['filePath'];
|
1015 | }
|
1016 |
|
1017 | return "./" + stringify(type);
|
1018 | };
|
1019 | ReflectionCapabilities.prototype.resourceUri = function (type) {
|
1020 | return "./" + stringify(type);
|
1021 | };
|
1022 | ReflectionCapabilities.prototype.resolveIdentifier = function (name, moduleUrl, members, runtime) {
|
1023 | return runtime;
|
1024 | };
|
1025 | ReflectionCapabilities.prototype.resolveEnum = function (enumIdentifier, name) {
|
1026 | return enumIdentifier[name];
|
1027 | };
|
1028 | return ReflectionCapabilities;
|
1029 | }());
|
1030 | function convertTsickleDecoratorIntoMetadata(decoratorInvocations) {
|
1031 | if (!decoratorInvocations) {
|
1032 | return [];
|
1033 | }
|
1034 | return decoratorInvocations.map(function (decoratorInvocation) {
|
1035 | var decoratorType = decoratorInvocation.type;
|
1036 | var annotationCls = decoratorType.annotationCls;
|
1037 | var annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
|
1038 | return new (annotationCls.bind.apply(annotationCls, tslib.__spreadArrays([void 0], annotationArgs)))();
|
1039 | });
|
1040 | }
|
1041 | function getParentCtor(ctor) {
|
1042 | if (!ctor.prototype) {
|
1043 | return Object;
|
1044 | }
|
1045 | var parentProto = Object.getPrototypeOf(ctor.prototype);
|
1046 | var parentCtor = parentProto ? parentProto.constructor : null;
|
1047 |
|
1048 |
|
1049 | return parentCtor || Object;
|
1050 | }
|
1051 |
|
1052 | |
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 | |
1060 |
|
1061 |
|
1062 |
|
1063 | var ReflectorReader = (function () {
|
1064 | function ReflectorReader() {
|
1065 | }
|
1066 | return ReflectorReader;
|
1067 | }());
|
1068 |
|
1069 | |
1070 |
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 | |
1077 |
|
1078 |
|
1079 |
|
1080 | var Reflector = (function (_super) {
|
1081 | tslib.__extends(Reflector, _super);
|
1082 | function Reflector(reflectionCapabilities) {
|
1083 | var _this = _super.call(this) || this;
|
1084 | _this.reflectionCapabilities = reflectionCapabilities;
|
1085 | return _this;
|
1086 | }
|
1087 | Reflector.prototype.updateCapabilities = function (caps) {
|
1088 | this.reflectionCapabilities = caps;
|
1089 | };
|
1090 | Reflector.prototype.factory = function (type) {
|
1091 | return this.reflectionCapabilities.factory(type);
|
1092 | };
|
1093 | Reflector.prototype.parameters = function (typeOrFunc) {
|
1094 | return this.reflectionCapabilities.parameters(typeOrFunc);
|
1095 | };
|
1096 | Reflector.prototype.annotations = function (typeOrFunc) {
|
1097 | return this.reflectionCapabilities.annotations(typeOrFunc);
|
1098 | };
|
1099 | Reflector.prototype.propMetadata = function (typeOrFunc) {
|
1100 | return this.reflectionCapabilities.propMetadata(typeOrFunc);
|
1101 | };
|
1102 | Reflector.prototype.hasLifecycleHook = function (type, lcProperty) {
|
1103 | return this.reflectionCapabilities.hasLifecycleHook(type, lcProperty);
|
1104 | };
|
1105 | Reflector.prototype.getter = function (name) {
|
1106 | return this.reflectionCapabilities.getter(name);
|
1107 | };
|
1108 | Reflector.prototype.setter = function (name) {
|
1109 | return this.reflectionCapabilities.setter(name);
|
1110 | };
|
1111 | Reflector.prototype.method = function (name) {
|
1112 | return this.reflectionCapabilities.method(name);
|
1113 | };
|
1114 | Reflector.prototype.importUri = function (type) {
|
1115 | return this.reflectionCapabilities.importUri(type);
|
1116 | };
|
1117 | Reflector.prototype.resourceUri = function (type) {
|
1118 | return this.reflectionCapabilities.resourceUri(type);
|
1119 | };
|
1120 | Reflector.prototype.resolveIdentifier = function (name, moduleUrl, members, runtime) {
|
1121 | return this.reflectionCapabilities.resolveIdentifier(name, moduleUrl, members, runtime);
|
1122 | };
|
1123 | Reflector.prototype.resolveEnum = function (identifier, name) {
|
1124 | return this.reflectionCapabilities.resolveEnum(identifier, name);
|
1125 | };
|
1126 | return Reflector;
|
1127 | }(ReflectorReader));
|
1128 |
|
1129 | |
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 | |
1137 |
|
1138 |
|
1139 |
|
1140 | var reflector = new Reflector(new ReflectionCapabilities());
|
1141 |
|
1142 | |
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 | |
1150 |
|
1151 |
|
1152 |
|
1153 |
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 |
|
1172 | var OpaqueToken = (function () {
|
1173 | function OpaqueToken(_desc) {
|
1174 | this._desc = _desc;
|
1175 | }
|
1176 | OpaqueToken.prototype.toString = function () {
|
1177 | return "Token " + this._desc;
|
1178 | };
|
1179 | return OpaqueToken;
|
1180 | }());
|
1181 | |
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 | var InjectionToken = (function (_super) {
|
1204 | tslib.__extends(InjectionToken, _super);
|
1205 | function InjectionToken(desc) {
|
1206 | return _super.call(this, desc) || this;
|
1207 | }
|
1208 | InjectionToken.prototype.toString = function () {
|
1209 | return "InjectionToken " + this._desc;
|
1210 | };
|
1211 | return InjectionToken;
|
1212 | }(OpaqueToken));
|
1213 |
|
1214 | |
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 | |
1222 |
|
1223 |
|
1224 |
|
1225 | var ReflectiveDependency = (function () {
|
1226 | function ReflectiveDependency(key, optional, visibility) {
|
1227 | this.key = key;
|
1228 | this.optional = optional;
|
1229 | this.visibility = visibility;
|
1230 | }
|
1231 | ReflectiveDependency.fromKey = function (key) {
|
1232 | return new ReflectiveDependency(key, false, null);
|
1233 | };
|
1234 | return ReflectiveDependency;
|
1235 | }());
|
1236 | var _EMPTY_LIST = [];
|
1237 |
|
1238 | var ResolvedReflectiveProvider_ = (function () {
|
1239 | function ResolvedReflectiveProvider_(key, resolvedFactories, multiProvider) {
|
1240 | this.key = key;
|
1241 | this.resolvedFactories = resolvedFactories;
|
1242 | this.multiProvider = multiProvider;
|
1243 | }
|
1244 | Object.defineProperty(ResolvedReflectiveProvider_.prototype, "resolvedFactory", {
|
1245 | get: function () {
|
1246 | return this.resolvedFactories[0];
|
1247 | },
|
1248 | enumerable: false,
|
1249 | configurable: true
|
1250 | });
|
1251 | return ResolvedReflectiveProvider_;
|
1252 | }());
|
1253 | |
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 | var ResolvedReflectiveFactory = (function () {
|
1259 | function ResolvedReflectiveFactory(
|
1260 | /**
|
1261 | * Factory function which can return an instance of an object represented by a key.
|
1262 | */
|
1263 | factory,
|
1264 | /**
|
1265 | * Arguments (dependencies) to the `factory` function.
|
1266 | */
|
1267 | dependencies) {
|
1268 | this.factory = factory;
|
1269 | this.dependencies = dependencies;
|
1270 | }
|
1271 | return ResolvedReflectiveFactory;
|
1272 | }());
|
1273 | |
1274 |
|
1275 |
|
1276 | function resolveReflectiveFactory(provider) {
|
1277 | var factoryFn;
|
1278 | var resolvedDeps;
|
1279 | if (provider.useClass) {
|
1280 | var useClass = resolveForwardRef(provider.useClass);
|
1281 | factoryFn = reflector.factory(useClass);
|
1282 | resolvedDeps = _dependenciesFor(useClass);
|
1283 | }
|
1284 | else if (provider.useExisting) {
|
1285 | factoryFn = function (aliasInstance) { return aliasInstance; };
|
1286 | resolvedDeps = [ReflectiveDependency.fromKey(ReflectiveKey.get(provider.useExisting))];
|
1287 | }
|
1288 | else if (provider.useFactory) {
|
1289 | factoryFn = provider.useFactory;
|
1290 | resolvedDeps = constructDependencies(provider.useFactory, provider.deps);
|
1291 | }
|
1292 | else {
|
1293 | factoryFn = function () { return provider.useValue; };
|
1294 | resolvedDeps = _EMPTY_LIST;
|
1295 | }
|
1296 | return new ResolvedReflectiveFactory(factoryFn, resolvedDeps);
|
1297 | }
|
1298 | |
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 | function resolveReflectiveProvider(provider) {
|
1305 | return new ResolvedReflectiveProvider_(ReflectiveKey.get(provider.provide), [resolveReflectiveFactory(provider)], provider.multi || false);
|
1306 | }
|
1307 | |
1308 |
|
1309 |
|
1310 | function resolveReflectiveProviders(providers) {
|
1311 | var normalized = _normalizeProviders(providers, []);
|
1312 | var resolved = normalized.map(resolveReflectiveProvider);
|
1313 | var resolvedProviderMap = mergeResolvedReflectiveProviders(resolved, new Map());
|
1314 | return Array.from(resolvedProviderMap.values());
|
1315 | }
|
1316 | |
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 | function mergeResolvedReflectiveProviders(providers, normalizedProvidersMap) {
|
1322 | for (var i = 0; i < providers.length; i++) {
|
1323 | var provider = providers[i];
|
1324 | var existing = normalizedProvidersMap.get(provider.key.id);
|
1325 | if (existing) {
|
1326 | if (provider.multiProvider !== existing.multiProvider) {
|
1327 | throw mixingMultiProvidersWithRegularProvidersError(existing, provider);
|
1328 | }
|
1329 | if (provider.multiProvider) {
|
1330 | for (var j = 0; j < provider.resolvedFactories.length; j++) {
|
1331 | existing.resolvedFactories.push(provider.resolvedFactories[j]);
|
1332 | }
|
1333 | }
|
1334 | else {
|
1335 | normalizedProvidersMap.set(provider.key.id, provider);
|
1336 | }
|
1337 | }
|
1338 | else {
|
1339 | var resolvedProvider = void 0;
|
1340 | if (provider.multiProvider) {
|
1341 | resolvedProvider = new ResolvedReflectiveProvider_(provider.key, provider.resolvedFactories.slice(), provider.multiProvider);
|
1342 | }
|
1343 | else {
|
1344 | resolvedProvider = provider;
|
1345 | }
|
1346 | normalizedProvidersMap.set(provider.key.id, resolvedProvider);
|
1347 | }
|
1348 | }
|
1349 | return normalizedProvidersMap;
|
1350 | }
|
1351 | function _normalizeProviders(providers, res) {
|
1352 | providers.forEach(function (b) {
|
1353 | if (b instanceof Type) {
|
1354 | res.push({ provide: b, useClass: b });
|
1355 | }
|
1356 | else if (b && typeof b === 'object' && b.provide !== undefined) {
|
1357 | res.push(b);
|
1358 | }
|
1359 | else if (b instanceof Array) {
|
1360 | _normalizeProviders(b, res);
|
1361 | }
|
1362 | else {
|
1363 | throw invalidProviderError(b);
|
1364 | }
|
1365 | });
|
1366 | return res;
|
1367 | }
|
1368 | function constructDependencies(typeOrFunc, dependencies) {
|
1369 | if (!dependencies) {
|
1370 | return _dependenciesFor(typeOrFunc);
|
1371 | }
|
1372 | else {
|
1373 | var params_1 = dependencies.map(function (t) { return [t]; });
|
1374 | return dependencies.map(function (t) { return _extractToken(typeOrFunc, t, params_1); });
|
1375 | }
|
1376 | }
|
1377 | function _dependenciesFor(typeOrFunc) {
|
1378 | var params = reflector.parameters(typeOrFunc);
|
1379 | if (!params)
|
1380 | return [];
|
1381 | if (params.some(function (p) { return p == null; })) {
|
1382 | throw noAnnotationError(typeOrFunc, params);
|
1383 | }
|
1384 | return params.map(function (p) { return _extractToken(typeOrFunc, p, params); });
|
1385 | }
|
1386 | function _extractToken(typeOrFunc, metadata, params) {
|
1387 | var token = null;
|
1388 | var optional = false;
|
1389 | if (!Array.isArray(metadata)) {
|
1390 | if (metadata instanceof Inject) {
|
1391 | return _createDependency(metadata['token'], optional, null);
|
1392 | }
|
1393 | else {
|
1394 | return _createDependency(metadata, optional, null);
|
1395 | }
|
1396 | }
|
1397 | var visibility = null;
|
1398 | for (var i = 0; i < metadata.length; ++i) {
|
1399 | var paramMetadata = metadata[i];
|
1400 | if (paramMetadata instanceof Type) {
|
1401 | token = paramMetadata;
|
1402 | }
|
1403 | else if (paramMetadata instanceof Inject) {
|
1404 | token = paramMetadata['token'];
|
1405 | }
|
1406 | else if (paramMetadata instanceof Optional) {
|
1407 | optional = true;
|
1408 | }
|
1409 | else if (paramMetadata instanceof Self || paramMetadata instanceof SkipSelf) {
|
1410 | visibility = paramMetadata;
|
1411 | }
|
1412 | else if (paramMetadata instanceof InjectionToken) {
|
1413 | token = paramMetadata;
|
1414 | }
|
1415 | }
|
1416 | token = resolveForwardRef(token);
|
1417 | if (token != null) {
|
1418 | return _createDependency(token, optional, visibility);
|
1419 | }
|
1420 | else {
|
1421 | throw noAnnotationError(typeOrFunc, params);
|
1422 | }
|
1423 | }
|
1424 | function _createDependency(token, optional, visibility) {
|
1425 | return new ReflectiveDependency(ReflectiveKey.get(token), optional, visibility);
|
1426 | }
|
1427 |
|
1428 | |
1429 |
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 |
|
1436 | var UNDEFINED = new Object();
|
1437 | |
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 |
|
1445 |
|
1446 |
|
1447 |
|
1448 |
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 |
|
1457 |
|
1458 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 | var ReflectiveInjector = (function () {
|
1473 | function ReflectiveInjector() {
|
1474 | }
|
1475 | |
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 | ReflectiveInjector.resolve = function (providers) {
|
1509 | return resolveReflectiveProviders(providers);
|
1510 | };
|
1511 | |
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 |
|
1521 |
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 | ReflectiveInjector.resolveAndCreate = function (providers, parent) {
|
1538 | var ResolvedReflectiveProviders = ReflectiveInjector.resolve(providers);
|
1539 | return ReflectiveInjector.fromResolvedProviders(ResolvedReflectiveProviders, parent);
|
1540 | };
|
1541 | |
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 |
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 | ReflectiveInjector.fromResolvedProviders = function (providers, parent) {
|
1565 |
|
1566 | return new ReflectiveInjector_(providers, parent);
|
1567 | };
|
1568 | return ReflectiveInjector;
|
1569 | }());
|
1570 |
|
1571 | var ReflectiveInjector_ = (function () {
|
1572 | |
1573 |
|
1574 |
|
1575 | function ReflectiveInjector_(_providers, _parent) {
|
1576 |
|
1577 | this._constructionCounter = 0;
|
1578 | this._providers = _providers;
|
1579 | this._parent = _parent || null;
|
1580 | var len = _providers.length;
|
1581 | this.keyIds = new Array(len);
|
1582 | this.objs = new Array(len);
|
1583 | for (var i = 0; i < len; i++) {
|
1584 | this.keyIds[i] = _providers[i].key.id;
|
1585 | this.objs[i] = UNDEFINED;
|
1586 | }
|
1587 | }
|
1588 | ReflectiveInjector_.prototype.get = function (token, notFoundValue) {
|
1589 | if (notFoundValue === void 0) { notFoundValue = THROW_IF_NOT_FOUND; }
|
1590 | return this._getByKey(ReflectiveKey.get(token), null, notFoundValue);
|
1591 | };
|
1592 | Object.defineProperty(ReflectiveInjector_.prototype, "parent", {
|
1593 | get: function () {
|
1594 | return this._parent;
|
1595 | },
|
1596 | enumerable: false,
|
1597 | configurable: true
|
1598 | });
|
1599 | ReflectiveInjector_.prototype.resolveAndCreateChild = function (providers) {
|
1600 | var ResolvedReflectiveProviders = ReflectiveInjector.resolve(providers);
|
1601 | return this.createChildFromResolved(ResolvedReflectiveProviders);
|
1602 | };
|
1603 | ReflectiveInjector_.prototype.createChildFromResolved = function (providers) {
|
1604 | var inj = new ReflectiveInjector_(providers);
|
1605 | inj._parent = this;
|
1606 | return inj;
|
1607 | };
|
1608 | ReflectiveInjector_.prototype.resolveAndInstantiate = function (provider) {
|
1609 | return this.instantiateResolved(ReflectiveInjector.resolve([provider])[0]);
|
1610 | };
|
1611 | ReflectiveInjector_.prototype.instantiateResolved = function (provider) {
|
1612 | return this._instantiateProvider(provider);
|
1613 | };
|
1614 | ReflectiveInjector_.prototype.getProviderAtIndex = function (index) {
|
1615 | if (index < 0 || index >= this._providers.length) {
|
1616 | throw outOfBoundsError(index);
|
1617 | }
|
1618 | return this._providers[index];
|
1619 | };
|
1620 |
|
1621 | ReflectiveInjector_.prototype._new = function (provider) {
|
1622 | if (this._constructionCounter++ > this._getMaxNumberOfObjects()) {
|
1623 | throw cyclicDependencyError(this, provider.key);
|
1624 | }
|
1625 | return this._instantiateProvider(provider);
|
1626 | };
|
1627 | ReflectiveInjector_.prototype._getMaxNumberOfObjects = function () {
|
1628 | return this.objs.length;
|
1629 | };
|
1630 | ReflectiveInjector_.prototype._instantiateProvider = function (provider) {
|
1631 | if (provider.multiProvider) {
|
1632 | var res = new Array(provider.resolvedFactories.length);
|
1633 | for (var i = 0; i < provider.resolvedFactories.length; ++i) {
|
1634 | res[i] = this._instantiate(provider, provider.resolvedFactories[i]);
|
1635 | }
|
1636 | return res;
|
1637 | }
|
1638 | else {
|
1639 | return this._instantiate(provider, provider.resolvedFactories[0]);
|
1640 | }
|
1641 | };
|
1642 | ReflectiveInjector_.prototype._instantiate = function (provider, ResolvedReflectiveFactory) {
|
1643 | var _this = this;
|
1644 | var factory = ResolvedReflectiveFactory.factory;
|
1645 | var deps;
|
1646 | try {
|
1647 | deps = ResolvedReflectiveFactory.dependencies.map(function (dep) { return _this._getByReflectiveDependency(dep); });
|
1648 | }
|
1649 | catch (e) {
|
1650 | if (e.addKey) {
|
1651 | e.addKey(this, provider.key);
|
1652 | }
|
1653 | throw e;
|
1654 | }
|
1655 | var obj;
|
1656 | try {
|
1657 | obj = factory.apply(void 0, deps);
|
1658 | }
|
1659 | catch (e) {
|
1660 | throw instantiationError(this, e, e.stack, provider.key);
|
1661 | }
|
1662 | return obj;
|
1663 | };
|
1664 | ReflectiveInjector_.prototype._getByReflectiveDependency = function (dep) {
|
1665 | return this._getByKey(dep.key, dep.visibility, dep.optional ? null : THROW_IF_NOT_FOUND);
|
1666 | };
|
1667 | ReflectiveInjector_.prototype._getByKey = function (key, visibility, notFoundValue) {
|
1668 |
|
1669 | if (key === INJECTOR_KEY) {
|
1670 | return this;
|
1671 | }
|
1672 | if (visibility instanceof Self) {
|
1673 | return this._getByKeySelf(key, notFoundValue);
|
1674 | }
|
1675 | else {
|
1676 | return this._getByKeyDefault(key, notFoundValue, visibility);
|
1677 | }
|
1678 | };
|
1679 | ReflectiveInjector_.prototype._getObjByKeyId = function (keyId) {
|
1680 | for (var i = 0; i < this.keyIds.length; i++) {
|
1681 | if (this.keyIds[i] === keyId) {
|
1682 | if (this.objs[i] === UNDEFINED) {
|
1683 | this.objs[i] = this._new(this._providers[i]);
|
1684 | }
|
1685 | return this.objs[i];
|
1686 | }
|
1687 | }
|
1688 | return UNDEFINED;
|
1689 | };
|
1690 |
|
1691 | ReflectiveInjector_.prototype._throwOrNull = function (key, notFoundValue) {
|
1692 | if (notFoundValue !== THROW_IF_NOT_FOUND) {
|
1693 | return notFoundValue;
|
1694 | }
|
1695 | else {
|
1696 | throw noProviderError(this, key);
|
1697 | }
|
1698 | };
|
1699 |
|
1700 | ReflectiveInjector_.prototype._getByKeySelf = function (key, notFoundValue) {
|
1701 | var obj = this._getObjByKeyId(key.id);
|
1702 | return obj !== UNDEFINED ? obj : this._throwOrNull(key, notFoundValue);
|
1703 | };
|
1704 |
|
1705 | ReflectiveInjector_.prototype._getByKeyDefault = function (key, notFoundValue, visibility) {
|
1706 | var inj;
|
1707 | if (visibility instanceof SkipSelf) {
|
1708 | inj = this._parent;
|
1709 | }
|
1710 | else {
|
1711 | inj = this;
|
1712 | }
|
1713 | while (inj instanceof ReflectiveInjector_) {
|
1714 | var inj_ = inj;
|
1715 | var obj = inj_._getObjByKeyId(key.id);
|
1716 | if (obj !== UNDEFINED)
|
1717 | return obj;
|
1718 | inj = inj_._parent;
|
1719 | }
|
1720 | if (inj !== null) {
|
1721 | return inj.get(key.token, notFoundValue);
|
1722 | }
|
1723 | else {
|
1724 | return this._throwOrNull(key, notFoundValue);
|
1725 | }
|
1726 | };
|
1727 | Object.defineProperty(ReflectiveInjector_.prototype, "displayName", {
|
1728 | get: function () {
|
1729 | var providers = _mapProviders(this, function (b) { return ' "' + b.key.displayName + '" '; }).join(', ');
|
1730 | return "ReflectiveInjector(providers: [" + providers + "])";
|
1731 | },
|
1732 | enumerable: false,
|
1733 | configurable: true
|
1734 | });
|
1735 | ReflectiveInjector_.prototype.toString = function () {
|
1736 | return this.displayName;
|
1737 | };
|
1738 | return ReflectiveInjector_;
|
1739 | }());
|
1740 | var INJECTOR_KEY = ReflectiveKey.get(Injector);
|
1741 | function _mapProviders(injector, fn) {
|
1742 | var res = new Array(injector._providers.length);
|
1743 | for (var i = 0; i < injector._providers.length; ++i) {
|
1744 | res[i] = fn(injector.getProviderAtIndex(i));
|
1745 | }
|
1746 | return res;
|
1747 | }
|
1748 |
|
1749 | |
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 |
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 |
|
1784 |
|
1785 |
|
1786 |
|
1787 |
|
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 |
|
1793 | function resolveDependencies() {
|
1794 | var inputs = [];
|
1795 | for (var _i = 0; _i < arguments.length; _i++) {
|
1796 | inputs[_i] = arguments[_i];
|
1797 | }
|
1798 | var deps = new Set();
|
1799 | function resolver(klass) {
|
1800 | if (deps.has(klass))
|
1801 | return;
|
1802 | deps.add(klass);
|
1803 |
|
1804 |
|
1805 | ReflectiveInjector.resolve([klass])
|
1806 | .reduce(function (a, x) { return a.concat(x.resolvedFactories); }, [])
|
1807 | .reduce(function (a, r) { return a.concat(r.dependencies); }, [])
|
1808 | .forEach(function (d) { return resolver(d.key.token); });
|
1809 | }
|
1810 | for (var _a = 0, inputs_1 = inputs; _a < inputs_1.length; _a++) {
|
1811 | var input = inputs_1[_a];
|
1812 | resolver(input);
|
1813 | }
|
1814 | return Array.from(deps);
|
1815 | }
|
1816 |
|
1817 | exports.Class = Class;
|
1818 | exports.Host = Host;
|
1819 | exports.Inject = Inject;
|
1820 | exports.Injectable = Injectable;
|
1821 | exports.InjectionToken = InjectionToken;
|
1822 | exports.Injector = Injector;
|
1823 | exports.OpaqueToken = OpaqueToken;
|
1824 | exports.Optional = Optional;
|
1825 | exports.ReflectiveInjector = ReflectiveInjector;
|
1826 | exports.ReflectiveKey = ReflectiveKey;
|
1827 | exports.ResolvedReflectiveFactory = ResolvedReflectiveFactory;
|
1828 | exports.Self = Self;
|
1829 | exports.SkipSelf = SkipSelf;
|
1830 | exports.Type = Type;
|
1831 | exports.forwardRef = forwardRef;
|
1832 | exports.isType = isType;
|
1833 | exports.makeDecorator = makeDecorator;
|
1834 | exports.makeParamDecorator = makeParamDecorator;
|
1835 | exports.makePropDecorator = makePropDecorator;
|
1836 | exports.resolveDependencies = resolveDependencies;
|
1837 | exports.resolveForwardRef = resolveForwardRef;
|
1838 |
|
1839 | Object.defineProperty(exports, '__esModule', { value: true });
|
1840 |
|
1841 | })));
|