Range.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<int> Range(int start, int count) => Range(start, count, TaskPoolAsyncScheduler.Default);
  10. public static IAsyncObservable<int> Range(int start, int count, IAsyncScheduler scheduler)
  11. {
  12. var max = ((long)start) + count - 1;
  13. if (count < 0 || max > int.MaxValue)
  14. throw new ArgumentOutOfRangeException(nameof(count));
  15. if (scheduler == null)
  16. throw new ArgumentNullException(nameof(scheduler));
  17. return Create<int>(observer => scheduler.ScheduleAsync(async ct =>
  18. {
  19. ct.ThrowIfCancellationRequested();
  20. for (int i = start, end = start + count - 1; i <= end && !ct.IsCancellationRequested; i++)
  21. {
  22. await observer.OnNextAsync(i).RendezVous(scheduler);
  23. }
  24. ct.ThrowIfCancellationRequested();
  25. await observer.OnCompletedAsync().RendezVous(scheduler);
  26. }));
  27. }
  28. }
  29. }