// 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. using System.Collections.Generic; namespace System.Reactive.Linq.ObservableImpl { internal sealed class Case : Producer._>, IEvaluatableObservable where TValue : notnull { private readonly Func _selector; private readonly IDictionary> _sources; private readonly IObservable _defaultSource; public Case(Func selector, IDictionary> sources, IObservable defaultSource) { _selector = selector; _sources = sources; _defaultSource = defaultSource; } public IObservable Eval() { if (_sources.TryGetValue(_selector(), out var res)) { return res; } return _defaultSource; } protected override _ CreateSink(IObserver observer) => new(observer); protected override void Run(_ sink) => sink.Run(this); internal sealed class _ : IdentitySink { public _(IObserver observer) : base(observer) { } public void Run(Case parent) { IObservable result; try { result = parent.Eval(); } catch (Exception exception) { ForwardOnError(exception); return; } Run(result); } } } }