ToArray.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.ObservableImpl
  6. {
  7. internal sealed class ToArray<TSource> : Producer<TSource[], ToArray<TSource>._>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. public ToArray(IObservable<TSource> source)
  11. {
  12. _source = source;
  13. }
  14. protected override _ CreateSink(IObserver<TSource[]> observer) => new _(observer);
  15. protected override void Run(_ sink) => sink.Run(_source);
  16. internal sealed class _ : Sink<TSource, TSource[]>
  17. {
  18. private readonly List<TSource> _list;
  19. public _(IObserver<TSource[]> observer)
  20. : base(observer)
  21. {
  22. _list = new List<TSource>();
  23. }
  24. public override void OnNext(TSource value)
  25. {
  26. _list.Add(value);
  27. }
  28. public override void OnCompleted()
  29. {
  30. ForwardOnNext(_list.ToArray());
  31. ForwardOnCompleted();
  32. }
  33. }
  34. }
  35. }