AsyncEnumerableEx.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Func<Task> dispose)
  12. {
  13. var self = new AnonymousAsyncIterator<T>(
  14. async () =>
  15. {
  16. var tcs = new TaskCompletionSource<bool>();
  17. var stop = new Action(() => tcs.TrySetCanceled());
  18. return await moveNext(tcs).ConfigureAwait(false);
  19. },
  20. current,
  21. dispose
  22. );
  23. return self;
  24. }
  25. private sealed class AnonymousAsyncIterator<T> : AsyncIterator<T>
  26. {
  27. private readonly Func<T> currentFunc;
  28. private readonly Func<Task> dispose;
  29. private readonly Func<Task<bool>> moveNext;
  30. public AnonymousAsyncIterator(Func<Task<bool>> moveNext, Func<T> currentFunc, Func<Task> dispose)
  31. {
  32. Debug.Assert(moveNext != null);
  33. this.moveNext = moveNext;
  34. this.currentFunc = currentFunc;
  35. this.dispose = dispose;
  36. // Explicit call to initialize enumerator mode
  37. GetAsyncEnumerator();
  38. }
  39. public override AsyncIterator<T> Clone()
  40. {
  41. throw new NotSupportedException("AnonymousAsyncIterator cannot be cloned. It is only intended for use as an iterator.");
  42. }
  43. public override async Task DisposeAsync()
  44. {
  45. if (dispose != null)
  46. {
  47. await dispose().ConfigureAwait(false);
  48. }
  49. await base.DisposeAsync().ConfigureAwait(false);
  50. }
  51. protected override async Task<bool> MoveNextCore()
  52. {
  53. switch (state)
  54. {
  55. case AsyncIteratorState.Allocated:
  56. state = AsyncIteratorState.Iterating;
  57. goto case AsyncIteratorState.Iterating;
  58. case AsyncIteratorState.Iterating:
  59. if (await moveNext().ConfigureAwait(false))
  60. {
  61. current = currentFunc();
  62. return true;
  63. }
  64. await DisposeAsync().ConfigureAwait(false);
  65. break;
  66. }
  67. return false;
  68. }
  69. }
  70. }
  71. }