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

Allows custom code to be run when messages arrive without affecting the observer.

function peek(nextAction:Function = null, completedAction:Function = null, 
    errorAction:Function = null) : IObservable.<sourceValueClass>

Where nextAction is function (value : sourceValueClass) : void

Where completedAction is function () : void

Where errorAction is function (error : Error) : void

Remarks

Calls the appropriate function (onNext, onCompleted, onError) when a message arrives.

If null is supplied for any function, no action will be performed for that message type.

The returned sequence completes if the source sequence completes

The returned sequence errors if the source sequence errors or if nextAction, completedAction or errorAction throw an error

Marble Diagrams

n(x) = nextAction
c() = completedAction
e(x) = errorAction

xs ──o─────o─────/
     ├n(x) ├n(x) ├c()
zs ──o─────o─────/

ws ──o─────o─────x
     ├n(x) ├n(x) ├e(x)
xs ──o─────o─────x

Scheduling

Unless specified, Scheduler.synchronous will be used to schedule the subscription to each source, including the first.

Return Value

IObservable.<sourceValueClass>

Examples

Observable.value(1)
    .peek(
        function(x:int):void { trace("[do] next - " + i.toString()); }
        function():void { trace("[do] completed"; }
    )
    .subscribe(
        function(x:int) : void { trace(x); },
        function() : void { trace("Completed"); },
        function(e:Error) : void { trace("Error: " + e.message); }
    );

    // Trace output is:
    // [do] next - 1
    // 1
    // [do] complete
    // Completed
Observable.range(1, 5)
    .timestamp()
    .peek(
        function(ts:TimeStamped):void { trace("log value @ " + ts.timestamp.toString() + " = " + i.toString()); }
    )
    .removeTimestamp()
    .select(function(i:int) : int { return i * 100; })
    .subscribe(
        function(x:int) : void { trace(x); },
        function() : void { trace("Completed"); }
    );

    // Trace output is:
    // log value @ [timestamp value] = 1
    // 100
    // log value @ [timestamp value] = 2
    // 200
    // log value @ [timestamp value] = 3
    // 300
    // log value @ [timestamp value] = 4
    // 400
    // log value @ [timestamp value] = 5
    // 500
    // Completed
Clone this wiki locally