a zip code crypto-currency system good for red ONLY

sequenceEqual.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /** PURE_IMPORTS_START .._Subscriber,.._util_tryCatch,.._util_errorObject 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 { Subscriber } from '../Subscriber';
  10. import { tryCatch } from '../util/tryCatch';
  11. import { errorObject } from '../util/errorObject';
  12. /**
  13. * Compares all values of two observables in sequence using an optional comparor function
  14. * and returns an observable of a single boolean value representing whether or not the two sequences
  15. * are equal.
  16. *
  17. * <span class="informal">Checks to see of all values emitted by both observables are equal, in order.</span>
  18. *
  19. * <img src="./img/sequenceEqual.png" width="100%">
  20. *
  21. * `sequenceEqual` subscribes to two observables and buffers incoming values from each observable. Whenever either
  22. * observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom
  23. * up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the
  24. * observables completes, the operator will wait for the other observable to complete; If the other
  25. * observable emits before completing, the returned observable will emit `false` and complete. If one observable never
  26. * completes or emits after the other complets, the returned observable will never complete.
  27. *
  28. * @example <caption>figure out if the Konami code matches</caption>
  29. * var code = Rx.Observable.from([
  30. * "ArrowUp",
  31. * "ArrowUp",
  32. * "ArrowDown",
  33. * "ArrowDown",
  34. * "ArrowLeft",
  35. * "ArrowRight",
  36. * "ArrowLeft",
  37. * "ArrowRight",
  38. * "KeyB",
  39. * "KeyA",
  40. * "Enter" // no start key, clearly.
  41. * ]);
  42. *
  43. * var keys = Rx.Observable.fromEvent(document, 'keyup')
  44. * .map(e => e.code);
  45. * var matches = keys.bufferCount(11, 1)
  46. * .mergeMap(
  47. * last11 =>
  48. * Rx.Observable.from(last11)
  49. * .sequenceEqual(code)
  50. * );
  51. * matches.subscribe(matched => console.log('Successful cheat at Contra? ', matched));
  52. *
  53. * @see {@link combineLatest}
  54. * @see {@link zip}
  55. * @see {@link withLatestFrom}
  56. *
  57. * @param {Observable} compareTo The observable sequence to compare the source sequence to.
  58. * @param {function} [comparor] An optional function to compare each value pair
  59. * @return {Observable} An Observable of a single boolean value representing whether or not
  60. * the values emitted by both observables were equal in sequence.
  61. * @method sequenceEqual
  62. * @owner Observable
  63. */
  64. export function sequenceEqual(compareTo, comparor) {
  65. return function (source) { return source.lift(new SequenceEqualOperator(compareTo, comparor)); };
  66. }
  67. export var SequenceEqualOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  68. function SequenceEqualOperator(compareTo, comparor) {
  69. this.compareTo = compareTo;
  70. this.comparor = comparor;
  71. }
  72. SequenceEqualOperator.prototype.call = function (subscriber, source) {
  73. return source.subscribe(new SequenceEqualSubscriber(subscriber, this.compareTo, this.comparor));
  74. };
  75. return SequenceEqualOperator;
  76. }());
  77. /**
  78. * We need this JSDoc comment for affecting ESDoc.
  79. * @ignore
  80. * @extends {Ignored}
  81. */
  82. export var SequenceEqualSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  83. __extends(SequenceEqualSubscriber, _super);
  84. function SequenceEqualSubscriber(destination, compareTo, comparor) {
  85. _super.call(this, destination);
  86. this.compareTo = compareTo;
  87. this.comparor = comparor;
  88. this._a = [];
  89. this._b = [];
  90. this._oneComplete = false;
  91. this.add(compareTo.subscribe(new SequenceEqualCompareToSubscriber(destination, this)));
  92. }
  93. SequenceEqualSubscriber.prototype._next = function (value) {
  94. if (this._oneComplete && this._b.length === 0) {
  95. this.emit(false);
  96. }
  97. else {
  98. this._a.push(value);
  99. this.checkValues();
  100. }
  101. };
  102. SequenceEqualSubscriber.prototype._complete = function () {
  103. if (this._oneComplete) {
  104. this.emit(this._a.length === 0 && this._b.length === 0);
  105. }
  106. else {
  107. this._oneComplete = true;
  108. }
  109. };
  110. SequenceEqualSubscriber.prototype.checkValues = function () {
  111. var _c = this, _a = _c._a, _b = _c._b, comparor = _c.comparor;
  112. while (_a.length > 0 && _b.length > 0) {
  113. var a = _a.shift();
  114. var b = _b.shift();
  115. var areEqual = false;
  116. if (comparor) {
  117. areEqual = tryCatch(comparor)(a, b);
  118. if (areEqual === errorObject) {
  119. this.destination.error(errorObject.e);
  120. }
  121. }
  122. else {
  123. areEqual = a === b;
  124. }
  125. if (!areEqual) {
  126. this.emit(false);
  127. }
  128. }
  129. };
  130. SequenceEqualSubscriber.prototype.emit = function (value) {
  131. var destination = this.destination;
  132. destination.next(value);
  133. destination.complete();
  134. };
  135. SequenceEqualSubscriber.prototype.nextB = function (value) {
  136. if (this._oneComplete && this._a.length === 0) {
  137. this.emit(false);
  138. }
  139. else {
  140. this._b.push(value);
  141. this.checkValues();
  142. }
  143. };
  144. return SequenceEqualSubscriber;
  145. }(Subscriber));
  146. var SequenceEqualCompareToSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  147. __extends(SequenceEqualCompareToSubscriber, _super);
  148. function SequenceEqualCompareToSubscriber(destination, parent) {
  149. _super.call(this, destination);
  150. this.parent = parent;
  151. }
  152. SequenceEqualCompareToSubscriber.prototype._next = function (value) {
  153. this.parent.nextB(value);
  154. };
  155. SequenceEqualCompareToSubscriber.prototype._error = function (err) {
  156. this.parent.error(err);
  157. };
  158. SequenceEqualCompareToSubscriber.prototype._complete = function () {
  159. this.parent._complete();
  160. };
  161. return SequenceEqualCompareToSubscriber;
  162. }(Subscriber));
  163. //# sourceMappingURL=sequenceEqual.js.map