1
0

Defer.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.Reactive.Disposables;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal sealed class Defer<TValue> : Producer<TValue, Defer<TValue>._>, IEvaluatableObservable<TValue>
  8. {
  9. private readonly Func<IObservable<TValue>> _observableFactory;
  10. public Defer(Func<IObservable<TValue>> observableFactory)
  11. {
  12. _observableFactory = observableFactory;
  13. }
  14. protected override _ CreateSink(IObserver<TValue> observer) => new _(_observableFactory, observer);
  15. protected override void Run(_ sink) => sink.Run();
  16. public IObservable<TValue> Eval() => _observableFactory();
  17. internal sealed class _ : IdentitySink<TValue>
  18. {
  19. private readonly Func<IObservable<TValue>> _observableFactory;
  20. public _(Func<IObservable<TValue>> observableFactory, IObserver<TValue> observer)
  21. : base(observer)
  22. {
  23. _observableFactory = observableFactory;
  24. }
  25. public void Run()
  26. {
  27. var result = default(IObservable<TValue>);
  28. try
  29. {
  30. result = _observableFactory();
  31. }
  32. catch (Exception exception)
  33. {
  34. ForwardOnError(exception);
  35. return;
  36. }
  37. base.Run(result);
  38. }
  39. }
  40. }
  41. }