decorators.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { IdentifierDecoratorSymbol, } from './dependencyIdentifier';
  17. import { prettyPrintIdentifier } from './dependencyItem';
  18. import { Quantity } from './types';
  19. import { RediError } from './error';
  20. export var TARGET = Symbol('$$TARGET');
  21. export var DEPENDENCIES = Symbol('$$DEPENDENCIES');
  22. var DependencyDescriptorNotFoundError = /** @class */ (function (_super) {
  23. __extends(DependencyDescriptorNotFoundError, _super);
  24. function DependencyDescriptorNotFoundError(index, target) {
  25. var msg = "Could not find dependency registered on the ".concat(index, " (indexed) parameter of the constructor of \"").concat(prettyPrintIdentifier(target), "\".");
  26. return _super.call(this, msg) || this;
  27. }
  28. return DependencyDescriptorNotFoundError;
  29. }(RediError));
  30. var IdentifierUndefinedError = /** @class */ (function (_super) {
  31. __extends(IdentifierUndefinedError, _super);
  32. function IdentifierUndefinedError(target, index) {
  33. 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");
  34. return _super.call(this, msg) || this;
  35. }
  36. return IdentifierUndefinedError;
  37. }(RediError));
  38. export { IdentifierUndefinedError };
  39. /**
  40. * @internal
  41. */
  42. export function getDependencies(registerTarget) {
  43. var target = registerTarget;
  44. return target[DEPENDENCIES] || [];
  45. }
  46. /**
  47. * @internal
  48. */
  49. export function getDependencyByIndex(registerTarget, index) {
  50. var allDependencies = getDependencies(registerTarget);
  51. var dep = allDependencies.find(function (descriptor) { return descriptor.paramIndex === index; });
  52. if (!dep) {
  53. throw new DependencyDescriptorNotFoundError(index, registerTarget);
  54. }
  55. return dep;
  56. }
  57. /**
  58. * @internal
  59. */
  60. export function setDependency(registerTarget, identifier, paramIndex, quantity, lookUp) {
  61. if (quantity === void 0) { quantity = Quantity.REQUIRED; }
  62. var descriptor = {
  63. paramIndex: paramIndex,
  64. identifier: identifier,
  65. quantity: quantity,
  66. lookUp: lookUp,
  67. withNew: false,
  68. };
  69. // sometimes identifier could be 'undefined' if user meant to pass in an ES class
  70. // this is related to how classes are transpiled
  71. if (typeof identifier === 'undefined') {
  72. throw new IdentifierUndefinedError(registerTarget, paramIndex);
  73. }
  74. var target = registerTarget;
  75. // deal with inheritance, subclass need to declare dependencies on its on
  76. if (target[TARGET] === target) {
  77. target[DEPENDENCIES].push(descriptor);
  78. }
  79. else {
  80. target[DEPENDENCIES] = [descriptor];
  81. target[TARGET] = target;
  82. }
  83. }
  84. var knownIdentifiers = new Set();
  85. /**
  86. * Create a dependency identifier
  87. *
  88. * @param id name of the identifier
  89. * @returns Identifier that could also be used as a decorator
  90. */
  91. export function createIdentifier(id) {
  92. if (knownIdentifiers.has(id)) {
  93. throw new RediError("Identifier \"".concat(id, "\" already exists."));
  94. }
  95. else {
  96. knownIdentifiers.add(id);
  97. }
  98. var decorator = (function (registerTarget, _key, index) {
  99. setDependency(registerTarget, decorator, index);
  100. }); // decorator as an identifier
  101. // TODO: @wzhudev should assign a name to the function so it would be easy to debug in inspect tools
  102. // decorator.name = `[redi]: ${id}`;
  103. decorator.toString = function () { return id; };
  104. decorator[IdentifierDecoratorSymbol] = true;
  105. return decorator;
  106. }
  107. /**
  108. * @internal
  109. */
  110. /* istanbul ignore next */
  111. export function TEST_ONLY_clearKnownIdentifiers() {
  112. knownIdentifiers.clear();
  113. }
  114. //# sourceMappingURL=decorators.js.map