concurrencyDecorator.js 841 B

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