1
0

Materialize.cs 1.8 KB

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