Closed generic-github-user closed 5 years ago
Could create a new network, then copy over all information from the first network (nodes, connections, etc.), or clone the network with JSON.parse(JSON.stringify(network))
and use a separate function to add methods to it, as these should be the same for all networks.
Could also use Object.assign()
, as this seems to be working well in testing.
New cloning function:
const clone = function (object) {
return Object.assign({}, object);
}
This should be tested.
This doesn't work - object.assign()
does a "shallow" copy of an object, so any modifications to the network more than one level deep affect all clones of the network. See this question on StackOverflow:
https://stackoverflow.com/questions/34504682/js-does-object-assign-create-deep-copy-or-shallow-copy
An immutable data structure may be needed instead of cloning network objects.
See these threads on Hacker News: https://news.ycombinator.com/item?id=16233330 https://news.ycombinator.com/item?id=17126981
This Stack Overflow answer further explains some of the issues with deep cloning an object in JavaScript: https://stackoverflow.com/a/728694
An immutable data structure will be used for the initial release for future stability and expandability.
For now, a mutable data structure will be used for network objects: https://github.com/generic-github-user/Caesium/issues/91#issuecomment-415371880
It turns out that setting up an immutable data structure isn't as simple as it sounds . . .
JSON.parse(JSON.stringify())
is being used with a a separate function to add methods to each cloned network object.
Looks like we will actually be using an immutable data structure . . . references within network objects being cloned are causing issues: #92
UUID system for all networks, nodes, and connections has been implemented; all internal references within network objects have been removed. See #91 and #93.
"Cloning" a network, including its functions, may not be possible: https://stackoverflow.com/questions/18089033/json-stringify-does-not-process-object-methods