a zip code crypto-currency system good for red ONLY

bufferToggle.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /** PURE_IMPORTS_START .._Subscription,.._util_subscribeToResult,.._OuterSubscriber PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { Subscription } from '../Subscription';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. import { OuterSubscriber } from '../OuterSubscriber';
  12. /**
  13. * Buffers the source Observable values starting from an emission from
  14. * `openings` and ending when the output of `closingSelector` emits.
  15. *
  16. * <span class="informal">Collects values from the past as an array. Starts
  17. * collecting only when `opening` emits, and calls the `closingSelector`
  18. * function to get an Observable that tells when to close the buffer.</span>
  19. *
  20. * <img src="./img/bufferToggle.png" width="100%">
  21. *
  22. * Buffers values from the source by opening the buffer via signals from an
  23. * Observable provided to `openings`, and closing and sending the buffers when
  24. * a Subscribable or Promise returned by the `closingSelector` function emits.
  25. *
  26. * @example <caption>Every other second, emit the click events from the next 500ms</caption>
  27. * var clicks = Rx.Observable.fromEvent(document, 'click');
  28. * var openings = Rx.Observable.interval(1000);
  29. * var buffered = clicks.bufferToggle(openings, i =>
  30. * i % 2 ? Rx.Observable.interval(500) : Rx.Observable.empty()
  31. * );
  32. * buffered.subscribe(x => console.log(x));
  33. *
  34. * @see {@link buffer}
  35. * @see {@link bufferCount}
  36. * @see {@link bufferTime}
  37. * @see {@link bufferWhen}
  38. * @see {@link windowToggle}
  39. *
  40. * @param {SubscribableOrPromise<O>} openings A Subscribable or Promise of notifications to start new
  41. * buffers.
  42. * @param {function(value: O): SubscribableOrPromise} closingSelector A function that takes
  43. * the value emitted by the `openings` observable and returns a Subscribable or Promise,
  44. * which, when it emits, signals that the associated buffer should be emitted
  45. * and cleared.
  46. * @return {Observable<T[]>} An observable of arrays of buffered values.
  47. * @method bufferToggle
  48. * @owner Observable
  49. */
  50. export function bufferToggle(openings, closingSelector) {
  51. return function bufferToggleOperatorFunction(source) {
  52. return source.lift(new BufferToggleOperator(openings, closingSelector));
  53. };
  54. }
  55. var BufferToggleOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  56. function BufferToggleOperator(openings, closingSelector) {
  57. this.openings = openings;
  58. this.closingSelector = closingSelector;
  59. }
  60. BufferToggleOperator.prototype.call = function (subscriber, source) {
  61. return source.subscribe(new BufferToggleSubscriber(subscriber, this.openings, this.closingSelector));
  62. };
  63. return BufferToggleOperator;
  64. }());
  65. /**
  66. * We need this JSDoc comment for affecting ESDoc.
  67. * @ignore
  68. * @extends {Ignored}
  69. */
  70. var BufferToggleSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  71. __extends(BufferToggleSubscriber, _super);
  72. function BufferToggleSubscriber(destination, openings, closingSelector) {
  73. _super.call(this, destination);
  74. this.openings = openings;
  75. this.closingSelector = closingSelector;
  76. this.contexts = [];
  77. this.add(subscribeToResult(this, openings));
  78. }
  79. BufferToggleSubscriber.prototype._next = function (value) {
  80. var contexts = this.contexts;
  81. var len = contexts.length;
  82. for (var i = 0; i < len; i++) {
  83. contexts[i].buffer.push(value);
  84. }
  85. };
  86. BufferToggleSubscriber.prototype._error = function (err) {
  87. var contexts = this.contexts;
  88. while (contexts.length > 0) {
  89. var context = contexts.shift();
  90. context.subscription.unsubscribe();
  91. context.buffer = null;
  92. context.subscription = null;
  93. }
  94. this.contexts = null;
  95. _super.prototype._error.call(this, err);
  96. };
  97. BufferToggleSubscriber.prototype._complete = function () {
  98. var contexts = this.contexts;
  99. while (contexts.length > 0) {
  100. var context = contexts.shift();
  101. this.destination.next(context.buffer);
  102. context.subscription.unsubscribe();
  103. context.buffer = null;
  104. context.subscription = null;
  105. }
  106. this.contexts = null;
  107. _super.prototype._complete.call(this);
  108. };
  109. BufferToggleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  110. outerValue ? this.closeBuffer(outerValue) : this.openBuffer(innerValue);
  111. };
  112. BufferToggleSubscriber.prototype.notifyComplete = function (innerSub) {
  113. this.closeBuffer(innerSub.context);
  114. };
  115. BufferToggleSubscriber.prototype.openBuffer = function (value) {
  116. try {
  117. var closingSelector = this.closingSelector;
  118. var closingNotifier = closingSelector.call(this, value);
  119. if (closingNotifier) {
  120. this.trySubscribe(closingNotifier);
  121. }
  122. }
  123. catch (err) {
  124. this._error(err);
  125. }
  126. };
  127. BufferToggleSubscriber.prototype.closeBuffer = function (context) {
  128. var contexts = this.contexts;
  129. if (contexts && context) {
  130. var buffer = context.buffer, subscription = context.subscription;
  131. this.destination.next(buffer);
  132. contexts.splice(contexts.indexOf(context), 1);
  133. this.remove(subscription);
  134. subscription.unsubscribe();
  135. }
  136. };
  137. BufferToggleSubscriber.prototype.trySubscribe = function (closingNotifier) {
  138. var contexts = this.contexts;
  139. var buffer = [];
  140. var subscription = new Subscription();
  141. var context = { buffer: buffer, subscription: subscription };
  142. contexts.push(context);
  143. var innerSubscription = subscribeToResult(this, closingNotifier, context);
  144. if (!innerSubscription || innerSubscription.closed) {
  145. this.closeBuffer(context);
  146. }
  147. else {
  148. innerSubscription.context = context;
  149. this.add(innerSubscription);
  150. subscription.add(innerSubscription);
  151. }
  152. };
  153. return BufferToggleSubscriber;
  154. }(OuterSubscriber));
  155. //# sourceMappingURL=bufferToggle.js.map