Browse Source

Adding TakeWhile.

Bart De Smet 8 years ago
parent
commit
5daef01349

+ 202 - 0
AsyncRx.NET/System.Reactive.Async/System/Reactive/Linq/Operators/TakeWhile.cs

@@ -0,0 +1,202 @@
+// 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.Threading.Tasks;
+
+namespace System.Reactive.Linq
+{
+    partial class AsyncObservable
+    {
+        public static IAsyncObservable<T> TakeWhile<T>(this IAsyncObservable<T> source, Func<T, bool> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(observer => source.SubscribeAsync(observer.TakeWhile(predicate)));
+        }
+
+        public static IAsyncObservable<T> TakeWhile<T>(this IAsyncObservable<T> source, Func<T, Task<bool>> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(observer => source.SubscribeAsync(observer.TakeWhile(predicate)));
+        }
+
+        public static IAsyncObservable<T> TakeWhile<T>(this IAsyncObservable<T> source, Func<T, int, bool> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(observer => source.SubscribeAsync(observer.TakeWhile(predicate)));
+        }
+
+        public static IAsyncObservable<T> TakeWhile<T>(this IAsyncObservable<T> source, Func<T, int, Task<bool>> predicate)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(observer => source.SubscribeAsync(observer.TakeWhile(predicate)));
+        }
+    }
+
+    partial class AsyncObserver
+    {
+        public static IAsyncObserver<T> TakeWhile<T>(this IAsyncObserver<T> observer, Func<T, bool> predicate)
+        {
+            if (observer == null)
+                throw new ArgumentNullException(nameof(observer));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(
+                async x =>
+                {
+                    bool b;
+
+                    try
+                    {
+                        b = predicate(x);
+                    }
+                    catch (Exception ex)
+                    {
+                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
+                        return;
+                    }
+
+                    if (b)
+                    {
+                        await observer.OnNextAsync(x).ConfigureAwait(false);
+                    }
+                    else
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                },
+                observer.OnErrorAsync,
+                observer.OnCompletedAsync
+            );
+        }
+
+        public static IAsyncObserver<T> TakeWhile<T>(this IAsyncObserver<T> observer, Func<T, Task<bool>> predicate)
+        {
+            if (observer == null)
+                throw new ArgumentNullException(nameof(observer));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            return Create<T>(
+                async x =>
+                {
+                    bool b;
+
+                    try
+                    {
+                        b = await predicate(x).ConfigureAwait(false);
+                    }
+                    catch (Exception ex)
+                    {
+                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
+                        return;
+                    }
+
+                    if (b)
+                    {
+                        await observer.OnNextAsync(x).ConfigureAwait(false);
+                    }
+                    else
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                },
+                observer.OnErrorAsync,
+                observer.OnCompletedAsync
+            );
+        }
+
+        public static IAsyncObserver<T> TakeWhile<T>(this IAsyncObserver<T> observer, Func<T, int, bool> predicate)
+        {
+            if (observer == null)
+                throw new ArgumentNullException(nameof(observer));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            int i = 0;
+
+            return Create<T>(
+                async x =>
+                {
+                    bool b;
+
+                    try
+                    {
+                        b = predicate(x, checked(i++));
+                    }
+                    catch (Exception ex)
+                    {
+                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
+                        return;
+                    }
+
+                    if (b)
+                    {
+                        await observer.OnNextAsync(x).ConfigureAwait(false);
+                    }
+                    else
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                },
+                observer.OnErrorAsync,
+                observer.OnCompletedAsync
+            );
+        }
+
+        public static IAsyncObserver<T> TakeWhile<T>(this IAsyncObserver<T> observer, Func<T, int, Task<bool>> predicate)
+        {
+            if (observer == null)
+                throw new ArgumentNullException(nameof(observer));
+            if (predicate == null)
+                throw new ArgumentNullException(nameof(predicate));
+
+            int i = 0;
+
+            return Create<T>(
+                async x =>
+                {
+                    bool b;
+
+                    try
+                    {
+                        b = await predicate(x, checked(i++)).ConfigureAwait(false);
+                    }
+                    catch (Exception ex)
+                    {
+                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
+                        return;
+                    }
+
+                    if (b)
+                    {
+                        await observer.OnNextAsync(x).ConfigureAwait(false);
+                    }
+                    else
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                },
+                observer.OnErrorAsync,
+                observer.OnCompletedAsync
+            );
+        }
+    }
+}