first.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Emits only the first value (or the first value that meets some condition)
  7. * emitted by the source Observable.
  8. *
  9. * <span class="informal">Emits only the first value. Or emits only the first
  10. * value that passes some test.</span>
  11. *
  12. * <img src="./img/first.png" width="100%">
  13. *
  14. * If called with no arguments, `first` emits the first value of the source
  15. * Observable, then completes. If called with a `predicate` function, `first`
  16. * emits the first value of the source that matches the specified condition. It
  17. * may also take a `resultSelector` function to produce the output value from
  18. * the input value, and a `defaultValue` to emit in case the source completes
  19. * before it is able to emit a valid value. Throws an error if `defaultValue`
  20. * was not provided and a matching element is not found.
  21. *
  22. * @example <caption>Emit only the first click that happens on the DOM</caption>
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var result = clicks.first();
  25. * result.subscribe(x => console.log(x));
  26. *
  27. * @example <caption>Emits the first click that happens on a DIV</caption>
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var result = clicks.first(ev => ev.target.tagName === 'DIV');
  30. * result.subscribe(x => console.log(x));
  31. *
  32. * @see {@link filter}
  33. * @see {@link find}
  34. * @see {@link take}
  35. *
  36. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  37. * callback if the Observable completes before any `next` notification was sent.
  38. *
  39. * @param {function(value: T, index: number, source: Observable<T>): boolean} [predicate]
  40. * An optional function called with each item to test for condition matching.
  41. * @param {T} [defaultValue] The default value emitted in case no valid value
  42. * was found on the source.
  43. * @return {Observable<T>} An Observable of the first item that matches the
  44. * condition.
  45. * @method first
  46. * @owner Observable
  47. */
  48. function first() {
  49. var args = [];
  50. for (var _i = 0; _i < arguments.length; _i++) {
  51. args[_i] = arguments[_i];
  52. }
  53. return operators_1.first.apply(void 0, args)(this);
  54. }
  55. exports.first = first;
  56. //# sourceMappingURL=first.js.map