dependencyQuantity.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 { getDependencyByIndex, IdentifierUndefinedError, setDependency, } from './decorators';
  17. import { prettyPrintIdentifier } from './dependencyItem';
  18. import { RediError } from './error';
  19. import { Quantity } from './types';
  20. var QuantityCheckError = /** @class */ (function (_super) {
  21. __extends(QuantityCheckError, _super);
  22. function QuantityCheckError(id, quantity, actual) {
  23. var msg = "Expect \"".concat(quantity, "\" dependency items for id \"").concat(prettyPrintIdentifier(id), "\" but get ").concat(actual, ".");
  24. return _super.call(this, msg) || this;
  25. }
  26. return QuantityCheckError;
  27. }(RediError));
  28. export function checkQuantity(id, quantity, length) {
  29. if ((quantity === Quantity.OPTIONAL && length > 1) ||
  30. (quantity === Quantity.REQUIRED && length !== 1)) {
  31. throw new QuantityCheckError(id, quantity, length);
  32. }
  33. }
  34. export function retrieveQuantity(quantity, arr) {
  35. if (quantity === Quantity.MANY) {
  36. return arr;
  37. }
  38. else {
  39. return arr[0];
  40. }
  41. }
  42. function changeQuantity(target, index, quantity) {
  43. var descriptor = getDependencyByIndex(target, index);
  44. descriptor.quantity = quantity;
  45. }
  46. function quantifyDecoratorFactoryProducer(quantity) {
  47. return function decoratorFactory(id) {
  48. if (this instanceof decoratorFactory) {
  49. return this;
  50. }
  51. return function (registerTarget, _key, index) {
  52. if (id) {
  53. setDependency(registerTarget, id, index, quantity);
  54. }
  55. else {
  56. if (quantity === Quantity.REQUIRED) {
  57. throw new IdentifierUndefinedError(registerTarget, index);
  58. }
  59. changeQuantity(registerTarget, index, quantity);
  60. }
  61. };
  62. };
  63. }
  64. export var Many = quantifyDecoratorFactoryProducer(Quantity.MANY);
  65. export var Optional = quantifyDecoratorFactoryProducer(Quantity.OPTIONAL);
  66. export var Inject = quantifyDecoratorFactoryProducer(Quantity.REQUIRED);
  67. //# sourceMappingURL=dependencyQuantity.js.map