Open Nicocchi opened 1 year ago
Hey you just swapped name and id in the constructor of your Port.
Since the name of the port is used as an index in NodeModel
(which DefaultNodeModel
relies on, when u watch in the implementation):
addPort(port) {
port.setParent(this);
this.ports[port.getName()] = port;
return port;
}
So essentially only add 1, all further are overwritten, since you use "true" as name.
And now the big BUT: DefaultNodeModel
also saves its in and outPorts BUT: in lists
Thats why all is displayed properly (since the DefaultNodeWidget
relies on the lists, but serializing and deserializing breaks the code since:
deserialize(event) {
super.deserialize(event);
this.options.name = event.data.name;
this.options.color = event.data.color;
this.portsIn = _.map(event.data.portsInOrder, (id) => {
return this.getPortFromID(id);
});
this.portsOut = _.map(event.data.portsOutOrder, (id) => {
return this.getPortFromID(id);
});
}
relies on this.getPortFromID()
from NodeModel
and this searches its own port object representation for the id, where only the last added port is present. resulting in an null
return and writing null
in the serialized object.
Bottom line:
Now you know WHY it fails a fix four your issue will be to just swap the parameters in the constructor of your ports.
But: I think my PR (which wasn't meant to be for this) could result in an fix for this. If this would result in an major change of how ports are saved in NodeModel.
Because even if your issue can easily be fixed it shows a major weakness (at least in my eyes) of how ports are handled in NodeModel
I've followed the example serialization code, and serialization works, but when I try to deserialize, if more than 2 ports are added, it just returns:
Uncaught TypeError: Cannot read porperties of null (reading 'getID') at DefaultNodeModel.ts:114:17
I'm not using any custom nodes or links, etc just DefaultNodeModel.Stack:
React 18.2 + Vite 4.1 + Electron 23.2
Serialization
Deserializaton
Adding ports
I am also saving and opening the data to a JSON file
Saving
Opening