a zip code crypto-currency system good for red ONLY

bufferToggle.js 6.2KB

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