forked from Dasync/AsyncEnumerable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncEnumerable.cs
122 lines (110 loc) · 5.02 KB
/
AsyncEnumerable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Collections.Async
{
/// <summary>
/// Helps to enumerate items in a collection asynchronously
/// </summary>
/// <example>
/// <code>
/// IAsyncEnumerable<int> ProduceNumbers(int start, int end)
/// {
/// return new AsyncEnumerable<int>(async yield => {
/// for (int number = start; number <= end; number++)
/// await yield.ReturnAsync(number);
/// });
/// }
///
/// async Task ConsumeAsync()
/// {
/// var asyncEnumerableCollection = ProduceNumbers(start: 1, end: 10);
/// await asyncEnumerableCollection.ForEachAsync(async number => {
/// await Console.Out.WriteLineAsync(number)
/// });
/// }
///
/// // It's backward compatible with synchronous enumeration, but gives no benefits
/// void ConsumeSync()
/// {
/// var enumerableCollection = ProduceNumbers(start: 1, end: 10);
/// foreach (var number in enumerableCollection) {
/// Console.Out.WriteLine(number)
/// };
/// }
/// </code>
/// </example>
public sealed class AsyncEnumerable<T> : AsyncEnumerable, IAsyncEnumerable<T>
{
private Func<AsyncEnumerator<T>.Yield, Task> _enumerationFunction;
private bool _oneTimeUse;
/// <summary>
/// A pre-cached empty collection
/// </summary>
public readonly static IAsyncEnumerable<T> Empty = new AsyncEnumerable<T>(yield => CompletedTask);
/// <summary>
/// Constructor
/// </summary>
/// <param name="enumerationFunction">A function that enumerates items in a collection asynchronously</param>
public AsyncEnumerable(Func<AsyncEnumerator<T>.Yield, Task> enumerationFunction)
: this(enumerationFunction, oneTimeUse: false)
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="enumerationFunction">A function that enumerates items in a collection asynchronously</param>
/// <param name="oneTimeUse">When True the enumeration can be performed once only and Reset method is not allowed</param>
public AsyncEnumerable(Func<AsyncEnumerator<T>.Yield, Task> enumerationFunction, bool oneTimeUse)
{
_enumerationFunction = enumerationFunction;
_oneTimeUse = oneTimeUse;
}
/// <summary>
/// Creates an enumerator that iterates through a collection asynchronously
/// </summary>
/// <param name="cancellationToken">A cancellation token to cancel creation of the enumerator in case if it takes a lot of time</param>
/// <returns>Returns a task with the created enumerator as result on completion</returns>
public Task<IAsyncEnumerator<T>> GetAsyncEnumeratorAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var enumerator = new AsyncEnumerator<T>(_enumerationFunction, _oneTimeUse);
return Task.FromResult<IAsyncEnumerator<T>>(enumerator);
}
/// <summary>
/// Creates an enumerator that iterates through a collection asynchronously
/// </summary>
/// <param name="cancellationToken">A cancellation token to cancel creation of the enumerator in case if it takes a lot of time</param>
/// <returns>Returns a task with the created enumerator as result on completion</returns>
Task<IAsyncEnumerator> IAsyncEnumerable.GetAsyncEnumeratorAsync(CancellationToken cancellationToken) => GetAsyncEnumeratorAsync(cancellationToken).ContinueWith<IAsyncEnumerator>(task => task.Result);
/// <summary>
/// Returns an enumerator that iterates through the collection
/// </summary>
/// <returns>An instance of enumerator</returns>
public IEnumerator<T> GetEnumerator() => GetAsyncEnumeratorAsync().ConfigureAwait(false).GetAwaiter().GetResult();
/// <summary>
/// Returns an enumerator that iterates through the collection
/// </summary>
/// <returns>An instance of enumerator</returns>
IEnumerator IEnumerable.GetEnumerator() => GetAsyncEnumeratorAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
/// <summary>
/// Base abstract class that implements <see cref="IAsyncEnumerable"/>.
/// Use concrete implementation <see cref="AsyncEnumerable{T}"/>.
/// </summary>
public abstract class AsyncEnumerable : IAsyncEnumerable
{
/// <summary>
/// Returns pre-cached empty collection
/// </summary>
public static IAsyncEnumerable<T> Empty<T>() => AsyncEnumerable<T>.Empty;
internal static readonly Task CompletedTask = Task.FromResult(0);
Task<IAsyncEnumerator> IAsyncEnumerable.GetAsyncEnumeratorAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}