浏览代码

Adding higher-order Merge.

Bart De Smet 8 年之前
父节点
当前提交
b56c4ea20a

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

@@ -16,6 +16,7 @@ namespace Playground
 
         static async Task MainAsync()
         {
+            await MergeAsync();
             await RangeAsync();
             await ReturnAsync();
             await SubjectAsync();
@@ -23,6 +24,22 @@ namespace Playground
             await TimerAsync();
         }
 
+        static async Task MergeAsync()
+        {
+            var subject = new SequentialSimpleAsyncSubject<IAsyncObservable<int>>();
+
+            var res = subject.Merge();
+
+            await res.SubscribeAsync(Print<int>());
+
+            for (var i = 1; i <= 10; i++)
+            {
+                await subject.OnNextAsync(AsyncObservable.Range(0, i));
+            }
+
+            await subject.OnCompletedAsync();
+        }
+
         static async Task RangeAsync()
         {
             await AsyncObservable.Range(0, 10).SubscribeAsync(Print<int>()); // TODO: Use ForEachAsync.

+ 94 - 0
AsyncRx.NET/System.Reactive.Async/System/Reactive/Linq/Operators/Merge.cs

@@ -0,0 +1,94 @@
+// 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;
+using System.Threading.Tasks;
+
+namespace System.Reactive.Linq
+{
+    partial class AsyncObservable
+    {
+        // TODO: Add Merge with max concurrency and IEnumerable<T>-based overloads.
+
+        public static IAsyncObservable<TSource> Merge<TSource>(this IAsyncObservable<IAsyncObservable<TSource>> source)
+        {
+            if (source == null)
+                throw new ArgumentNullException(nameof(source));
+
+            return Create<TSource>(async observer =>
+            {
+                var (sink, cancel) = AsyncObserver.Merge(observer);
+
+                var subscription = await source.SubscribeAsync(sink);
+
+                return StableCompositeAsyncDisposable.Create(subscription, cancel);
+            });
+        }
+    }
+
+    partial class AsyncObserver
+    {
+        public static (IAsyncObserver<IAsyncObservable<TSource>>, IAsyncDisposable) Merge<TSource>(IAsyncObserver<TSource> observer)
+        {
+            if (observer == null)
+                throw new ArgumentNullException(nameof(observer));
+
+            var gate = new AsyncLock();
+
+            var count = 1;
+
+            var disposable = new CompositeAsyncDisposable();
+
+            async Task OnErrorAsync(Exception ex)
+            {
+                using (await gate.LockAsync().ConfigureAwait(false))
+                {
+                    await observer.OnErrorAsync(ex);
+                }
+            };
+
+            async Task OnCompletedAsync()
+            {
+                using (await gate.LockAsync().ConfigureAwait(false))
+                {
+                    if (--count == 0)
+                    {
+                        await observer.OnCompletedAsync().ConfigureAwait(false);
+                    }
+                }
+            };
+
+            return
+                (
+                    Create<IAsyncObservable<TSource>>(
+                        async xs =>
+                        {
+                            using (await gate.LockAsync().ConfigureAwait(false))
+                            {
+                                count++;
+                            }
+
+                            var inner = await xs.SubscribeAsync(
+                                async x =>
+                                {
+                                    using (await gate.LockAsync().ConfigureAwait(false))
+                                    {
+                                        await observer.OnNextAsync(x).ConfigureAwait(false);
+                                    }
+                                },
+                                OnErrorAsync,
+                                OnCompletedAsync
+                            ).ConfigureAwait(false);
+
+                            await disposable.AddAsync(inner).ConfigureAwait(false);
+                        },
+                        OnErrorAsync,
+                        OnCompletedAsync
+                    ),
+                    disposable
+                );
+        }
+    }
+}