Skip to content

Revert "Remove CancelableEnumerator (#10099)" #10129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/SignalR/common/Shared/AsyncEnumerableAdapters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,37 @@ public IAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellati
{
((CancellationTokenSource)ctsState).Cancel();
}, _cts);

return new CancelableEnumerator<TResult>(_asyncEnumerable.GetAsyncEnumerator(), registration);
}

return enumerator;
}

private class CancelableEnumerator<T> : IAsyncEnumerator<T>
{
private IAsyncEnumerator<T> _asyncEnumerator;
private readonly CancellationTokenRegistration _cancellationTokenRegistration;

public T Current => (T)_asyncEnumerator.Current;

public CancelableEnumerator(IAsyncEnumerator<T> asyncEnumerator, CancellationTokenRegistration registration)
{
_asyncEnumerator = asyncEnumerator;
_cancellationTokenRegistration = registration;
}

public ValueTask<bool> MoveNextAsync()
{
return _asyncEnumerator.MoveNextAsync();
}

public ValueTask DisposeAsync()
{
_cancellationTokenRegistration.Dispose();
return _asyncEnumerator.DisposeAsync();
}
}
}

/// <summary>Converts an IAsyncEnumerable of T to an IAsyncEnumerable of object.</summary>
Expand All @@ -71,6 +98,10 @@ public CancelableAsyncEnumerable(IAsyncEnumerable<T> asyncEnumerable, Cancellati

public IAsyncEnumerator<object> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
// Assume that this will be iterated through with await foreach which always passes a default token.
// Instead use the token from the ctor.
Debug.Assert(cancellationToken == default);

var enumeratorOfT = _asyncEnumerable.GetAsyncEnumerator(_cancellationToken);
return enumeratorOfT as IAsyncEnumerator<object> ?? new BoxedAsyncEnumerator(enumeratorOfT);
}
Expand Down
5 changes: 3 additions & 2 deletions src/SignalR/server/Core/src/Internal/DefaultHubDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,13 @@ private async Task StreamResultsAsync(string invocationId, HubConnectionContext
IHubActivator<THub> hubActivator, THub hub, CancellationTokenSource streamCts, HubMethodInvocationMessage hubMethodInvocationMessage)
{
string error = null;

try
{
await foreach(var item in enumerable.WithCancellation(streamCts.Token))
await foreach (var streamItem in enumerable)
{
// Send the stream item
await connection.WriteAsync(new StreamItemMessage(invocationId, item));
await connection.WriteAsync(new StreamItemMessage(invocationId, streamItem));
}
}
catch (ChannelClosedException ex)
Expand Down