Source: groups.js

  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.ConcurrentGroup = exports.SerialGroup = void 0;
  13. /**
  14. * The base type for Runnables composed of other Runnables.
  15. * Do not construct the base Group type; use Job.sequential (or SerialGroup)
  16. * or Job.concurrent (or ConcurrentGroup) instead.
  17. *
  18. * @abstract
  19. */
  20. class Group {
  21. constructor(...runnables) {
  22. this.runnables = runnables || [];
  23. }
  24. add(...runnables) {
  25. for (const runnable of runnables) {
  26. this.runnables.push(runnable);
  27. }
  28. }
  29. length() {
  30. return this.runnables.length;
  31. }
  32. }
  33. /**
  34. * A Runnable composed of sub-Runnables (such as jobs
  35. * or concurrent groups) running one after another.
  36. * A new Runnable is started only when the previous one completes.
  37. * The sequence completes when the last Runnable has completed (or when any
  38. * Runnable fails).
  39. *
  40. * @param runnables The work items to be run in sequence
  41. */
  42. class SerialGroup extends Group {
  43. constructor(...runnables) {
  44. super(...runnables);
  45. }
  46. /**
  47. * Runs the serial group.
  48. *
  49. * @returns A Promise which completes when the last item in the group completes (or
  50. * any item fails). If all items ran successfully, the Promise resolves; if any
  51. * item failed (that is, its Runnable#run Promise rejected), the Promise
  52. * rejects.
  53. */
  54. run() {
  55. return __awaiter(this, void 0, void 0, function* () {
  56. for (const runnable of this.runnables) {
  57. yield runnable.run();
  58. }
  59. });
  60. }
  61. }
  62. exports.SerialGroup = SerialGroup;
  63. /**
  64. * A Runnable composed of sub-Runnables (such as jobs
  65. * or sequential groups) running concurrently.
  66. * When run, all Runnables are started simultaneously (subject to
  67. * scheduling constraints).
  68. * The concurrent group completes when all Runnables have completed.
  69. *
  70. * @param runnables The work items to be run in parallel
  71. */
  72. class ConcurrentGroup extends Group {
  73. constructor(...runnables) {
  74. super(...runnables);
  75. }
  76. /**
  77. * Runs the concurrent group.
  78. *
  79. * @returns A Promise which completes when all items in the group complete.
  80. * If all items ran successfully, the Promise resolves; if any
  81. * item failed (that is, its Runnable#run Promise rejected), the Promise
  82. * rejects.
  83. */
  84. run() {
  85. return __awaiter(this, void 0, void 0, function* () {
  86. const promises = [];
  87. for (const runnable of this.runnables) {
  88. promises.push(runnable.run());
  89. }
  90. try {
  91. yield Promise.all(promises);
  92. return Promise.resolve();
  93. }
  94. catch (e) {
  95. return Promise.reject(e);
  96. }
  97. });
  98. }
  99. }
  100. exports.ConcurrentGroup = ConcurrentGroup;
  101. //# sourceMappingURL=groups.js.map