-
Notifications
You must be signed in to change notification settings - Fork 8
generate
richardszalay edited this page May 6, 2011
·
14 revisions
Creates a custom observable sequence that is controlled by methods supplied as arguments.
static function generate(initialState : Object, predicate : Function, iterate : Function, resultMap : Function, scheduler : IScheduler = null) : IObservable
Where predicate is function(state : stateClass) : Boolean
Where iterate is function(state : stateClass) : stateClass
Where resultMap is function(state : stateClass) : TResult
Starting with initialState, values go through the following cycle:
- The current state is passed to predicate
- If predicate returns false, the sequence completes
- The current state is passed to resultMap and the return value is emitted as a value
- The current state is passed to iterate and the return value replaces the current state
The returned sequence completes when predicate returns false
The returned sequence raises an error if iterate, predicate or resultMap throw an error.
p = predicate map = resultMap it = iterate s = state
state ────p(s)──it(s)──p(s)──it(s)──p(s) true true false │ │ │ map(s) map(s) │ │ │ │ output ─────o────────────o────────────/
Unless specified, this operator uses Scheduler.synchronous
.
IObservable.<valueClass>
Observable.generate(5,
function(state:int):Boolean { return state <= 20; },
function(state:int):int { return state + 5; },
function(state:int):String { return state.toString() + "!"; }
)
.subscribe(
function(value : int) : void { trace(value); },
function():void { trace("Completed"); }
);
// Trace output is:
// 5!
// 10!
// 15!
// 20!
// Completed