concurrencyDecorator.js 1.0 KB

1234567891011121314151617181920212223242526
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.concurrency = void 0;
  4. const tslib_1 = require("tslib");
  5. const concurrency_1 = require("./concurrency");
  6. const instances = new WeakMap();
  7. /**
  8. * A class method decorator that limits the concurrency of the method to the
  9. * given number of parallel executions. All invocations are queued and executed
  10. * in the order they were called.
  11. */
  12. function concurrency(limit) {
  13. return (fn, context) => {
  14. return function (...args) {
  15. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  16. let map = instances.get(this);
  17. if (!map)
  18. instances.set(this, (map = new WeakMap()));
  19. if (!map.has(fn))
  20. map.set(fn, (0, concurrency_1.concurrency)(limit));
  21. return map.get(fn)(() => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield fn.call(this, ...args); }));
  22. });
  23. };
  24. };
  25. }
  26. exports.concurrency = concurrency;