takeUntil

연산자(operator) 정의: takeUntil(notifier: Observable): Observable

Emit values until provided observable emits.


:bulb: If you only need a specific number of values, try take!


Examples

Example 1: Take values until timer emits

( jsBin | jsFiddle )

//emit value every 1s
const source = Rx.Observable.interval(1000);
//after 5 seconds, emit value
const timer = Rx.Observable.timer(5000);
//when timer emits after 5s, complete source
const example = source.takeUntil(timer);
//output: 0,1,2,3
const subscribe = example.subscribe(val => console.log(val));
Example 2: Take the first 5 even numbers

( jsBin | jsFiddle )

//emit value every 1s
const source = Rx.Observable.interval(1000);
//is number even?
const isEven = val => val % 2 === 0;
//only allow values that are even
const evenSource = source.filter(isEven);
//keep a running total of the number of even numbers out
const evenNumberCount = evenSource
    .scan((acc, _) => acc + 1, 0);
//do not emit until 5 even numbers have been emitted
const fiveEvenNumbers = evenNumberCount.filter(val => val > 5);

const example = evenSource
    //also give me the current even number count for display
  .withLatestFrom(evenNumberCount)
    .map(([val, count]) => `Even number (${count}) : ${val}`)
  //when five even numbers have been emitted, complete source observable
  .takeUntil(fiveEvenNumbers);
/*
    Even number (1) : 0,
    Even number (2) : 2
    Even number (3) : 4
    Even number (4) : 6
    Even number (5) : 8
*/
const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/takeUntil.ts

results matching ""

    No results matching ""