bufferToggle.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.bufferToggle = void 0;
  15. var Subscription_1 = require("../Subscription");
  16. var lift_1 = require("../util/lift");
  17. var innerFrom_1 = require("../observable/innerFrom");
  18. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  19. var noop_1 = require("../util/noop");
  20. var arrRemove_1 = require("../util/arrRemove");
  21. function bufferToggle(openings, closingSelector) {
  22. return lift_1.operate(function (source, subscriber) {
  23. var buffers = [];
  24. innerFrom_1.innerFrom(openings).subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (openValue) {
  25. var buffer = [];
  26. buffers.push(buffer);
  27. var closingSubscription = new Subscription_1.Subscription();
  28. var emitBuffer = function () {
  29. arrRemove_1.arrRemove(buffers, buffer);
  30. subscriber.next(buffer);
  31. closingSubscription.unsubscribe();
  32. };
  33. closingSubscription.add(innerFrom_1.innerFrom(closingSelector(openValue)).subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, emitBuffer, noop_1.noop)));
  34. }, noop_1.noop));
  35. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  36. var e_1, _a;
  37. try {
  38. for (var buffers_1 = __values(buffers), buffers_1_1 = buffers_1.next(); !buffers_1_1.done; buffers_1_1 = buffers_1.next()) {
  39. var buffer = buffers_1_1.value;
  40. buffer.push(value);
  41. }
  42. }
  43. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  44. finally {
  45. try {
  46. if (buffers_1_1 && !buffers_1_1.done && (_a = buffers_1.return)) _a.call(buffers_1);
  47. }
  48. finally { if (e_1) throw e_1.error; }
  49. }
  50. }, function () {
  51. while (buffers.length > 0) {
  52. subscriber.next(buffers.shift());
  53. }
  54. subscriber.complete();
  55. }));
  56. });
  57. }
  58. exports.bufferToggle = bufferToggle;
  59. //# sourceMappingURL=bufferToggle.js.map