-
Notifications
You must be signed in to change notification settings - Fork 735
/
Never.cs
58 lines (46 loc) · 2.11 KB
/
Never.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
public static partial class AsyncEnumerableEx
{
public static IAsyncEnumerable<TValue> Never<TValue>() => NeverAsyncEnumerable<TValue>.Instance;
private sealed class NeverAsyncEnumerable<TValue> : IAsyncEnumerable<TValue>
{
internal static readonly NeverAsyncEnumerable<TValue> Instance = new NeverAsyncEnumerable<TValue>();
public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
return new NeverAsyncEnumerator(cancellationToken);
}
private sealed class NeverAsyncEnumerator : IAsyncEnumerator<TValue>
{
private readonly CancellationToken _token;
private CancellationTokenRegistration _registration;
private bool _once;
public NeverAsyncEnumerator(CancellationToken token) => _token = token;
public TValue Current => throw new InvalidOperationException();
public ValueTask DisposeAsync()
{
_registration.Dispose();
return default;
}
public ValueTask<bool> MoveNextAsync()
{
if (_once)
{
return new ValueTask<bool>(false);
}
_once = true;
var task = new TaskCompletionSource<bool>();
_registration = _token.Register(state => ((TaskCompletionSource<bool>)state!).SetCanceled(), task);
return new ValueTask<bool>(task.Task);
}
}
}
}
}