1
0

ToArray.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class ToArray<TSource> : Producer<TSource[]>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. public ToArray(IObservable<TSource> source)
  13. {
  14. _source = source;
  15. }
  16. protected override IDisposable Run(IObserver<TSource[]> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(observer, cancel);
  19. setSink(sink);
  20. return _source.SubscribeSafe(sink);
  21. }
  22. class _ : Sink<TSource[]>, IObserver<TSource>
  23. {
  24. private List<TSource> _list;
  25. public _(IObserver<TSource[]> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. _list = new List<TSource>();
  29. }
  30. public void OnNext(TSource value)
  31. {
  32. _list.Add(value);
  33. }
  34. public void OnError(Exception error)
  35. {
  36. base._observer.OnError(error);
  37. base.Dispose();
  38. }
  39. public void OnCompleted()
  40. {
  41. base._observer.OnNext(_list.ToArray());
  42. base._observer.OnCompleted();
  43. base.Dispose();
  44. }
  45. }
  46. }
  47. }
  48. #endif