Skip to content

createWithCancelable

richardszalay edited this page May 20, 2011 · 5 revisions

Creates a custom observable sequence that uses a cancelable resource

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

Where subscribeFunc is function (observer : IObserver) : ICancelable

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 ICancelable returned by subscribeFunc will be canceled.

If subscribeFunc returns null, an ArgumentError will be thrown. If there is no functionality to cleanup, return 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.createWithCancelable(
    function(observer : IObserver) : ICancelable
    {
        var scheduler : IScheduler = Scheduler.asynchronous;

        var value : int = 0;

        return Scheduler.scheduleRecursive(scheduler, function(recurse : Function) : void
        {
            observer.onNext(value);
            value++;
        });
    });

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

// Trace output:
// 0
// 1
// 2
// 3
// ... (until canceled)
Clone this wiki locally