forked from Dasync/AsyncEnumerable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncEnumerableWrapper.cs
28 lines (21 loc) · 1.12 KB
/
AsyncEnumerableWrapper.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
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Collections.Async
{
internal sealed class AsyncEnumerableWrapper<T> : IAsyncEnumerable<T>
{
private IEnumerable<T> _enumerable;
private bool _runSynchronously;
public AsyncEnumerableWrapper(IEnumerable<T> enumerable, bool runSynchronously)
{
_enumerable = enumerable;
_runSynchronously = runSynchronously;
}
public Task<IAsyncEnumerator<T>> GetAsyncEnumeratorAsync(CancellationToken cancellationToken = default(CancellationToken)) => Task.FromResult(CreateAsyncEnumerator());
Task<IAsyncEnumerator> IAsyncEnumerable.GetAsyncEnumeratorAsync(CancellationToken cancellationToken) => Task.FromResult<IAsyncEnumerator>(CreateAsyncEnumerator());
public IEnumerator<T> GetEnumerator() => _enumerable.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _enumerable.GetEnumerator();
private IAsyncEnumerator<T> CreateAsyncEnumerator() => new AsyncEnumeratorWrapper<T>(_enumerable.GetEnumerator(), _runSynchronously);
}
}