Front end of the Slack clone application.

find.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  9. * Emits only the first value emitted by the source Observable that meets some
  10. * condition.
  11. *
  12. * <span class="informal">Finds the first value that passes some test and emits
  13. * that.</span>
  14. *
  15. * <img src="./img/find.png" width="100%">
  16. *
  17. * `find` searches for the first item in the source Observable that matches the
  18. * specified condition embodied by the `predicate`, and returns the first
  19. * occurrence in the source. Unlike {@link first}, the `predicate` is required
  20. * in `find`, and does not emit an error if a valid value is not found.
  21. *
  22. * @example <caption>Find and emit the first click that happens on a DIV element</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var result = clicks.find(ev => ev.target.tagName === 'DIV');
  25. * result.subscribe(x => console.log(x));
  26. *
  27. * @see {@link filter}
  28. * @see {@link first}
  29. * @see {@link findIndex}
  30. * @see {@link take}
  31. *
  32. * @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
  33. * A function called with each item to test for condition matching.
  34. * @param {any} [thisArg] An optional argument to determine the value of `this`
  35. * in the `predicate` function.
  36. * @return {Observable<T>} An Observable of the first item that matches the
  37. * condition.
  38. * @method find
  39. * @owner Observable
  40. */
  41. function find(predicate, thisArg) {
  42. if (typeof predicate !== 'function') {
  43. throw new TypeError('predicate is not a function');
  44. }
  45. return function (source) { return source.lift(new FindValueOperator(predicate, source, false, thisArg)); };
  46. }
  47. exports.find = find;
  48. var FindValueOperator = (function () {
  49. function FindValueOperator(predicate, source, yieldIndex, thisArg) {
  50. this.predicate = predicate;
  51. this.source = source;
  52. this.yieldIndex = yieldIndex;
  53. this.thisArg = thisArg;
  54. }
  55. FindValueOperator.prototype.call = function (observer, source) {
  56. return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
  57. };
  58. return FindValueOperator;
  59. }());
  60. exports.FindValueOperator = FindValueOperator;
  61. /**
  62. * We need this JSDoc comment for affecting ESDoc.
  63. * @ignore
  64. * @extends {Ignored}
  65. */
  66. var FindValueSubscriber = (function (_super) {
  67. __extends(FindValueSubscriber, _super);
  68. function FindValueSubscriber(destination, predicate, source, yieldIndex, thisArg) {
  69. _super.call(this, destination);
  70. this.predicate = predicate;
  71. this.source = source;
  72. this.yieldIndex = yieldIndex;
  73. this.thisArg = thisArg;
  74. this.index = 0;
  75. }
  76. FindValueSubscriber.prototype.notifyComplete = function (value) {
  77. var destination = this.destination;
  78. destination.next(value);
  79. destination.complete();
  80. };
  81. FindValueSubscriber.prototype._next = function (value) {
  82. var _a = this, predicate = _a.predicate, thisArg = _a.thisArg;
  83. var index = this.index++;
  84. try {
  85. var result = predicate.call(thisArg || this, value, index, this.source);
  86. if (result) {
  87. this.notifyComplete(this.yieldIndex ? index : value);
  88. }
  89. }
  90. catch (err) {
  91. this.destination.error(err);
  92. }
  93. };
  94. FindValueSubscriber.prototype._complete = function () {
  95. this.notifyComplete(this.yieldIndex ? -1 : undefined);
  96. };
  97. return FindValueSubscriber;
  98. }(Subscriber_1.Subscriber));
  99. exports.FindValueSubscriber = FindValueSubscriber;
  100. //# sourceMappingURL=find.js.map