Wouterdek/NodeNetwork

Validation Inifinite Loop when cascading values through nodes

rooper149 opened this issue · 2 comments

If you create a node that basically passes an input to an output it seems you can get yourself stuck in a loop that has something to do with validation. For instance in this code: if you connect Node A's output to Node B's input everything is fine, but if you then disconnect those two nodes and instead connect Node B's output to Node A's input you get stuck in a loop and the UI becomes unresponsive. Is this a known constraint/issue, or am I missing something?

public class Test : NodeViewModel
    {
        public ValueNodeInputViewModel<string> Input { get; }
        public ValueNodeOutputViewModel<string> Output { get; }

        static Test()
        {
            Splat.Locator.CurrentMutable.Register(() => new NodeView(), typeof(IViewFor<Test>));
        }

        public Test(string name)
        {
            Name = name;

            Input = new ValueNodeInputViewModel<string>()
            {
                Name = "Input"
            };
            Inputs.Add(Input);

            Output = new ValueNodeOutputViewModel<string>()
            {
                Name = "Output",
                Value = this.WhenAnyObservable(vm => vm.Input.ValueChanged)
            };
            Outputs.Add(Output);
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var network = new NetworkViewModel();

            var test = new Test(@"Node A");
            network.Nodes.Add(test);

            var test2 = new Test(@"Node B");
            network.Nodes.Add(test2);  
            
            networkView.ViewModel = network;
        }
    }

That's a bug, thanks for the example code.

I'll look more into this later. An easy way to fix this is to replace

Value = this.WhenAnyObservable(vm => vm.Input.ValueChanged)

with

Value = this.WhenAnyValue(vm => vm.Input.Value)

or

Value = this.WhenAnyObservable(vm => vm.Input.ValueChanged).Distinct()