No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

executor.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.InlineExecutor = exports.QueueExecutor = void 0;
  4. const queue_1 = require("../../../throttler/queue");
  5. async function handler(op) {
  6. var _a, _b, _c, _d, _e, _f;
  7. try {
  8. op.result = await op.func();
  9. }
  10. catch (err) {
  11. const code = err.status ||
  12. err.code ||
  13. ((_b = (_a = err.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode) ||
  14. ((_c = err.original) === null || _c === void 0 ? void 0 : _c.code) ||
  15. ((_f = (_e = (_d = err.original) === null || _d === void 0 ? void 0 : _d.context) === null || _e === void 0 ? void 0 : _e.response) === null || _f === void 0 ? void 0 : _f.statusCode);
  16. if (code === 429 || code === 409 || code === 503) {
  17. throw err;
  18. }
  19. op.error = err;
  20. }
  21. return;
  22. }
  23. class QueueExecutor {
  24. constructor(options) {
  25. this.queue = new queue_1.Queue(Object.assign(Object.assign({}, options), { handler }));
  26. }
  27. async run(func) {
  28. const op = { func };
  29. await this.queue.run(op);
  30. if (op.error) {
  31. throw op.error;
  32. }
  33. return op.result;
  34. }
  35. }
  36. exports.QueueExecutor = QueueExecutor;
  37. class InlineExecutor {
  38. run(func) {
  39. return func();
  40. }
  41. }
  42. exports.InlineExecutor = InlineExecutor;