-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
FallbackIfEmpty.cs
91 lines (83 loc) · 3.72 KB
/
FallbackIfEmpty.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
namespace SuperLinq.Async;
public static partial class AsyncSuperEnumerable
{
/// <summary>
/// Returns the elements of a sequence, but if it is empty then
/// returns an alternate sequence from an array of values.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequences.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="fallback">The array that is returned as the alternate
/// sequence if <paramref name="source"/> is empty.</param>
/// <returns>
/// An <see cref="IAsyncEnumerable{T}"/> that containing fallback values
/// if <paramref name="source"/> is empty; otherwise, <paramref name="source"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="fallback"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<T> FallbackIfEmpty<T>(this IAsyncEnumerable<T> source, params T[] fallback)
{
return source.FallbackIfEmpty((IEnumerable<T>)fallback);
}
/// <summary>
/// Returns the elements of a sequence, but if it is empty then
/// returns an alternate sequence from an array of values.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequences.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="fallback">The array that is returned as the alternate
/// sequence if <paramref name="source"/> is empty.</param>
/// <returns>
/// An <see cref="IAsyncEnumerable{T}"/> that containing fallback values
/// if <paramref name="source"/> is empty; otherwise, <paramref name="source"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="fallback"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<T> FallbackIfEmpty<T>(this IAsyncEnumerable<T> source, IEnumerable<T> fallback)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(fallback);
return source.FallbackIfEmpty(fallback.ToAsyncEnumerable());
}
/// <summary>
/// Returns the elements of a sequence, but if it is empty then
/// returns an alternate sequence of values.
/// </summary>
/// <typeparam name="T">The type of the elements in the sequences.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="fallback">The alternate sequence that is returned
/// if <paramref name="source"/> is empty.</param>
/// <returns>
/// An <see cref="IAsyncEnumerable{T}"/> that containing fallback values
/// if <paramref name="source"/> is empty; otherwise, <paramref name="source"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="fallback"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<T> FallbackIfEmpty<T>(this IAsyncEnumerable<T> source, IAsyncEnumerable<T> fallback)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(fallback);
return Core(source, fallback);
static async IAsyncEnumerable<T> Core(
IAsyncEnumerable<T> source,
IAsyncEnumerable<T> fallback,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken))
{
if (await e.MoveNextAsync())
{
do
{
yield return e.Current;
}
while (await e.MoveNextAsync());
yield break;
}
}
await foreach (var item in fallback.WithCancellation(cancellationToken).ConfigureAwait(false))
yield return item;
}
}
}