Open TW80000 opened 5 years ago
@lchanmann @kgrgreer @jlhughes @mcarcaso @adamvy
@TW80000 do we have progress on this issue?
I haven't done any more work on it, but I did think of an even better 3rd solution.
Property
already defines a cloneProperty
function that properties can implement to provide custom deep cloning behaviour. We should just implement this method on special cases like properties that are MDAOs or node libraries and simply return a reference in those cases rather than doing the default behaviour of deep cloning.
https://github.com/foam-framework/foam2/blob/master/src/foam/core/Property.js#L144-L153
What do you think of that? @lchanmann
Context
Changes were made recently to the cloning method for plain JavaScript objects such that they do a true deep clone instead of simply returning a reference to the argument.
Stack trace of failing tests
Problems
DirTreeHandler
, which has a property namedpath
that was set to the entire Node.js "path" library. Since FOAM recognized the path library as an object, the clone method tried to deep clone it, which lead to a "Maximum call stack size exceeded" error.Possible solutions
Add a
deepClone
methodProblem 2 paired with the fact that deep cloning is only strictly necessary some of the time means shallow cloning is a reasonable default because it will be faster. If shallow cloning isn't sufficient for the use case, a deep clone can be explicitly performed instead. Therefore this solution is to add a separate
deepClone
method that can be called when deep cloning is required, and all other cases can just callclone
.One advantage of this solution is that it doesn't require any changes to existing code.
Add a
shallowClone
methodSame as above, but the opposite. Add a method that callers can use to explicitly request a shallow clone to avoid problems like Problem 1. The
clone
method remains a deep clone.This requires us to go back on a case by case basis and change existing code that broke because of the change from
clone
being shallow to deep. It can be fixed by calling the newshallowClone
method instead.