dependencyCollection.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. if (typeof b !== "function" && b !== null)
  10. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  11. extendStatics(d, b);
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();
  16. import { isIdentifierDecorator, } from './dependencyIdentifier';
  17. import { prettyPrintIdentifier } from './dependencyItem';
  18. import { checkQuantity, retrieveQuantity } from './dependencyQuantity';
  19. import { isDisposable } from './dispose';
  20. import { Quantity } from './types';
  21. import { RediError } from './error';
  22. export function isBareClassDependency(thing) {
  23. return thing.length === 1;
  24. }
  25. var ResolvingStack = [];
  26. export function pushResolvingStack(id) {
  27. ResolvingStack.push(id);
  28. }
  29. export function popupResolvingStack() {
  30. ResolvingStack.pop();
  31. }
  32. export function clearResolvingStack() {
  33. ResolvingStack.length = 0;
  34. }
  35. var DependencyNotFoundForModuleError = /** @class */ (function (_super) {
  36. __extends(DependencyNotFoundForModuleError, _super);
  37. function DependencyNotFoundForModuleError(toInstantiate, id, index) {
  38. var _this = this;
  39. var msg = "Cannot find \"".concat(prettyPrintIdentifier(id), "\" registered by any injector. It is the ").concat(index, "th param of \"").concat(isIdentifierDecorator(toInstantiate)
  40. ? prettyPrintIdentifier(toInstantiate)
  41. : toInstantiate.name, "\". The stack of dependencies is: \"").concat(ResolvingStack.map(function (id) { return prettyPrintIdentifier(id); }).join(' -> '), "\".");
  42. _this = _super.call(this, msg) || this;
  43. clearResolvingStack();
  44. return _this;
  45. }
  46. return DependencyNotFoundForModuleError;
  47. }(RediError));
  48. export { DependencyNotFoundForModuleError };
  49. var DependencyNotFoundError = /** @class */ (function (_super) {
  50. __extends(DependencyNotFoundError, _super);
  51. function DependencyNotFoundError(id) {
  52. var msg = "Cannot find \"".concat(prettyPrintIdentifier(id), "\" registered by any injector. The stack of dependencies is: \"").concat(ResolvingStack.map(function (id) { return prettyPrintIdentifier(id); }).join(' -> '), "\".");
  53. return _super.call(this, msg) || this;
  54. }
  55. return DependencyNotFoundError;
  56. }(RediError));
  57. export { DependencyNotFoundError };
  58. /**
  59. * Store unresolved dependencies in an injector.
  60. *
  61. * @internal
  62. */
  63. var DependencyCollection = /** @class */ (function () {
  64. function DependencyCollection(dependencies) {
  65. var _this = this;
  66. this.dependencyMap = new Map();
  67. this.normalizeDependencies(dependencies).map(function (pair) {
  68. return _this.add(pair[0], pair[1]);
  69. });
  70. }
  71. DependencyCollection.prototype.add = function (ctorOrId, val) {
  72. if (typeof val === 'undefined') {
  73. val = { useClass: ctorOrId, lazy: false };
  74. }
  75. var arr = this.dependencyMap.get(ctorOrId);
  76. if (typeof arr === 'undefined') {
  77. arr = [];
  78. this.dependencyMap.set(ctorOrId, arr);
  79. }
  80. arr.push(val);
  81. };
  82. DependencyCollection.prototype.delete = function (id) {
  83. this.dependencyMap.delete(id);
  84. };
  85. DependencyCollection.prototype.get = function (id, quantity) {
  86. if (quantity === void 0) { quantity = Quantity.REQUIRED; }
  87. var ret = this.dependencyMap.get(id);
  88. checkQuantity(id, quantity, ret.length);
  89. return retrieveQuantity(quantity, ret);
  90. };
  91. DependencyCollection.prototype.has = function (id) {
  92. return this.dependencyMap.has(id);
  93. };
  94. DependencyCollection.prototype.append = function (dependencies) {
  95. var _this = this;
  96. this.normalizeDependencies(dependencies).forEach(function (pair) {
  97. return _this.add(pair[0], pair[1]);
  98. });
  99. };
  100. DependencyCollection.prototype.dispose = function () {
  101. this.dependencyMap.clear();
  102. };
  103. /**
  104. * normalize dependencies to `DependencyItem`
  105. */
  106. DependencyCollection.prototype.normalizeDependencies = function (dependencies) {
  107. return dependencies.map(function (dependency) {
  108. var id = dependency[0];
  109. var val;
  110. if (isBareClassDependency(dependency)) {
  111. val = {
  112. useClass: dependency[0],
  113. lazy: false,
  114. };
  115. }
  116. else {
  117. val = dependency[1];
  118. }
  119. return [id, val];
  120. });
  121. };
  122. return DependencyCollection;
  123. }());
  124. export { DependencyCollection };
  125. /**
  126. * Store resolved dependencies.
  127. *
  128. * @internal
  129. */
  130. var ResolvedDependencyCollection = /** @class */ (function () {
  131. function ResolvedDependencyCollection() {
  132. this.resolvedDependencies = new Map();
  133. }
  134. ResolvedDependencyCollection.prototype.add = function (id, val) {
  135. var arr = this.resolvedDependencies.get(id);
  136. if (typeof arr === 'undefined') {
  137. arr = [];
  138. this.resolvedDependencies.set(id, arr);
  139. }
  140. arr.push(val);
  141. };
  142. ResolvedDependencyCollection.prototype.has = function (id) {
  143. return this.resolvedDependencies.has(id);
  144. };
  145. ResolvedDependencyCollection.prototype.delete = function (id) {
  146. if (this.resolvedDependencies.has(id)) {
  147. var things = this.resolvedDependencies.get(id);
  148. things.forEach(function (t) { return (isDisposable(t) ? t.dispose() : void 0); });
  149. this.resolvedDependencies.delete(id);
  150. }
  151. };
  152. ResolvedDependencyCollection.prototype.get = function (id, quantity) {
  153. if (quantity === void 0) { quantity = Quantity.REQUIRED; }
  154. var ret = this.resolvedDependencies.get(id);
  155. if (!ret) {
  156. throw new DependencyNotFoundError(id);
  157. }
  158. checkQuantity(id, quantity, ret.length);
  159. if (quantity === Quantity.MANY) {
  160. return ret;
  161. }
  162. else {
  163. return ret[0];
  164. }
  165. };
  166. ResolvedDependencyCollection.prototype.dispose = function () {
  167. Array.from(this.resolvedDependencies.values()).forEach(function (items) {
  168. items.forEach(function (item) { return (isDisposable(item) ? item.dispose() : void 0); });
  169. });
  170. this.resolvedDependencies.clear();
  171. };
  172. return ResolvedDependencyCollection;
  173. }());
  174. export { ResolvedDependencyCollection };
  175. //# sourceMappingURL=dependencyCollection.js.map