// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. namespace System.Reactive.Linq.ObservableImpl { internal sealed class Defer : Producer._>, IEvaluatableObservable { private readonly Func> _observableFactory; public Defer(Func> observableFactory) { _observableFactory = observableFactory; } protected override _ CreateSink(IObserver observer) => new(_observableFactory, observer); protected override void Run(_ sink) => sink.Run(); public IObservable Eval() => _observableFactory(); internal sealed class _ : IdentitySink { private readonly Func> _observableFactory; public _(Func> observableFactory, IObserver observer) : base(observer) { _observableFactory = observableFactory; } public void Run() { IObservable result; try { result = _observableFactory(); } catch (Exception exception) { ForwardOnError(exception); return; } Run(result); } } } }