Skip to content

Instantly share code, notes, and snippets.

@trxcllnt
Created April 15, 2013 11:21
Show Gist options
  • Save trxcllnt/5387420 to your computer and use it in GitHub Desktop.
Save trxcllnt/5387420 to your computer and use it in GitHub Desktop.
concatMany transforms an IEnumerable to an IObservable by selecting an Observable for each Enumerable value, waiting until the Observable is complete, then moving on to the next value in the Enumerable.
function concatMany(enumerable:IEnumerable, selector:Function):IObservable {
return Observable.createWithCancelable(function(observer:IObserver):ICancelable {
const iterator:IEnumerator = enumerable.getEnumerator();
const subscriptions:CompositeCancelable = new CompositeCancelable();
var schedule:Function = function():void {
subscriptions.add(Scheduler.scheduleRecursive(Scheduler.defaultScheduler, function(reschedule:Function):void {
schedule = reschedule;
const item:Object = iterator.current;
const obs:IObservable = selector(item);
const completed:Function = function():void {
subscriptions.remove(subscription);
recurse();
};
const subscription:ICancelable = obs.subscribe(observer.onNext, completed, observer.onError);
subscriptions.add(subscription);
}));
};
const recurse:Function = ifElse(
iterator.moveNext,
function():void { schedule(); },
observer.onCompleted
);
recurse();
return subscriptions;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment