Browse Source

Adding While.

Bart De Smet 8 years ago
parent
commit
5ebe4c9b9b

+ 8 - 0
AsyncRx.NET/Playground/Program.cs

@@ -44,6 +44,7 @@ namespace Playground
             //await SubjectAsync();
             //await TakeUntilAsync();
             //await TimerAsync();
+            //await WhileAsync();
         }
 
         static async Task AggregateAsync()
@@ -247,6 +248,13 @@ namespace Playground
             await AsyncObservable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)).Take(5).Select(_ => DateTimeOffset.Now).SubscribeAsync(Print<DateTimeOffset>()); // TODO: Use ForEachAsync.
         }
 
+        static async Task WhileAsync()
+        {
+            var i = 0;
+
+            await AsyncObservable.While(() => ++i < 5, AsyncObservable.Range(0, 5)).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.
+        }
+
         static IAsyncObserver<T> Print<T>()
         {
             return AsyncObserver.Create<T>(

+ 66 - 0
AsyncRx.NET/System.Reactive.Async.Linq/System/Reactive/Linq/Operators/While.cs

@@ -0,0 +1,66 @@
+// 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.Reactive.Disposables;
+using System.Threading.Tasks;
+
+namespace System.Reactive.Linq
+{
+    partial class AsyncObservable
+    {
+        // REVIEW: Use a tail-recursive sink.
+
+        public static IAsyncObservable<TSource> While<TSource>(Func<bool> condition, IAsyncObservable<TSource> source)
+        {
+            if (condition == null)
+                throw new ArgumentNullException(nameof(condition));
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+
+            return Create<TSource>(async observer =>
+            {
+                var subscription = new SerialAsyncDisposable();
+
+                var o = default(IAsyncObserver<TSource>);
+
+                o = AsyncObserver.CreateUnsafe<TSource>(
+                        observer.OnNextAsync,
+                        observer.OnErrorAsync,
+                        MoveNext
+                    );
+
+                async Task MoveNext()
+                {
+                    var b = default(bool);
+
+                    try
+                    {
+                        b = condition();
+                    }
+                    catch (Exception ex)
+                    {
+                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
+                    }
+
+                    if (b)
+                    {
+                        var sad = new SingleAssignmentAsyncDisposable();
+                        await subscription.AssignAsync(sad).ConfigureAwait(false);
+
+                        var d = await source.SubscribeSafeAsync(o).ConfigureAwait(false);
+                        await sad.AssignAsync(d).ConfigureAwait(false);
+                    }
+                    else
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                }
+
+                await MoveNext().ConfigureAwait(false);
+
+                return subscription;
+            });
+        }
+    }
+}