Front end of the Slack clone application.

sequenceEqual.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Subscriber_1 = require('../Subscriber');
  8. var tryCatch_1 = require('../util/tryCatch');
  9. var errorObject_1 = require('../util/errorObject');
  10. /**
  11. * Compares all values of two observables in sequence using an optional comparor function
  12. * and returns an observable of a single boolean value representing whether or not the two sequences
  13. * are equal.
  14. *
  15. * <span class="informal">Checks to see of all values emitted by both observables are equal, in order.</span>
  16. *
  17. * <img src="./img/sequenceEqual.png" width="100%">
  18. *
  19. * `sequenceEqual` subscribes to two observables and buffers incoming values from each observable. Whenever either
  20. * observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom
  21. * up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the
  22. * observables completes, the operator will wait for the other observable to complete; If the other
  23. * observable emits before completing, the returned observable will emit `false` and complete. If one observable never
  24. * completes or emits after the other complets, the returned observable will never complete.
  25. *
  26. * @example <caption>figure out if the Konami code matches</caption>
  27. * var code = Rx.Observable.from([
  28. * "ArrowUp",
  29. * "ArrowUp",
  30. * "ArrowDown",
  31. * "ArrowDown",
  32. * "ArrowLeft",
  33. * "ArrowRight",
  34. * "ArrowLeft",
  35. * "ArrowRight",
  36. * "KeyB",
  37. * "KeyA",
  38. * "Enter" // no start key, clearly.
  39. * ]);
  40. *
  41. * var keys = Rx.Observable.fromEvent(document, 'keyup')
  42. * .map(e => e.code);
  43. * var matches = keys.bufferCount(11, 1)
  44. * .mergeMap(
  45. * last11 =>
  46. * Rx.Observable.from(last11)
  47. * .sequenceEqual(code)
  48. * );
  49. * matches.subscribe(matched => console.log('Successful cheat at Contra? ', matched));
  50. *
  51. * @see {@link combineLatest}
  52. * @see {@link zip}
  53. * @see {@link withLatestFrom}
  54. *
  55. * @param {Observable} compareTo The observable sequence to compare the source sequence to.
  56. * @param {function} [comparor] An optional function to compare each value pair
  57. * @return {Observable} An Observable of a single boolean value representing whether or not
  58. * the values emitted by both observables were equal in sequence.
  59. * @method sequenceEqual
  60. * @owner Observable
  61. */
  62. function sequenceEqual(compareTo, comparor) {
  63. return function (source) { return source.lift(new SequenceEqualOperator(compareTo, comparor)); };
  64. }
  65. exports.sequenceEqual = sequenceEqual;
  66. var SequenceEqualOperator = (function () {
  67. function SequenceEqualOperator(compareTo, comparor) {
  68. this.compareTo = compareTo;
  69. this.comparor = comparor;
  70. }
  71. SequenceEqualOperator.prototype.call = function (subscriber, source) {
  72. return source.subscribe(new SequenceEqualSubscriber(subscriber, this.compareTo, this.comparor));
  73. };
  74. return SequenceEqualOperator;
  75. }());
  76. exports.SequenceEqualOperator = SequenceEqualOperator;
  77. /**
  78. * We need this JSDoc comment for affecting ESDoc.
  79. * @ignore
  80. * @extends {Ignored}
  81. */
  82. var SequenceEqualSubscriber = (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_1.tryCatch(comparor)(a, b);
  118. if (areEqual === errorObject_1.errorObject) {
  119. this.destination.error(errorObject_1.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_1.Subscriber));
  146. exports.SequenceEqualSubscriber = SequenceEqualSubscriber;
  147. var SequenceEqualCompareToSubscriber = (function (_super) {
  148. __extends(SequenceEqualCompareToSubscriber, _super);
  149. function SequenceEqualCompareToSubscriber(destination, parent) {
  150. _super.call(this, destination);
  151. this.parent = parent;
  152. }
  153. SequenceEqualCompareToSubscriber.prototype._next = function (value) {
  154. this.parent.nextB(value);
  155. };
  156. SequenceEqualCompareToSubscriber.prototype._error = function (err) {
  157. this.parent.error(err);
  158. };
  159. SequenceEqualCompareToSubscriber.prototype._complete = function () {
  160. this.parent._complete();
  161. };
  162. return SequenceEqualCompareToSubscriber;
  163. }(Subscriber_1.Subscriber));
  164. //# sourceMappingURL=sequenceEqual.js.map