| 1 |
import { MonoTypeOperatorFunction, ObservableInput } from '../types'; |
| 2 |
import { operate } from '../util/lift'; |
| 3 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 4 |
import { innerFrom } from '../observable/innerFrom'; |
| 5 |
import { noop } from '../util/noop'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Emits the values emitted by the source Observable until a `notifier` |
| 9 |
* Observable emits a value. |
| 10 |
* |
| 11 |
* <span class="informal">Lets values pass until a second Observable, |
| 12 |
* `notifier`, emits a value. Then, it completes.</span> |
| 13 |
* |
| 14 |
*  |
| 15 |
* |
| 16 |
* `takeUntil` subscribes and begins mirroring the source Observable. It also |
| 17 |
* monitors a second Observable, `notifier` that you provide. If the `notifier` |
| 18 |
* emits a value, the output Observable stops mirroring the source Observable |
| 19 |
* and completes. If the `notifier` doesn't emit any value and completes |
| 20 |
* then `takeUntil` will pass all values. |
| 21 |
* |
| 22 |
* ## Example |
| 23 |
* |
| 24 |
* Tick every second until the first click happens |
| 25 |
* |
| 26 |
* ```ts |
| 27 |
* import { interval, fromEvent, takeUntil } from 'rxjs'; |
| 28 |
* |
| 29 |
* const source = interval(1000); |
| 30 |
* const clicks = fromEvent(document, 'click'); |
| 31 |
* const result = source.pipe(takeUntil(clicks)); |
| 32 |
* result.subscribe(x => console.log(x)); |
| 33 |
* ``` |
| 34 |
* |
| 35 |
* @see {@link take} |
| 36 |
* @see {@link takeLast} |
| 37 |
* @see {@link takeWhile} |
| 38 |
* @see {@link skip} |
| 39 |
* |
| 40 |
* @param notifier The `ObservableInput` whose first emitted value will cause the output |
| 41 |
* Observable of `takeUntil` to stop emitting values from the source Observable. |
| 42 |
* @return A function that returns an Observable that emits the values from the |
| 43 |
* source Observable until `notifier` emits its first value. |
| 44 |
*/ |
| 45 |
export function takeUntil<T>(notifier: ObservableInput<any>): MonoTypeOperatorFunction<T> { |
| 46 |
return operate((source, subscriber) => { |
| 47 |
innerFrom(notifier).subscribe(createOperatorSubscriber(subscriber, () => subscriber.complete(), noop)); |
| 48 |
!subscriber.closed && source.subscribe(subscriber); |
| 49 |
}); |
| 50 |
} |