concurrencyDecorator.js 848 B

1234567891011121314151617181920212223
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.concurrency = concurrency;
  4. const concurrency_1 = require("./concurrency");
  5. /* tslint:disable no-invalid-this */
  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 async function (...args) {
  15. let map = instances.get(this);
  16. if (!map)
  17. instances.set(this, (map = new WeakMap()));
  18. if (!map.has(fn))
  19. map.set(fn, (0, concurrency_1.concurrency)(limit));
  20. return map.get(fn)(async () => await fn.call(this, ...args));
  21. };
  22. };
  23. }