Observable.TakeWhile allows you to run a sequence as long as a condition is true (using a delegate so we can perform computations on the actual sequence objects), but it's checking this condition BEFORE each element. How can I perform the same check but AFTER each element?
The following code demonstrates the problem
void RunIt()
{
List<SomeCommand> listOfCommands = new List<SomeCommand>();
listOfCommands.Add(new SomeCommand { CurrentIndex = 1, TotalCount = 3 });
listOfCommands.Add(new SomeCommand { CurrentIndex = 2, TotalCount = 3 });
listOfCommands.Add(new SomeCommand { CurrentIndex = 3, TotalCount = 3 });
var obs = listOfCommands.ToObservable().TakeWhile(c => c.CurrentIndex != c.TotalCount);
obs.Subscribe(x =>
{
Debug.WriteLine("{0} of {1}", x.CurrentIndex, x.TotalCount);
});
}
class SomeCommand
{
public int CurrentIndex;
public int TotalCount;
}
This outputs
1 of 3
2 of 3
I can't get the third element
Looking at this example, you may think all I have to do is change my condition like so -
var obs = listOfCommands.ToObservable().TakeWhile(c => c.CurrentIndex <= c.TotalCount);
But then the observable will never complete (because in my real world code, the stream doesn't end after those three commands)