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

Emits the first value from a source sequence as long it is the only value in the sequence. Emits the default value if the sequence is empty.

function singleOrDefault() : IObservable.<T>

Remarks

The returned sequence completes when the source sequence completes with zero or one value.

The returned sequence errors when the source sequences errors or if the source sequence emits more than one value.

Marble Diagrams

xs = source
ys = output

xs  ──o─────────────/
      │
      └─────────────┐
                    │
ys  ────────────────o/


xs  ────────────────/
                    │
                    │
                    │
ys  ────────────────o
               defaultValue


xs  ──o──────────o
                 │
                 │
                 │
ys  ─────────────x

Return Value

IObservable.<T>

Examples

var source : IObservable = Observable.empty()
    .singleOrDefault();

source.subscribe(
    function(value : int) : void { trace(value; },
    function() : void { trace("Completed!"); },
    function(err : Error) : void { trace("Error: " + err.message); }
    );

// Trace output is:
// 0
// Completed!
//
Clone this wiki locally