I have an interface like this:
interface IProcessor{
IObservable<Item> Process(Item item);
}
I have an array of workers:
IProcessor[] _workers = ....
I want to pass an item through all the workers:
var ret = Observable.Return(item);
for (var i = 0; i < _workers.Length; i++)
{
int index = i;
ret = ret
.SelectMany(r => _workers[index].Process(r))
;
}
return ret;
I’m not too happy with how this looks — is there a cleaner way?
This works for me:
Please keep in mind that this kind of aggregation of observables – both in your question and in my answer – can cause memory issues (i.e. stack overflow) quickly. In my tests I could get 400 workers working, but 500 caused a crash.
You’re better off changing your
IProcessorto not use observables and implement your observable like this:With this approach I can get over 20,000 nested workers before a stack overflow and the results are almost instantaneous up to that level.