peek - richardszalay/raix GitHub Wiki
ο»Ώ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
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
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βββββxUnless specified, Scheduler.synchronous will be used to schedule the subscription to each source, including the first.
IObservable.<sourceValueClass>
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
// CompletedObservable.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