// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Reactive.Disposables; 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() { var result = default(IObservable); try { result = _observableFactory(); } catch (Exception exception) { ForwardOnError(exception); return; } base.Run(result); } } } }