Quellcode durchsuchen

Minimal work to remove CancellationToken from MoveNextAsync.

Bart De Smet vor 8 Jahren
Ursprung
Commit
c36206ae8d

+ 12 - 19
Ix.NET/Source/System.Interactive.Async/AsyncIterator.cs

@@ -73,7 +73,7 @@ namespace System.Linq
                 }
             }
 
-            public async Task<bool> MoveNextAsync(CancellationToken cancellationToken)
+            public async Task<bool> MoveNextAsync()
             {
                 // Note: MoveNext *must* be implemented as an async method to ensure
                 // that any exceptions thrown from the MoveNextCore call are handled 
@@ -84,27 +84,20 @@ namespace System.Linq
                     return false;
                 }
 
-                using (cancellationToken.Register(Dispose))
-                using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
+                try
                 {
-                    try
-                    {
-                        // Short circuit and don't even call MoveNexCore
-                        cancellationToken.ThrowIfCancellationRequested();
-
-                        var result = await MoveNextCore(cts.Token)
-                                         .ConfigureAwait(false);
+                    var result = await MoveNextCore(default(CancellationToken))
+                                        .ConfigureAwait(false);
 
-                        currentIsInvalid = !result; // if move next is false, invalid otherwise valid
+                    currentIsInvalid = !result; // if move next is false, invalid otherwise valid
 
-                        return result;
-                    }
-                    catch
-                    {
-                        currentIsInvalid = true;
-                        Dispose();
-                        throw;
-                    }
+                    return result;
+                }
+                catch
+                {
+                    currentIsInvalid = true;
+                    Dispose();
+                    throw;
                 }
             }
 

+ 3 - 3
Ix.NET/Source/System.Interactive.Async/GroupJoin.cs

@@ -111,10 +111,10 @@ namespace System.Linq
                     _comparer = comparer;
                 }
 
-                public async Task<bool> MoveNextAsync(CancellationToken cancellationToken)
+                public async Task<bool> MoveNextAsync()
                 {
                     // nothing to do 
-                    if (!await _outer.MoveNextAsync(cancellationToken)
+                    if (!await _outer.MoveNextAsync()
                                      .ConfigureAwait(false))
                     {
                         return false;
@@ -122,7 +122,7 @@ namespace System.Linq
 
                     if (_lookup == null)
                     {
-                        _lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(_inner, _innerKeySelector, _comparer, cancellationToken)
+                        _lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(_inner, _innerKeySelector, _comparer)
                                                 .ConfigureAwait(false);
                     }
 

+ 20 - 1
Ix.NET/Source/System.Interactive.Async/IAsyncEnumerator.cs

@@ -22,11 +22,30 @@ namespace System.Collections.Generic
         /// <summary>
         ///     Advances the enumerator to the next element in the sequence, returning the result asynchronously.
         /// </summary>
+        /// <returns>
+        ///     Task containing the result of the operation: true if the enumerator was successfully advanced
+        ///     to the next element; false if the enumerator has passed the end of the sequence.
+        /// </returns>
+        Task<bool> MoveNextAsync();
+    }
+
+    /// <summary>
+    /// Provides a set of extension methods for <see cref="IAsyncEnumerator{T}"/>.
+    /// </summary>
+    public static class AsyncEnumeratorExtensions
+    {
+        /// <summary>
+        ///     Advances the enumerator to the next element in the sequence, returning the result asynchronously.
+        /// </summary>
+        /// <param name="source">The enumerator to advance.</param>
         /// <param name="cancellationToken">Cancellation token that can be used to cancel the operation.</param>
         /// <returns>
         ///     Task containing the result of the operation: true if the enumerator was successfully advanced
         ///     to the next element; false if the enumerator has passed the end of the sequence.
         /// </returns>
-        Task<bool> MoveNextAsync(CancellationToken cancellationToken);
+        public static Task<bool> MoveNextAsync<T>(this IAsyncEnumerator<T> source, CancellationToken cancellationToken)
+        {
+            return source.MoveNextAsync();
+        }
     }
 }

+ 1 - 1
Ix.NET/Source/System.Interactive.Async/Join.cs

@@ -119,7 +119,7 @@ namespace System.Linq
                                 if (await outerEnumerator.MoveNextAsync(cancellationToken)
                                                          .ConfigureAwait(false))
                                 {
-                                    lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer, cancellationToken).ConfigureAwait(false);
+                                    lookup = await Internal.Lookup<TKey, TInner>.CreateForJoinAsync(inner, innerKeySelector, comparer).ConfigureAwait(false);
 
                                     if (lookup.Count != 0)
                                     {

+ 2 - 2
Ix.NET/Source/System.Interactive.Async/Lookup.cs

@@ -228,12 +228,12 @@ namespace System.Linq.Internal
             return lookup;
         }
 
-        internal static async Task<Lookup<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
+        internal static async Task<Lookup<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer)
         {
             var lookup = new Lookup<TKey, TElement>(comparer);
             using (var enu = source.GetAsyncEnumerator())
             {
-                while (await enu.MoveNextAsync(cancellationToken)
+                while (await enu.MoveNextAsync()
                                 .ConfigureAwait(false))
                 {
                     var key = keySelector(enu.Current);

+ 4 - 6
Ix.NET/Source/Tests/AsyncTests.Bugs.cs

@@ -239,22 +239,20 @@ namespace Tests
                 {
                 }
 
-                public CancellationToken LastToken { get; private set; }
                 public bool MoveNextWasCalled { get; private set; }
 
                 public int Current => i;
                 
-                public async Task<bool> MoveNextAsync(CancellationToken cancellationToken)
+                public async Task<bool> MoveNextAsync()
                 {
-                    LastToken = cancellationToken;
                     MoveNextWasCalled = true;
                   
                     i++;
                     if (Current >= iterationsBeforeDelay)
                     {
-                        await Task.Delay(WaitTimeoutMs, cancellationToken);
+                        await Task.Delay(WaitTimeoutMs);
                     }
-                    cancellationToken.ThrowIfCancellationRequested();
+
                     return true;
                 }
             }
@@ -416,7 +414,7 @@ namespace Tests
                     _disposeCounter.DisposeCount++;
                 }
 
-                public Task<bool> MoveNextAsync(CancellationToken _)
+                public Task<bool> MoveNextAsync()
                 {
                     return Task.Factory.StartNew(() => false);
                 }

+ 0 - 1
Ix.NET/Source/Tests/AsyncTests.Single.cs

@@ -32,7 +32,6 @@ namespace Tests
             en.MoveNext();
 
             Assert.True(en.MoveNextWasCalled);
-            Assert.Equal(CancellationToken.None, en.LastToken);
         }
 
         [Fact]