Explorar o código

Moving Throw to AsyncEnumerableEx.

Bart De Smet %!s(int64=8) %!d(string=hai) anos
pai
achega
49c49e96ab

+ 1 - 1
Ix.NET/Source/System.Interactive.Async.Tests/System/Linq/AsyncEnumerableExTests.cs

@@ -14,7 +14,7 @@ namespace Tests
     public class AsyncEnumerableExTests
     {
         protected static readonly IAsyncEnumerable<int> Return42 = AsyncEnumerableEx.Return(42);
-        protected static IAsyncEnumerable<T> Throw<T>(Exception exception) => AsyncEnumerable.Throw<T>(exception);
+        protected static IAsyncEnumerable<T> Throw<T>(Exception exception) => AsyncEnumerableEx.Throw<T>(exception);
         protected static Func<Exception, bool> SingleInnerExceptionMatches(Exception ex) => e => ((AggregateException)e).Flatten().InnerExceptions.Single() == ex;
 
         protected const int WaitTimeoutMs = 5000;

+ 33 - 0
Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Throw.cs

@@ -0,0 +1,33 @@
+// 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.Tasks;
+
+namespace System.Linq
+{
+    public static partial class AsyncEnumerableEx
+    {
+        public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
+        {
+            if (exception == null)
+                throw new ArgumentNullException(nameof(exception));
+
+#if NO_TASK_FROMEXCEPTION
+            var tcs = new TaskCompletionSource<bool>();
+            tcs.TrySetException(exception);
+            var moveNextThrows = tcs.Task;
+#else
+            var moveNextThrows = Task.FromException<bool>(exception);
+#endif
+
+            return AsyncEnumerable.CreateEnumerable(
+                () => AsyncEnumerable.CreateEnumerator<TValue>(
+                    () => moveNextThrows,
+                    current: null,
+                    dispose: null)
+            );
+        }
+    }
+}

+ 21 - 1
Ix.NET/Source/System.Linq.Async.Tests/System/Linq/AsyncEnumerableTests.cs

@@ -14,7 +14,6 @@ namespace Tests
     public class AsyncEnumerableTests
     {
         protected static readonly IAsyncEnumerable<int> Return42 = new[] { 42 }.ToAsyncEnumerable();
-        protected static IAsyncEnumerable<T> Throw<T>(Exception exception) => AsyncEnumerable.Throw<T>(exception);
         protected static Func<Exception, bool> SingleInnerExceptionMatches(Exception ex) => e => ((AggregateException)e).Flatten().InnerExceptions.Single() == ex;
 
         protected const int WaitTimeoutMs = 5000;
@@ -80,5 +79,26 @@ namespace Tests
             e1Result.ShouldAllBeEquivalentTo(e2Result);
         }
 #pragma warning restore xUnit1013 // Public method should be marked as test
+
+        protected static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
+        {
+            if (exception == null)
+                throw new ArgumentNullException(nameof(exception));
+
+#if NO_TASK_FROMEXCEPTION
+            var tcs = new TaskCompletionSource<bool>();
+            tcs.TrySetException(exception);
+            var moveNextThrows = tcs.Task;
+#else
+            var moveNextThrows = Task.FromException<bool>(exception);
+#endif
+
+            return AsyncEnumerable.CreateEnumerable(
+                () => AsyncEnumerable.CreateEnumerator<TValue>(
+                    () => moveNextThrows,
+                    current: null,
+                    dispose: null)
+            );
+        }
     }
 }

+ 1 - 1
Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerable.cs

@@ -17,7 +17,7 @@ namespace System.Linq
             return source.Select(x => x);
         }
 
-        public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
+        private static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
         {
             if (exception == null)
                 throw new ArgumentNullException(nameof(exception));