codeMutex.js 635 B

12345678910111213141516171819202122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.codeMutex = void 0;
  4. const tslib_1 = require("tslib");
  5. /**
  6. * Executes only one instance of give code at a time. If other calls come in in
  7. * parallel, they get resolved to the result of the ongoing execution.
  8. */
  9. const codeMutex = () => {
  10. let result;
  11. return (code) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
  12. if (result)
  13. return result;
  14. try {
  15. return yield (result = code());
  16. }
  17. finally {
  18. result = undefined;
  19. }
  20. });
  21. };
  22. exports.codeMutex = codeMutex;