3

The below code will run for all sequences, I want the Observable to stop raising events after the condition is true.

private IObservable<string> EnsureIndexWithCounter(string index)
        {
            return Observable.Range(0, 5)
                             .SelectMany(p => IncrementCounterIfIndexExistsAsync(index, p)
                                                    .ToObservable()
                                                    .RepeatUntil(x => !x.Item1, 5))
                             .TakeWhile(p => !p.Item1)
                             .Select(p => p.Item2);
        }

        // Will be invoked 4 times, should be invoked as long the Item1 of the return tuple is true
        private async Task<Tuple<bool, string>> IncrementCounterIfIndexExistsAsync(string index, int counter)
        {
            var existsResponse = await Client.IndexExistsAsync(new IndexExistsRequest(index)).ConfigureAwait(false);
            var newCounter = existsResponse.Exists ? ++counter : counter;
            return Tuple.Create(existsResponse.Exists, $"{index}_{newCounter}");
        }
George Taskos
  • 8,324
  • 18
  • 82
  • 147
  • I'm not sure if i understand your code correctly, but why don't you just write the code as simple as https://dotnetfiddle.net/pKSWwO (something similar like that) – Khanh TO Oct 07 '17 at 04:03
  • This will not work, I need to test the index_counter. Also you can't test the Task so SelectMany is needed to await for the value. – George Taskos Oct 07 '17 at 17:22

0 Answers0