Expand.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Expand<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TSource>> selector)
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (selector == null)
  18. throw new ArgumentNullException(nameof(selector));
  19. return Create(() =>
  20. {
  21. var e = default(IAsyncEnumerator<TSource>);
  22. var cts = new CancellationTokenDisposable();
  23. var a = new AssignableDisposable();
  24. var d = Disposable.Create(cts, a);
  25. var queue = new Queue<IAsyncEnumerable<TSource>>();
  26. queue.Enqueue(source);
  27. var current = default(TSource);
  28. var f = default(Func<CancellationToken, Task<bool>>);
  29. f = async ct =>
  30. {
  31. if (e == null)
  32. {
  33. if (queue.Count > 0)
  34. {
  35. var src = queue.Dequeue();
  36. e = src.GetEnumerator();
  37. a.Disposable = e;
  38. return await f(ct)
  39. .ConfigureAwait(false);
  40. }
  41. return false;
  42. }
  43. if (await e.MoveNext(ct)
  44. .ConfigureAwait(false))
  45. {
  46. var item = e.Current;
  47. var next = selector(item);
  48. queue.Enqueue(next);
  49. current = item;
  50. return true;
  51. }
  52. e = null;
  53. return await f(ct)
  54. .ConfigureAwait(false);
  55. };
  56. return Create(
  57. f,
  58. () => current,
  59. d.Dispose,
  60. e
  61. );
  62. });
  63. }
  64. }
  65. }