pubsub-async-iterator.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (_) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. exports.PubSubAsyncIterator = void 0;
  40. var iterall_1 = require("iterall");
  41. var PubSubAsyncIterator = (function () {
  42. function PubSubAsyncIterator(pubsub, eventNames) {
  43. this.pubsub = pubsub;
  44. this.pullQueue = [];
  45. this.pushQueue = [];
  46. this.running = true;
  47. this.allSubscribed = null;
  48. this.eventsArray = typeof eventNames === 'string' ? [eventNames] : eventNames;
  49. }
  50. PubSubAsyncIterator.prototype.next = function () {
  51. return __awaiter(this, void 0, void 0, function () {
  52. return __generator(this, function (_a) {
  53. switch (_a.label) {
  54. case 0:
  55. if (!!this.allSubscribed) return [3, 2];
  56. return [4, (this.allSubscribed = this.subscribeAll())];
  57. case 1:
  58. _a.sent();
  59. _a.label = 2;
  60. case 2: return [2, this.pullValue()];
  61. }
  62. });
  63. });
  64. };
  65. PubSubAsyncIterator.prototype.return = function () {
  66. return __awaiter(this, void 0, void 0, function () {
  67. return __generator(this, function (_a) {
  68. switch (_a.label) {
  69. case 0: return [4, this.emptyQueue()];
  70. case 1:
  71. _a.sent();
  72. return [2, { value: undefined, done: true }];
  73. }
  74. });
  75. });
  76. };
  77. PubSubAsyncIterator.prototype.throw = function (error) {
  78. return __awaiter(this, void 0, void 0, function () {
  79. return __generator(this, function (_a) {
  80. switch (_a.label) {
  81. case 0: return [4, this.emptyQueue()];
  82. case 1:
  83. _a.sent();
  84. return [2, Promise.reject(error)];
  85. }
  86. });
  87. });
  88. };
  89. PubSubAsyncIterator.prototype[iterall_1.$$asyncIterator] = function () {
  90. return this;
  91. };
  92. PubSubAsyncIterator.prototype.pushValue = function (event) {
  93. return __awaiter(this, void 0, void 0, function () {
  94. return __generator(this, function (_a) {
  95. switch (_a.label) {
  96. case 0: return [4, this.allSubscribed];
  97. case 1:
  98. _a.sent();
  99. if (this.pullQueue.length !== 0) {
  100. this.pullQueue.shift()(this.running
  101. ? { value: event, done: false }
  102. : { value: undefined, done: true });
  103. }
  104. else {
  105. this.pushQueue.push(event);
  106. }
  107. return [2];
  108. }
  109. });
  110. });
  111. };
  112. PubSubAsyncIterator.prototype.pullValue = function () {
  113. var _this = this;
  114. return new Promise(function (resolve) {
  115. if (_this.pushQueue.length !== 0) {
  116. resolve(_this.running
  117. ? { value: _this.pushQueue.shift(), done: false }
  118. : { value: undefined, done: true });
  119. }
  120. else {
  121. _this.pullQueue.push(resolve);
  122. }
  123. });
  124. };
  125. PubSubAsyncIterator.prototype.emptyQueue = function () {
  126. return __awaiter(this, void 0, void 0, function () {
  127. var subscriptionIds;
  128. return __generator(this, function (_a) {
  129. switch (_a.label) {
  130. case 0:
  131. if (!this.running) return [3, 2];
  132. this.running = false;
  133. this.pullQueue.forEach(function (resolve) { return resolve({ value: undefined, done: true }); });
  134. this.pullQueue.length = 0;
  135. this.pushQueue.length = 0;
  136. return [4, this.allSubscribed];
  137. case 1:
  138. subscriptionIds = _a.sent();
  139. if (subscriptionIds) {
  140. this.unsubscribeAll(subscriptionIds);
  141. }
  142. _a.label = 2;
  143. case 2: return [2];
  144. }
  145. });
  146. });
  147. };
  148. PubSubAsyncIterator.prototype.subscribeAll = function () {
  149. var _this = this;
  150. return Promise.all(this.eventsArray.map(function (eventName) { return _this.pubsub.subscribe(eventName, _this.pushValue.bind(_this), {}); }));
  151. };
  152. PubSubAsyncIterator.prototype.unsubscribeAll = function (subscriptionIds) {
  153. for (var _i = 0, subscriptionIds_1 = subscriptionIds; _i < subscriptionIds_1.length; _i++) {
  154. var subscriptionId = subscriptionIds_1[_i];
  155. this.pubsub.unsubscribe(subscriptionId);
  156. }
  157. };
  158. return PubSubAsyncIterator;
  159. }());
  160. exports.PubSubAsyncIterator = PubSubAsyncIterator;
  161. //# sourceMappingURL=pubsub-async-iterator.js.map