takeUntil.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { OuterSubscriber } from '../OuterSubscriber';
  2. import { subscribeToResult } from '../util/subscribeToResult';
  3. /**
  4. * Emits the values emitted by the source Observable until a `notifier`
  5. * Observable emits a value.
  6. *
  7. * <span class="informal">Lets values pass until a second Observable,
  8. * `notifier`, emits something. Then, it completes.</span>
  9. *
  10. * <img src="./img/takeUntil.png" width="100%">
  11. *
  12. * `takeUntil` subscribes and begins mirroring the source Observable. It also
  13. * monitors a second Observable, `notifier` that you provide. If the `notifier`
  14. * emits a value or a complete notification, the output Observable stops
  15. * mirroring the source Observable and completes.
  16. *
  17. * @example <caption>Tick every second until the first click happens</caption>
  18. * var interval = Rx.Observable.interval(1000);
  19. * var clicks = Rx.Observable.fromEvent(document, 'click');
  20. * var result = interval.takeUntil(clicks);
  21. * result.subscribe(x => console.log(x));
  22. *
  23. * @see {@link take}
  24. * @see {@link takeLast}
  25. * @see {@link takeWhile}
  26. * @see {@link skip}
  27. *
  28. * @param {Observable} notifier The Observable whose first emitted value will
  29. * cause the output Observable of `takeUntil` to stop emitting values from the
  30. * source Observable.
  31. * @return {Observable<T>} An Observable that emits the values from the source
  32. * Observable until such time as `notifier` emits its first value.
  33. * @method takeUntil
  34. * @owner Observable
  35. */
  36. export function takeUntil(notifier) {
  37. return (source) => source.lift(new TakeUntilOperator(notifier));
  38. }
  39. class TakeUntilOperator {
  40. constructor(notifier) {
  41. this.notifier = notifier;
  42. }
  43. call(subscriber, source) {
  44. return source.subscribe(new TakeUntilSubscriber(subscriber, this.notifier));
  45. }
  46. }
  47. /**
  48. * We need this JSDoc comment for affecting ESDoc.
  49. * @ignore
  50. * @extends {Ignored}
  51. */
  52. class TakeUntilSubscriber extends OuterSubscriber {
  53. constructor(destination, notifier) {
  54. super(destination);
  55. this.notifier = notifier;
  56. this.add(subscribeToResult(this, notifier));
  57. }
  58. notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  59. this.complete();
  60. }
  61. notifyComplete() {
  62. // noop
  63. }
  64. }
  65. //# sourceMappingURL=takeUntil.js.map