123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- 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 { IdentifierDecoratorSymbol, } from './dependencyIdentifier';
- import { prettyPrintIdentifier } from './dependencyItem';
- import { Quantity } from './types';
- import { RediError } from './error';
- export var TARGET = Symbol('$$TARGET');
- export var DEPENDENCIES = Symbol('$$DEPENDENCIES');
- var DependencyDescriptorNotFoundError = /** @class */ (function (_super) {
- __extends(DependencyDescriptorNotFoundError, _super);
- function DependencyDescriptorNotFoundError(index, target) {
- var msg = "Could not find dependency registered on the ".concat(index, " (indexed) parameter of the constructor of \"").concat(prettyPrintIdentifier(target), "\".");
- return _super.call(this, msg) || this;
- }
- return DependencyDescriptorNotFoundError;
- }(RediError));
- var IdentifierUndefinedError = /** @class */ (function (_super) {
- __extends(IdentifierUndefinedError, _super);
- function IdentifierUndefinedError(target, index) {
- var msg = "It seems that you register \"undefined\" as dependency on the ".concat(index + 1, " parameter of \"").concat(prettyPrintIdentifier(target), "\". Please make sure that there is not cyclic dependency among your TypeScript files, or consider using \"forwardRef\". For more info please visit our website https://redi.wendell.fun/docs/debug#could-not-find-dependency-registered-on");
- return _super.call(this, msg) || this;
- }
- return IdentifierUndefinedError;
- }(RediError));
- export { IdentifierUndefinedError };
- /**
- * @internal
- */
- export function getDependencies(registerTarget) {
- var target = registerTarget;
- return target[DEPENDENCIES] || [];
- }
- /**
- * @internal
- */
- export function getDependencyByIndex(registerTarget, index) {
- var allDependencies = getDependencies(registerTarget);
- var dep = allDependencies.find(function (descriptor) { return descriptor.paramIndex === index; });
- if (!dep) {
- throw new DependencyDescriptorNotFoundError(index, registerTarget);
- }
- return dep;
- }
- /**
- * @internal
- */
- export function setDependency(registerTarget, identifier, paramIndex, quantity, lookUp) {
- if (quantity === void 0) { quantity = Quantity.REQUIRED; }
- var descriptor = {
- paramIndex: paramIndex,
- identifier: identifier,
- quantity: quantity,
- lookUp: lookUp,
- withNew: false,
- };
- // sometimes identifier could be 'undefined' if user meant to pass in an ES class
- // this is related to how classes are transpiled
- if (typeof identifier === 'undefined') {
- throw new IdentifierUndefinedError(registerTarget, paramIndex);
- }
- var target = registerTarget;
- // deal with inheritance, subclass need to declare dependencies on its on
- if (target[TARGET] === target) {
- target[DEPENDENCIES].push(descriptor);
- }
- else {
- target[DEPENDENCIES] = [descriptor];
- target[TARGET] = target;
- }
- }
- var knownIdentifiers = new Set();
- /**
- * Create a dependency identifier
- *
- * @param id name of the identifier
- * @returns Identifier that could also be used as a decorator
- */
- export function createIdentifier(id) {
- if (knownIdentifiers.has(id)) {
- throw new RediError("Identifier \"".concat(id, "\" already exists."));
- }
- else {
- knownIdentifiers.add(id);
- }
- var decorator = (function (registerTarget, _key, index) {
- setDependency(registerTarget, decorator, index);
- }); // decorator as an identifier
- // TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
- // decorator.name = `[redi]: ${id}`;
- decorator.toString = function () { return id; };
- decorator[IdentifierDecoratorSymbol] = true;
- return decorator;
- }
- /**
- * @internal
- */
- /* istanbul ignore next */
- export function TEST_ONLY_clearKnownIdentifiers() {
- knownIdentifiers.clear();
- }
- //# sourceMappingURL=decorators.js.map
|