bufferToggle.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** PURE_IMPORTS_START tslib,_Subscription,_util_subscribeToResult,_OuterSubscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscription } from '../Subscription';
  4. import { subscribeToResult } from '../util/subscribeToResult';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. export function bufferToggle(openings, closingSelector) {
  7. return function bufferToggleOperatorFunction(source) {
  8. return source.lift(new BufferToggleOperator(openings, closingSelector));
  9. };
  10. }
  11. var BufferToggleOperator = /*@__PURE__*/ (function () {
  12. function BufferToggleOperator(openings, closingSelector) {
  13. this.openings = openings;
  14. this.closingSelector = closingSelector;
  15. }
  16. BufferToggleOperator.prototype.call = function (subscriber, source) {
  17. return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
  18. };
  19. return BufferToggleOperator;
  20. }());
  21. var BufferToggleSubscriber = /*@__PURE__*/ (function (_super) {
  22. tslib_1.__extends(BufferToggleSubscriber, _super);
  23. function BufferToggleSubscriber(destination, openings, closingSelector) {
  24. var _this = _super.call(this, destination) || this;
  25. _this.closingSelector = closingSelector;
  26. _this.contexts = [];
  27. _this.add(subscribeToResult(_this, openings));
  28. return _this;
  29. }
  30. BufferToggleSubscriber.prototype._next = function (value) {
  31. var contexts = this.contexts;
  32. var len = contexts.length;
  33. for (var i = 0; i < len; i++) {
  34. contexts[i].buffer.push(value);
  35. }
  36. };
  37. BufferToggleSubscriber.prototype._error = function (err) {
  38. var contexts = this.contexts;
  39. while (contexts.length > 0) {
  40. var context_1 = contexts.shift();
  41. context_1.subscription.unsubscribe();
  42. context_1.buffer = null;
  43. context_1.subscription = null;
  44. }
  45. this.contexts = null;
  46. _super.prototype._error.call(this, err);
  47. };
  48. BufferToggleSubscriber.prototype._complete = function () {
  49. var contexts = this.contexts;
  50. while (contexts.length > 0) {
  51. var context_2 = contexts.shift();
  52. this.destination.next(context_2.buffer);
  53. context_2.subscription.unsubscribe();
  54. context_2.buffer = null;
  55. context_2.subscription = null;
  56. }
  57. this.contexts = null;
  58. _super.prototype._complete.call(this);
  59. };
  60. BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue) {
  61. outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
  62. };
  63. BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
  64. this.closeBuffer(innerSub.context);
  65. };
  66. BufferToggleSubscriber.prototype.openBuffer = function (value) {
  67. try {
  68. var closingSelector = this.closingSelector;
  69. var closingNotifier = closingSelector.call(this, value);
  70. if (closingNotifier) {
  71. this.trySubscribe(closingNotifier);
  72. }
  73. }
  74. catch (err) {
  75. this._error(err);
  76. }
  77. };
  78. BufferToggleSubscriber.prototype.closeBuffer = function (context) {
  79. var contexts = this.contexts;
  80. if (contexts && context) {
  81. var buffer = context.buffer, subscription = context.subscription;
  82. this.destination.next(buffer);
  83. contexts.splice(contexts.indexOf(context), 1);
  84. this.remove(subscription);
  85. subscription.unsubscribe();
  86. }
  87. };
  88. BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
  89. var contexts = this.contexts;
  90. var buffer = [];
  91. var subscription = new Subscription();
  92. var context = { buffer: buffer, subscription: subscription };
  93. contexts.push(context);
  94. var innerSubscription = subscribeToResult(this, closingNotifier, context);
  95. if (!innerSubscription || innerSubscription.closed) {
  96. this.closeBuffer(context);
  97. }
  98. else {
  99. innerSubscription.context = context;
  100. this.add(innerSubscription);
  101. subscription.add(innerSubscription);
  102. }
  103. };
  104. return BufferToggleSubscriber;
  105. }(OuterSubscriber));
  106. //# sourceMappingURL=bufferToggle.js.map