Skip to content
richardszalay edited this page Sep 14, 2010 · 14 revisions

Creates a custom observable sequence that is controlled by methods supplied as arguments.

static function generate(type : Class, initialState : Object, predicate : Function, 
    resultMap : Function, iterate : Function, scheduler : IScheduler = null)

Where type is the type of the values that will be emitted by the observable sequence. That is, the type of the return value of resultMap but not necessarily the type of initialState.

Where predicate is function(state : stateType) : Boolean

Where resultMap is function(state : stateType) : type

Where iterate is function(state : stateType) : stateType

Remarks

Starting with initialState, values go through the following cycle:

  1. The current state is passed to predicate
    • If predicate returns false, the sequence completes
  2. The current state is passed to resultMap and the return value is emitted as a value
  3. 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.

Marble Diagrams

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────────────/

Scheduling

Unless specified, this operator uses Scheduler.synchronous.

Return Value

IObservable.<type>

Examples

Observable.generate(String, 5,
        function(s:int):Boolean { return s <= 20; },
        function(s:int):String { return s.toString() + "!"; },
        function(s:int):int { return s + 5; }
    )
    .subscribe(
        function(value : int) : void { trace(value); },
        function():void { trace("Completed"); }
    );

    // Trace output is:
    // 5!
    // 10!
    // 15!
    // 20!
    // Completed
Clone this wiki locally