Skip to content
richardszalay edited this page May 20, 2011 · 7 revisions

Defers selection of one of two sequences until the sequence is subscribed to

function ifElse(predicate : Function, 
    ifTrue : IObservable.<TResult>, ifFalse : IObservable.<TResult>) : IObservable.<TResult>

Where predicate is function () : Boolean

Remarks

When a new subscription occurs, predicate will be called with no arguments. If predicate returns true, the ifTrue sequence will be subscribed to, otherwise the ifFalse sequence will be subscribed to.

The returned sequence completes when the approprate sequence (ifTrue or ifFalse) completes.

The returned sequence errors if the source sequence errors or if predicate throws an error

Marble Diagrams

p() = predicate

xs     ─┐
       p()
       true
ifTrue  └───o─────o──/
            │     │  │
zs     ─────o─────o──/

xs     ─┐
       p()
       false
ifFalse  └───o─────o──/
            │     │  │
zs     ─────o─────o──/

Return Value

IObservable.<TResult>

Examples

var toggle : Boolean = false;

var source : IObservable = Observable.defer(int, function() : IObservable
    {
        toggle = !toggle;

        return toggle;
    }, Observable.value(50), Observable.value(100));

var observer : IObserver = Observer.create(
        function(x:int) : void { trace(x); },
        function() : void { trace("Completed"); }
    );

source.subscribeWith(observer);
source.subscribeWith(observer);
source.subscribeWith(observer);

// Trace output is:
// 50
// Completed
// 100
// Completed
// 50
// Completed
Clone this wiki locally