Подтвердить что ты не робот

RxJS, если другие управляющие структуры с операторами Observables

Можно ли моделировать if/else структуры управления через RxJS-операторы. Насколько я понял, мы могли бы использовать Observable.filter() для моделирования ветки IF, но я не уверен, что мы моделируем ветвь ELSE через любой оператор Observable.

4b9b3361

Ответ 1

Есть несколько операторов, которые вы могли бы использовать для эмуляции:

В целях, скорее всего, того, что вы просите fo

раздел

//Returns an array containing two Observables
//One whose elements pass the filter, and another whose elements don't

var items = observableSource.partition((x) => x % 2 == 0);

var evens = items[0];
var odds = items[1];

//Only even numbers
evens.subscribe();

//Only odd numbers
odds.subscribe();

groupBy

//Uses a key selector and equality comparer to generate an Observable of GroupedObservables
observableSource.groupBy((value) => value % 2, (value) => value)
  .subscribe(groupedObservable => {
    groupedObservable.subscribe(groupedObservable.key ? oddObserver : evenObserver);
  });

, если

//Propagates one of the sources based on a particular condition
//!!Only one Observable will be subscribed to!!
Rx.Observable.if(() => value > 5, Rx.Observable.just(5), Rx.Observable.from([1,2, 3]))

case (доступно только в RxJS 4)

//Similar to `if` but it takes an object and only propagates based on key matching
//It takes an optional argument if none of the items match
//!!Only one Observable will be subscribed to!!
Rx.Observable.case(() => "blah",
{
  blah : //..Observable,
  foo : //..Another Observable,
  bar : //..Yet another
}, Rx.Observable.throw("Should have matched!"))