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

Takes multiple source sequences and returns values from the first sequence to emit a value. That is, it resolves a set of ambiguous sequences.

static function amb(sources : Array.< IObservable.<T> >) : IObservable.<T>

Remarks

When amb() is subscribed to, each sequence in sources is subscribed to. When any of the the sequences emits a value, the other sequences will be unsubscribed from. Values from the active sequence will then be used until it completes (or errors).

The returned sequence completes when the active source sequence completes, or if all sequences complete without a value.

The returned sequence raises an error if any of the sequences raises an error.

Marble Diagrams

ws, xs, yz = sources
zs = output

ws ─────│
        │
xs ─────o──o──o──/
        │  │  │  │
ys ─────│  │  │  │
        │  │  │  │
zs ─────o──o──o──/

ws ──────────/
xs ───────────────/
ys ─────────────/ │
zs ───────────────/

ws ──────────│
             │
xs ──────────x
             │
ys ──────────│
             │
zs ──────────x

Return Value

IObservable.<T>

Examples

var sourceA : IObservable = Observable.interval(500)
    .select(String, function(i:int) : String { return "A - " + i.toString(); });

var sourceB : IObservable = Observable.interval(1000)
    .select(String, function(i:int) : String { return "B - " + i.toString(); });

Observable.amb([sourceA, sourceB])
    .subscribe(
        function(value : String) : void { trace(value); },
        function():void { trace("Completed"); }
    );

    // Trace output is:
    // A - 0 (sourceB is unsubscribed from at this point)
    // A - 1
    // A - 2
    // A - 3
    // ...
Clone this wiki locally