Browse Source

Feeding CancellationToken to IAsyncPartition operations.

Bart De Smet 8 years ago
parent
commit
bcd0512515

+ 7 - 3
Ix.NET/Source/System.Linq.Async/System/Linq/IAsyncPartition.cs

@@ -2,6 +2,7 @@
 // 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.Threading;
 using System.Threading.Tasks;
 
 namespace System.Linq
@@ -29,19 +30,22 @@ namespace System.Linq
         /// Gets the item associated with a 0-based index in this sequence.
         /// </summary>
         /// <param name="index">The 0-based index to access.</param>
+        /// <param name="cancellationToken">Token to observe for cancellation requests.</param>
         /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
-        Task<Maybe<TElement>> TryGetElementAsync(int index);
+        Task<Maybe<TElement>> TryGetElementAsync(int index, CancellationToken cancellationToken);
 
         /// <summary>
         /// Gets the first item in this sequence.
         /// </summary>
+        /// <param name="cancellationToken">Token to observe for cancellation requests.</param>
         /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
-        Task<Maybe<TElement>> TryGetFirstAsync();
+        Task<Maybe<TElement>> TryGetFirstAsync(CancellationToken cancellationToken);
 
         /// <summary>
         /// Gets the last item in this sequence.
         /// </summary>
+        /// <param name="cancellationToken">Token to observe for cancellation requests.</param>
         /// <returns>The element if found, otherwise, the default value of <see cref="TElement"/>.</returns>
-        Task<Maybe<TElement>> TryGetLastAsync();
+        Task<Maybe<TElement>> TryGetLastAsync(CancellationToken cancellationToken);
     }
 }

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

@@ -39,7 +39,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetElementAsync(index).ConfigureAwait(false);
+                var first = await p.TryGetElementAsync(index, cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

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

@@ -39,7 +39,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetElementAsync(index).ConfigureAwait(false);
+                var first = await p.TryGetElementAsync(index, cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

+ 4 - 4
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Empty.cs

@@ -12,7 +12,7 @@ namespace System.Linq
     {
         public static IAsyncEnumerable<TValue> Empty<TValue>() => EmptyAsyncIterator<TValue>.Instance;
 
-        private sealed class EmptyAsyncIterator<TValue> : IAsyncPartition<TValue>, IAsyncEnumerator<TValue>
+        internal sealed class EmptyAsyncIterator<TValue> : IAsyncPartition<TValue>, IAsyncEnumerator<TValue>
         {
             public static readonly EmptyAsyncIterator<TValue> Instance = new EmptyAsyncIterator<TValue>();
 
@@ -28,11 +28,11 @@ namespace System.Linq
 
             public Task<List<TValue>> ToListAsync(CancellationToken cancellationToken) => Task.FromResult(new List<TValue>());
 
-            public Task<Maybe<TValue>> TryGetElementAsync(int index) => Task.FromResult(new Maybe<TValue>());
+            public Task<Maybe<TValue>> TryGetElementAsync(int index, CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
 
-            public Task<Maybe<TValue>> TryGetFirstAsync() => Task.FromResult(new Maybe<TValue>());
+            public Task<Maybe<TValue>> TryGetFirstAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
 
-            public Task<Maybe<TValue>> TryGetLastAsync() => Task.FromResult(new Maybe<TValue>());
+            public Task<Maybe<TValue>> TryGetLastAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
 
             public Task<bool> MoveNextAsync() => TaskExt.False;
 

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

@@ -77,7 +77,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetFirstAsync().ConfigureAwait(false);
+                var first = await p.TryGetFirstAsync(cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

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

@@ -77,7 +77,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetFirstAsync().ConfigureAwait(false);
+                var first = await p.TryGetFirstAsync(cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

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

@@ -78,7 +78,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetLastAsync().ConfigureAwait(false);
+                var first = await p.TryGetLastAsync(cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

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

@@ -78,7 +78,7 @@ namespace System.Linq
             }
             else if (source is IAsyncPartition<TSource> p)
             {
-                var first = await p.TryGetLastAsync().ConfigureAwait(false);
+                var first = await p.TryGetLastAsync(cancellationToken).ConfigureAwait(false);
 
                 if (first.HasValue)
                 {

+ 3 - 3
Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Range.cs

@@ -93,7 +93,7 @@ namespace System.Linq
                 return Task.FromResult(res);
             }
 
-            public Task<Maybe<int>> TryGetElementAsync(int index)
+            public Task<Maybe<int>> TryGetElementAsync(int index, CancellationToken cancellationToken)
             {
                 if ((uint)index < (uint)(end - start))
                 {
@@ -103,9 +103,9 @@ namespace System.Linq
                 return Task.FromResult(new Maybe<int>());
             }
 
-            public Task<Maybe<int>> TryGetFirstAsync() => Task.FromResult(new Maybe<int>(start));
+            public Task<Maybe<int>> TryGetFirstAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<int>(start));
 
-            public Task<Maybe<int>> TryGetLastAsync() => Task.FromResult(new Maybe<int>(end - 1));
+            public Task<Maybe<int>> TryGetLastAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<int>(end - 1));
 
             protected override async Task<bool> MoveNextCore()
             {