@@ -17,9 +17,9 @@ public static IAsyncEnumerable<object> MakeCancelableAsyncEnumerable<T>(IAsyncEn
17
17
return new CancelableAsyncEnumerable < T > ( asyncEnumerable , cancellationToken ) ;
18
18
}
19
19
20
- public static IAsyncEnumerable < object > GetAsyncEnumerableFromChannel < T > ( ChannelReader < T > channel , CancellationToken cancellationToken = default )
20
+ public static IAsyncEnumerable < object > MakeCancelableAsyncEnumerableFromChannel < T > ( ChannelReader < T > channel , CancellationToken cancellationToken = default )
21
21
{
22
- return new ChannelAsyncEnumerable < T > ( channel , cancellationToken ) ;
22
+ return MakeCancelableAsyncEnumerable ( channel . ReadAllAsync ( ) , cancellationToken ) ;
23
23
}
24
24
25
25
/// <summary>Converts an IAsyncEnumerable of T to an IAsyncEnumerable of object.</summary>
@@ -36,15 +36,19 @@ public CancelableAsyncEnumerable(IAsyncEnumerable<T> asyncEnumerable, Cancellati
36
36
37
37
public IAsyncEnumerator < object > GetAsyncEnumerator ( CancellationToken cancellationToken = default )
38
38
{
39
+ // Assume that this will be iterated through with await foreach which always passes a default token.
40
+ // Instead use the token from the ctor.
39
41
Debug . Assert ( cancellationToken == default ) ;
40
- return new CancelableAsyncEnumerator ( _asyncEnumerable . GetAsyncEnumerator ( _cancellationToken ) ) ;
42
+
43
+ var enumeratorOfT = _asyncEnumerable . GetAsyncEnumerator ( _cancellationToken ) ;
44
+ return enumeratorOfT as IAsyncEnumerator < object > ?? new BoxedAsyncEnumerator ( enumeratorOfT ) ;
41
45
}
42
46
43
- private class CancelableAsyncEnumerator : IAsyncEnumerator < object >
47
+ private class BoxedAsyncEnumerator : IAsyncEnumerator < object >
44
48
{
45
49
private IAsyncEnumerator < T > _asyncEnumerator ;
46
50
47
- public CancelableAsyncEnumerator ( IAsyncEnumerator < T > asyncEnumerator )
51
+ public BoxedAsyncEnumerator ( IAsyncEnumerator < T > asyncEnumerator )
48
52
{
49
53
_asyncEnumerator = asyncEnumerator ;
50
54
}
@@ -62,74 +66,5 @@ public ValueTask DisposeAsync()
62
66
}
63
67
}
64
68
}
65
-
66
- /// <summary>Provides an IAsyncEnumerable of object for the data in a channel.</summary>
67
- private class ChannelAsyncEnumerable < T > : IAsyncEnumerable < object >
68
- {
69
- private readonly ChannelReader < T > _channel ;
70
- private readonly CancellationToken _cancellationToken ;
71
-
72
- public ChannelAsyncEnumerable ( ChannelReader < T > channel , CancellationToken cancellationToken )
73
- {
74
- _channel = channel ;
75
- _cancellationToken = cancellationToken ;
76
- }
77
-
78
- public IAsyncEnumerator < object > GetAsyncEnumerator ( CancellationToken cancellationToken = default )
79
- {
80
- Debug . Assert ( cancellationToken == default ) ;
81
- return new ChannelAsyncEnumerator ( _channel , _cancellationToken ) ;
82
- }
83
-
84
- private class ChannelAsyncEnumerator : IAsyncEnumerator < object >
85
- {
86
- /// <summary>The channel being enumerated.</summary>
87
- private readonly ChannelReader < T > _channel ;
88
- /// <summary>Cancellation token used to cancel the enumeration.</summary>
89
- private readonly CancellationToken _cancellationToken ;
90
- /// <summary>The current element of the enumeration.</summary>
91
- private T _current ;
92
-
93
- public ChannelAsyncEnumerator ( ChannelReader < T > channel , CancellationToken cancellationToken )
94
- {
95
- _channel = channel ;
96
- _cancellationToken = cancellationToken ;
97
- }
98
-
99
- public object Current => _current ;
100
-
101
- public ValueTask < bool > MoveNextAsync ( )
102
- {
103
- var result = _channel . ReadAsync ( _cancellationToken ) ;
104
-
105
- if ( result . IsCompletedSuccessfully )
106
- {
107
- _current = result . Result ;
108
- return new ValueTask < bool > ( true ) ;
109
- }
110
-
111
- return new ValueTask < bool > ( MoveNextAsyncAwaited ( result ) ) ;
112
- }
113
-
114
- private async Task < bool > MoveNextAsyncAwaited ( ValueTask < T > channelReadTask )
115
- {
116
- try
117
- {
118
- _current = await channelReadTask ;
119
- }
120
- catch ( ChannelClosedException ex ) when ( ex . InnerException == null )
121
- {
122
- return false ;
123
- }
124
-
125
- return true ;
126
- }
127
-
128
- public ValueTask DisposeAsync ( )
129
- {
130
- return default ;
131
- }
132
- }
133
- }
134
69
}
135
70
}
0 commit comments