ToArray.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource[]> ToArray<TSource>(this IAsyncObservable<TSource> source)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. return Create<TSource[]>(observer => source.SubscribeSafeAsync(AsyncObserver.ToArray(observer)));
  14. }
  15. }
  16. partial class AsyncObserver
  17. {
  18. public static IAsyncObserver<TSource> ToArray<TSource>(IAsyncObserver<TSource[]> observer)
  19. {
  20. if (observer == null)
  21. throw new ArgumentNullException(nameof(observer));
  22. return Aggregate<TSource, List<TSource>, TSource[]>(observer, new List<TSource>(), (xs, x) => { xs.Add(x); return xs; }, xs => xs.ToArray());
  23. }
  24. }
  25. }