1
0

Materialize.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. namespace System.Reactive.Linq.ObservableImpl
  5. {
  6. internal sealed class Materialize<TSource> : Producer<Notification<TSource>>
  7. {
  8. private readonly IObservable<TSource> _source;
  9. public Materialize(IObservable<TSource> source)
  10. {
  11. _source = source;
  12. }
  13. public IObservable<TSource> Dematerialize()
  14. {
  15. return _source.AsObservable();
  16. }
  17. protected override IDisposable Run(IObserver<Notification<TSource>> observer, IDisposable cancel, Action<IDisposable> setSink)
  18. {
  19. var sink = new _(observer, cancel);
  20. setSink(sink);
  21. return _source.SubscribeSafe(sink);
  22. }
  23. class _ : Sink<Notification<TSource>>, IObserver<TSource>
  24. {
  25. public _(IObserver<Notification<TSource>> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. }
  29. public void OnNext(TSource value)
  30. {
  31. base._observer.OnNext(Notification.CreateOnNext<TSource>(value));
  32. }
  33. public void OnError(Exception error)
  34. {
  35. base._observer.OnNext(Notification.CreateOnError<TSource>(error));
  36. base._observer.OnCompleted();
  37. base.Dispose();
  38. }
  39. public void OnCompleted()
  40. {
  41. base._observer.OnNext(Notification.CreateOnCompleted<TSource>());
  42. base._observer.OnCompleted();
  43. base.Dispose();
  44. }
  45. }
  46. }
  47. }