bufferWhen.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /** PURE_IMPORTS_START tslib,_Subscription,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscription } from '../Subscription';
  4. import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
  5. export function bufferWhen(closingSelector) {
  6. return function (source) {
  7. return source.lift(new BufferWhenOperator(closingSelector));
  8. };
  9. }
  10. var BufferWhenOperator = /*@__PURE__*/ (function () {
  11. function BufferWhenOperator(closingSelector) {
  12. this.closingSelector = closingSelector;
  13. }
  14. BufferWhenOperator.prototype.call = function (subscriber, source) {
  15. return source.subscribe(new BufferWhenSubscriber(subscriber, this.closingSelector));
  16. };
  17. return BufferWhenOperator;
  18. }());
  19. var BufferWhenSubscriber = /*@__PURE__*/ (function (_super) {
  20. tslib_1.__extends(BufferWhenSubscriber, _super);
  21. function BufferWhenSubscriber(destination, closingSelector) {
  22. var _this = _super.call(this, destination) || this;
  23. _this.closingSelector = closingSelector;
  24. _this.subscribing = false;
  25. _this.openBuffer();
  26. return _this;
  27. }
  28. BufferWhenSubscriber.prototype._next = function (value) {
  29. this.buffer.push(value);
  30. };
  31. BufferWhenSubscriber.prototype._complete = function () {
  32. var buffer = this.buffer;
  33. if (buffer) {
  34. this.destination.next(buffer);
  35. }
  36. _super.prototype._complete.call(this);
  37. };
  38. BufferWhenSubscriber.prototype._unsubscribe = function () {
  39. this.buffer = undefined;
  40. this.subscribing = false;
  41. };
  42. BufferWhenSubscriber.prototype.notifyNext = function () {
  43. this.openBuffer();
  44. };
  45. BufferWhenSubscriber.prototype.notifyComplete = function () {
  46. if (this.subscribing) {
  47. this.complete();
  48. }
  49. else {
  50. this.openBuffer();
  51. }
  52. };
  53. BufferWhenSubscriber.prototype.openBuffer = function () {
  54. var closingSubscription = this.closingSubscription;
  55. if (closingSubscription) {
  56. this.remove(closingSubscription);
  57. closingSubscription.unsubscribe();
  58. }
  59. var buffer = this.buffer;
  60. if (this.buffer) {
  61. this.destination.next(buffer);
  62. }
  63. this.buffer = [];
  64. var closingNotifier;
  65. try {
  66. var closingSelector = this.closingSelector;
  67. closingNotifier = closingSelector();
  68. }
  69. catch (err) {
  70. return this.error(err);
  71. }
  72. closingSubscription = new Subscription();
  73. this.closingSubscription = closingSubscription;
  74. this.add(closingSubscription);
  75. this.subscribing = true;
  76. closingSubscription.add(innerSubscribe(closingNotifier, new SimpleInnerSubscriber(this)));
  77. this.subscribing = false;
  78. };
  79. return BufferWhenSubscriber;
  80. }(SimpleOuterSubscriber));
  81. //# sourceMappingURL=bufferWhen.js.map