forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncEnumerableExtensions.cs
More file actions
63 lines (51 loc) · 2.3 KB
/
Copy pathAsyncEnumerableExtensions.cs
File metadata and controls
63 lines (51 loc) · 2.3 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
[DebuggerStepThrough]
internal static class AsyncEnumerableExtensions
{
public static IAsyncEnumerable<TResult> Select<TSource, TResult>(
this IAsyncEnumerable<TSource> source,
Func<TSource, CancellationToken, Task<TResult>> selector)
=> new AsyncSelectEnumerable<TSource, TResult>(source, selector);
private class AsyncSelectEnumerable<TSource, TResult> : IAsyncEnumerable<TResult>
{
private readonly IAsyncEnumerable<TSource> _source;
private readonly Func<TSource, CancellationToken, Task<TResult>> _selector;
public AsyncSelectEnumerable(
IAsyncEnumerable<TSource> source,
Func<TSource, CancellationToken, Task<TResult>> selector)
{
_source = source;
_selector = selector;
}
public IAsyncEnumerator<TResult> GetEnumerator() => new AsyncSelectEnumerator(this);
private class AsyncSelectEnumerator : IAsyncEnumerator<TResult>
{
private readonly IAsyncEnumerator<TSource> _enumerator;
private readonly Func<TSource, CancellationToken, Task<TResult>> _selector;
public AsyncSelectEnumerator(AsyncSelectEnumerable<TSource, TResult> enumerable)
{
_enumerator = enumerable._source.GetEnumerator();
_selector = enumerable._selector;
}
public async Task<bool> MoveNext(CancellationToken cancellationToken)
{
if (!await _enumerator.MoveNext(cancellationToken))
{
return false;
}
Current = await _selector(_enumerator.Current, cancellationToken);
return true;
}
public TResult Current { get; private set; }
public void Dispose() => _enumerator.Dispose();
}
}
}
}