123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- var __extends = (this && this.__extends) || (function () {
- var extendStatics = function (d, b) {
- extendStatics = Object.setPrototypeOf ||
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
- return extendStatics(d, b);
- };
- return function (d, b) {
- if (typeof b !== "function" && b !== null)
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
- extendStatics(d, b);
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- })();
- import { isIdentifierDecorator, } from './dependencyIdentifier';
- import { prettyPrintIdentifier } from './dependencyItem';
- import { checkQuantity, retrieveQuantity } from './dependencyQuantity';
- import { isDisposable } from './dispose';
- import { Quantity } from './types';
- import { RediError } from './error';
- export function isBareClassDependency(thing) {
- return thing.length === 1;
- }
- var ResolvingStack = [];
- export function pushResolvingStack(id) {
- ResolvingStack.push(id);
- }
- export function popupResolvingStack() {
- ResolvingStack.pop();
- }
- export function clearResolvingStack() {
- ResolvingStack.length = 0;
- }
- var DependencyNotFoundForModuleError = /** @class */ (function (_super) {
- __extends(DependencyNotFoundForModuleError, _super);
- function DependencyNotFoundForModuleError(toInstantiate, id, index) {
- var _this = this;
- var msg = "Cannot find \"".concat(prettyPrintIdentifier(id), "\" registered by any injector. It is the ").concat(index, "th param of \"").concat(isIdentifierDecorator(toInstantiate)
- ? prettyPrintIdentifier(toInstantiate)
- : toInstantiate.name, "\". The stack of dependencies is: \"").concat(ResolvingStack.map(function (id) { return prettyPrintIdentifier(id); }).join(' -> '), "\".");
- _this = _super.call(this, msg) || this;
- clearResolvingStack();
- return _this;
- }
- return DependencyNotFoundForModuleError;
- }(RediError));
- export { DependencyNotFoundForModuleError };
- var DependencyNotFoundError = /** @class */ (function (_super) {
- __extends(DependencyNotFoundError, _super);
- function DependencyNotFoundError(id) {
- 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(' -> '), "\".");
- return _super.call(this, msg) || this;
- }
- return DependencyNotFoundError;
- }(RediError));
- export { DependencyNotFoundError };
- /**
- * Store unresolved dependencies in an injector.
- *
- * @internal
- */
- var DependencyCollection = /** @class */ (function () {
- function DependencyCollection(dependencies) {
- var _this = this;
- this.dependencyMap = new Map();
- this.normalizeDependencies(dependencies).map(function (pair) {
- return _this.add(pair[0], pair[1]);
- });
- }
- DependencyCollection.prototype.add = function (ctorOrId, val) {
- if (typeof val === 'undefined') {
- val = { useClass: ctorOrId, lazy: false };
- }
- var arr = this.dependencyMap.get(ctorOrId);
- if (typeof arr === 'undefined') {
- arr = [];
- this.dependencyMap.set(ctorOrId, arr);
- }
- arr.push(val);
- };
- DependencyCollection.prototype.delete = function (id) {
- this.dependencyMap.delete(id);
- };
- DependencyCollection.prototype.get = function (id, quantity) {
- if (quantity === void 0) { quantity = Quantity.REQUIRED; }
- var ret = this.dependencyMap.get(id);
- checkQuantity(id, quantity, ret.length);
- return retrieveQuantity(quantity, ret);
- };
- DependencyCollection.prototype.has = function (id) {
- return this.dependencyMap.has(id);
- };
- DependencyCollection.prototype.append = function (dependencies) {
- var _this = this;
- this.normalizeDependencies(dependencies).forEach(function (pair) {
- return _this.add(pair[0], pair[1]);
- });
- };
- DependencyCollection.prototype.dispose = function () {
- this.dependencyMap.clear();
- };
- /**
- * normalize dependencies to `DependencyItem`
- */
- DependencyCollection.prototype.normalizeDependencies = function (dependencies) {
- return dependencies.map(function (dependency) {
- var id = dependency[0];
- var val;
- if (isBareClassDependency(dependency)) {
- val = {
- useClass: dependency[0],
- lazy: false,
- };
- }
- else {
- val = dependency[1];
- }
- return [id, val];
- });
- };
- return DependencyCollection;
- }());
- export { DependencyCollection };
- /**
- * Store resolved dependencies.
- *
- * @internal
- */
- var ResolvedDependencyCollection = /** @class */ (function () {
- function ResolvedDependencyCollection() {
- this.resolvedDependencies = new Map();
- }
- ResolvedDependencyCollection.prototype.add = function (id, val) {
- var arr = this.resolvedDependencies.get(id);
- if (typeof arr === 'undefined') {
- arr = [];
- this.resolvedDependencies.set(id, arr);
- }
- arr.push(val);
- };
- ResolvedDependencyCollection.prototype.has = function (id) {
- return this.resolvedDependencies.has(id);
- };
- ResolvedDependencyCollection.prototype.delete = function (id) {
- if (this.resolvedDependencies.has(id)) {
- var things = this.resolvedDependencies.get(id);
- things.forEach(function (t) { return (isDisposable(t) ? t.dispose() : void 0); });
- this.resolvedDependencies.delete(id);
- }
- };
- ResolvedDependencyCollection.prototype.get = function (id, quantity) {
- if (quantity === void 0) { quantity = Quantity.REQUIRED; }
- var ret = this.resolvedDependencies.get(id);
- if (!ret) {
- throw new DependencyNotFoundError(id);
- }
- checkQuantity(id, quantity, ret.length);
- if (quantity === Quantity.MANY) {
- return ret;
- }
- else {
- return ret[0];
- }
- };
- ResolvedDependencyCollection.prototype.dispose = function () {
- Array.from(this.resolvedDependencies.values()).forEach(function (items) {
- items.forEach(function (item) { return (isDisposable(item) ? item.dispose() : void 0); });
- });
- this.resolvedDependencies.clear();
- };
- return ResolvedDependencyCollection;
- }());
- export { ResolvedDependencyCollection };
- //# sourceMappingURL=dependencyCollection.js.map
|