Dematerialize.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Dematerialize<TSource> : Producer<TSource>
  9. {
  10. private readonly IObservable<Notification<TSource>> _source;
  11. public Dematerialize(IObservable<Notification<TSource>> source)
  12. {
  13. _source = source;
  14. }
  15. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  16. {
  17. var sink = new _(observer, cancel);
  18. setSink(sink);
  19. return _source.SubscribeSafe(sink);
  20. }
  21. class _ : Sink<TSource>, IObserver<Notification<TSource>>
  22. {
  23. public _(IObserver<TSource> observer, IDisposable cancel)
  24. : base(observer, cancel)
  25. {
  26. }
  27. public void OnNext(Notification<TSource> value)
  28. {
  29. switch (value.Kind)
  30. {
  31. case NotificationKind.OnNext:
  32. base._observer.OnNext(value.Value);
  33. break;
  34. case NotificationKind.OnError:
  35. base._observer.OnError(value.Exception);
  36. base.Dispose();
  37. break;
  38. case NotificationKind.OnCompleted:
  39. base._observer.OnCompleted();
  40. base.Dispose();
  41. break;
  42. }
  43. }
  44. public void OnError(Exception error)
  45. {
  46. base._observer.OnError(error);
  47. base.Dispose();
  48. }
  49. public void OnCompleted()
  50. {
  51. base._observer.OnCompleted();
  52. base.Dispose();
  53. }
  54. }
  55. }
  56. }
  57. #endif