a zip code crypto-currency system good for red ONLY

sequenceEqual.js 4.8KB

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