Skip to content
viktorot edited this page May 19, 2015 · 10 revisions

Creates a custom observable sequence

static function create(subscribeFunc : Function) : IObservable.<*>

Where subscribeFunc is function (observer : IObserver) : Function

Remarks

When the sequence is subscribed to, subscribeFunc will be called with the observer being subscribed to. The function may call any combination of onNext, onComplete and onError at any point (even asynchronously). However, out of order calls (eg. onComplete → onNext) will be ignored.

When the sequence completes, errors or is unsubscribed from, the function returned by subscribeFunc will be called. If the asynchronous operation is cancelable, it should be done so here.

If subscribeFunc returns null, an ArgumentError will be thrown. If there is no functionality to cleanup, consider using createWithCancelable and returning Cancelable.empty.

The sequence completes when subscribeFunc calls onCompleted on the IObservable passed to it

The sequence errors when subscribeFunc calls onError on the IObservable passed to it

Return Value

IObservable.<*>

Examples

var source : IObservable = Observable.create(
    function(observer : IObserver) : Function
    {
        observer.onNext(0);
        observer.onNext(1);
        observer.onNext(2);
        observer.onCompleted();

        return function():void
        {
            // If this was cancelable, it would occur here
        }
    });

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

// Trace output:
// 0
// 1
// 2
// Completed!
Clone this wiki locally