diff --git a/src/UniTask.NetCore/Linq/Never.cs b/src/UniTask.NetCore/Linq/Never.cs new file mode 100644 index 0000000..88a1e00 --- /dev/null +++ b/src/UniTask.NetCore/Linq/Never.cs @@ -0,0 +1,57 @@ +using System.Threading; + +namespace Cysharp.Threading.Tasks.Linq +{ + public static partial class UniTaskAsyncEnumerable + { + public static IUniTaskAsyncEnumerable Never() + { + return Cysharp.Threading.Tasks.Linq.Never.Instance; + } + } + + internal class Never : IUniTaskAsyncEnumerable + { + public static readonly IUniTaskAsyncEnumerable Instance = new Never(); + + Never() + { + } + + public IUniTaskAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + return new Enumerator(cancellationToken); + } + + class Enumerator : IUniTaskAsyncEnumerator + { + CancellationToken cancellationToken; + + public Enumerator(CancellationToken cancellationToken) + { + this.cancellationToken = cancellationToken; + } + + public T Current => default; + + public UniTask MoveNextAsync() + { + var tcs = new UniTaskCompletionSource(); + + cancellationToken.Register(state => + { + var task = (UniTaskCompletionSource)state; + task.TrySetCanceled(cancellationToken); + }, tcs); + + return tcs.Task; + } + + public UniTask DisposeAsync() + { + return default; + } + } + } +} \ No newline at end of file diff --git a/src/UniTask.NetCoreTests/Linq/Factory.cs b/src/UniTask.NetCoreTests/Linq/Factory.cs index 7d82724..c2da016 100644 --- a/src/UniTask.NetCoreTests/Linq/Factory.cs +++ b/src/UniTask.NetCoreTests/Linq/Factory.cs @@ -59,6 +59,7 @@ namespace UniTask.NetCoreTests.Linq xs.Length.Should().Be(1); xs[0].Should().Be(value); } + }