Skip to content

Commit 6e18d00

Browse files
committed
Added new way to propagate undefined generics changes in order to allow updating the undefined generics later on when a better definition is found
1 parent 5faa463 commit 6e18d00

File tree

15 files changed

+348
-213
lines changed

15 files changed

+348
-213
lines changed

src/NodeDev.Blazor/Components/GraphCanvas.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void OnGraphChangedFromCore((Graph, bool) _)
141141

142142
#region UpdateConnectionType
143143

144-
public void UpdatePortTypeAndColor(Connection connection)
144+
public void UpdatePortColor(Connection connection)
145145
{
146146
var node = Diagram.Nodes.OfType<GraphNodeModel>().FirstOrDefault(x => x.Node == connection.Parent);
147147
if (node == null)
@@ -338,7 +338,7 @@ public void OnConnectionRemoved(BaseLinkModel baseLinkModel)
338338

339339
// We have to add back the textbox editor
340340
if (destination.Connections.Count == 0 && destination.Type.AllowTextboxEdit)
341-
UpdatePortTypeAndColor(destination);
341+
UpdatePortColor(destination);
342342

343343
UpdateVerticesInConnection(source, destination, baseLinkModel);
344344
}
@@ -478,7 +478,7 @@ private void OnGenericTypeSelected(TypeBase type)
478478
if (PopupNode == null || GenericTypeSelectionMenuGeneric == null)
479479
return;
480480

481-
GraphManagerService.PropagateNewGeneric(PopupNode, new Dictionary<UndefinedGenericType, TypeBase>() { [GenericTypeSelectionMenuGeneric] = type }, false);
481+
GraphManagerService.PropagateNewGeneric(PopupNode, new Dictionary<UndefinedGenericType, TypeBase>() { [GenericTypeSelectionMenuGeneric] = type }, false, null, overrideInitialTypes: true);
482482

483483
// Prefer updating the nodes directly instead of calling Graph.RaiseGraphChanged(true) to be sure it is called as soon as possible
484484
UpdateNodes(Graph.Nodes.Values.ToList());

src/NodeDev.Blazor/Services/GraphManager/GraphManagerService.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using NodeDev.Blazor.DiagramsModels;
2-
using NodeDev.Core;
1+
using NodeDev.Core;
32
using NodeDev.Core.Connections;
43
using NodeDev.Core.Nodes;
54
using NodeDev.Core.Types;
@@ -25,14 +24,14 @@ public void AddNewConnectionBetween(Connection source, Connection destination)
2524
Graph.Connect(source, destination, false);
2625

2726
// we're plugging something something with a generic into something without a generic
28-
if (source.Type.IsAssignableTo(destination.Type, out var newTypes) && newTypes.Count != 0)
27+
if (source.IsAssignableTo(destination, true, true, out var newTypes, out var usedInitialTypes) && newTypes.Count != 0)
2928
{
30-
PropagateNewGeneric(source.Parent, newTypes, false);
31-
PropagateNewGeneric(destination.Parent, newTypes, false);
29+
PropagateNewGeneric(source.Parent, newTypes, usedInitialTypes, destination, false);
30+
PropagateNewGeneric(destination.Parent, newTypes, usedInitialTypes, source, false);
3231
}
3332

34-
GraphCanvas.UpdatePortTypeAndColor(source);
35-
GraphCanvas.UpdatePortTypeAndColor(destination);
33+
GraphCanvas.UpdatePortColor(source);
34+
GraphCanvas.UpdatePortColor(destination);
3635

3736
// we have to disconnect the previously connected exec, since exec outputs can only have one connection
3837
if (source.Type.IsExec && source.Connections.Count > 1)
@@ -48,38 +47,47 @@ public void DisconnectConnectionBetween(Connection source, Connection destinatio
4847
Graph.Disconnect(source, destination, false);
4948
GraphCanvas.RemoveLinkFromGraphCanvas(source, destination);
5049

51-
GraphCanvas.UpdatePortTypeAndColor(source);
52-
GraphCanvas.UpdatePortTypeAndColor(destination);
50+
GraphCanvas.UpdatePortColor(source);
51+
GraphCanvas.UpdatePortColor(destination);
5352
}
5453

55-
public void PropagateNewGeneric(Node node, IReadOnlyDictionary<UndefinedGenericType, TypeBase> changedGenerics, bool requireUIRefresh)
54+
/// <summary>
55+
/// Propagate the new generic type to all the connections of the node and recursively to the connected nodes.
56+
/// </summary>
57+
/// <param name="initiatingConnection">The connection that initiated the propagation. This is used to avoid reupdating back and forth, sometimes erasing information in the process.</param>
58+
public void PropagateNewGeneric(Node node, IReadOnlyDictionary<UndefinedGenericType, TypeBase> changedGenerics, bool useInitialTypes, Connection? initiatingConnection, bool overrideInitialTypes)
5659
{
60+
bool hadAnyChanges = false;
5761
foreach (var port in node.InputsAndOutputs) // check if any of the ports have the generic we just solved
5862
{
59-
var previousType = port.Type;
63+
var previousType = useInitialTypes ? port.InitialType : port.Type;
6064

6165
if (!previousType.GetUndefinedGenericTypes().Any(changedGenerics.ContainsKey))
6266
continue;
6367

6468
// update port.Type property as well as the textbox visibility if necessary
65-
port.UpdateTypeAndTextboxVisibility(previousType.ReplaceUndefinedGeneric(changedGenerics));
66-
node.GenericConnectionTypeDefined(port);
67-
GraphCanvas.UpdatePortTypeAndColor(port);
69+
port.UpdateTypeAndTextboxVisibility(previousType.ReplaceUndefinedGeneric(changedGenerics), overrideInitialType: overrideInitialTypes);
70+
hadAnyChanges |= node.GenericConnectionTypeDefined(port).Count != 0;
71+
GraphCanvas.UpdatePortColor(port);
6872

6973
var isPortInput = port.IsInput; // cache for performance, IsInput is slow
7074
// check if other connections had their own generics and if we just solved them
7175
foreach (var other in port.Connections.ToList())
7276
{
77+
if(other == initiatingConnection)
78+
continue;
79+
7380
var source = isPortInput ? other : port;
7481
var target = isPortInput ? port : other;
75-
if (source.Type.IsAssignableTo(target.Type, out var changedGenerics2) && changedGenerics2.Count != 0)
76-
PropagateNewGeneric(other.Parent, changedGenerics2, false); // no need to refresh UI since we'll do it ourselves anyway at the end of this call
82+
if (source.IsAssignableTo(target, isPortInput, !isPortInput, out var changedGenerics2, out var usedInitialTypes) && changedGenerics2.Count != 0)
83+
PropagateNewGeneric(other.Parent, changedGenerics2, usedInitialTypes, port, false);
7784
else if ((changedGenerics2?.Count ?? 0) != 0)// looks like changing the generic made it so we can't link to this connection anymore
7885
DisconnectConnectionBetween(port, other);
7986
}
8087
}
8188

82-
Graph.RaiseGraphChanged(requireUIRefresh);
89+
if (hadAnyChanges)
90+
Graph.RaiseGraphChanged(false);
8391
}
8492

8593
public void SelectNodeOverload(Node popupNode, Node.AlternateOverload overload)

src/NodeDev.Blazor/Services/GraphManager/IGraphCanvas.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface IGraphCanvas
77
{
88
Graph Graph { get; }
99

10-
void UpdatePortTypeAndColor(Connection connection);
10+
void UpdatePortColor(Connection connection);
1111

1212
void RemoveLinkFromGraphCanvas(Connection source, Connection destination);
1313
}

0 commit comments

Comments
 (0)