Front end of the Slack clone application.

first.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 EmptyError_1 = require('../util/EmptyError');
  9. /**
  10. * Emits only the first value (or the first value that meets some condition)
  11. * emitted by the source Observable.
  12. *
  13. * <span class="informal">Emits only the first value. Or emits only the first
  14. * value that passes some test.</span>
  15. *
  16. * <img src="./img/first.png" width="100%">
  17. *
  18. * If called with no arguments, `first` emits the first value of the source
  19. * Observable, then completes. If called with a `predicate` function, `first`
  20. * emits the first value of the source that matches the specified condition. It
  21. * may also take a `resultSelector` function to produce the output value from
  22. * the input value, and a `defaultValue` to emit in case the source completes
  23. * before it is able to emit a valid value. Throws an error if `defaultValue`
  24. * was not provided and a matching element is not found.
  25. *
  26. * @example <caption>Emit only the first click that happens on the DOM</caption>
  27. * var clicks = Rx.Observable.fromEvent(document, 'click');
  28. * var result = clicks.first();
  29. * result.subscribe(x => console.log(x));
  30. *
  31. * @example <caption>Emits the first click that happens on a DIV</caption>
  32. * var clicks = Rx.Observable.fromEvent(document, 'click');
  33. * var result = clicks.first(ev => ev.target.tagName === 'DIV');
  34. * result.subscribe(x => console.log(x));
  35. *
  36. * @see {@link filter}
  37. * @see {@link find}
  38. * @see {@link take}
  39. *
  40. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  41. * callback if the Observable completes before any `next` notification was sent.
  42. *
  43. * @param {function(value: T, index: number, source: Observable<T>): boolean} [predicate]
  44. * An optional function called with each item to test for condition matching.
  45. * @param {function(value: T, index: number): R} [resultSelector] A function to
  46. * produce the value on the output Observable based on the values
  47. * and the indices of the source Observable. The arguments passed to this
  48. * function are:
  49. * - `value`: the value that was emitted on the source.
  50. * - `index`: the "index" of the value from the source.
  51. * @param {R} [defaultValue] The default value emitted in case no valid value
  52. * was found on the source.
  53. * @return {Observable<T|R>} An Observable of the first item that matches the
  54. * condition.
  55. * @method first
  56. * @owner Observable
  57. */
  58. function first(predicate, resultSelector, defaultValue) {
  59. return function (source) { return source.lift(new FirstOperator(predicate, resultSelector, defaultValue, source)); };
  60. }
  61. exports.first = first;
  62. var FirstOperator = (function () {
  63. function FirstOperator(predicate, resultSelector, defaultValue, source) {
  64. this.predicate = predicate;
  65. this.resultSelector = resultSelector;
  66. this.defaultValue = defaultValue;
  67. this.source = source;
  68. }
  69. FirstOperator.prototype.call = function (observer, source) {
  70. return source.subscribe(new FirstSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source));
  71. };
  72. return FirstOperator;
  73. }());
  74. /**
  75. * We need this JSDoc comment for affecting ESDoc.
  76. * @ignore
  77. * @extends {Ignored}
  78. */
  79. var FirstSubscriber = (function (_super) {
  80. __extends(FirstSubscriber, _super);
  81. function FirstSubscriber(destination, predicate, resultSelector, defaultValue, source) {
  82. _super.call(this, destination);
  83. this.predicate = predicate;
  84. this.resultSelector = resultSelector;
  85. this.defaultValue = defaultValue;
  86. this.source = source;
  87. this.index = 0;
  88. this.hasCompleted = false;
  89. this._emitted = false;
  90. }
  91. FirstSubscriber.prototype._next = function (value) {
  92. var index = this.index++;
  93. if (this.predicate) {
  94. this._tryPredicate(value, index);
  95. }
  96. else {
  97. this._emit(value, index);
  98. }
  99. };
  100. FirstSubscriber.prototype._tryPredicate = function (value, index) {
  101. var result;
  102. try {
  103. result = this.predicate(value, index, this.source);
  104. }
  105. catch (err) {
  106. this.destination.error(err);
  107. return;
  108. }
  109. if (result) {
  110. this._emit(value, index);
  111. }
  112. };
  113. FirstSubscriber.prototype._emit = function (value, index) {
  114. if (this.resultSelector) {
  115. this._tryResultSelector(value, index);
  116. return;
  117. }
  118. this._emitFinal(value);
  119. };
  120. FirstSubscriber.prototype._tryResultSelector = function (value, index) {
  121. var result;
  122. try {
  123. result = this.resultSelector(value, index);
  124. }
  125. catch (err) {
  126. this.destination.error(err);
  127. return;
  128. }
  129. this._emitFinal(result);
  130. };
  131. FirstSubscriber.prototype._emitFinal = function (value) {
  132. var destination = this.destination;
  133. if (!this._emitted) {
  134. this._emitted = true;
  135. destination.next(value);
  136. destination.complete();
  137. this.hasCompleted = true;
  138. }
  139. };
  140. FirstSubscriber.prototype._complete = function () {
  141. var destination = this.destination;
  142. if (!this.hasCompleted && typeof this.defaultValue !== 'undefined') {
  143. destination.next(this.defaultValue);
  144. destination.complete();
  145. }
  146. else if (!this.hasCompleted) {
  147. destination.error(new EmptyError_1.EmptyError);
  148. }
  149. };
  150. return FirstSubscriber;
  151. }(Subscriber_1.Subscriber));
  152. //# sourceMappingURL=first.js.map