New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove IIFEs, to loosen cyclical dependencies #14695
Conversation
You don't need to include |
What about the methods listed below? They have the following notation (note the missing pair of brackets): distanceToPoint: function () {
var v1 = new Vector2();
return function distanceToPoint( point ) {
var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
return clampedPoint.sub( point ).length();
};
}(), Do we also have to convert them?
|
@takahirox understood — removed. @Mugen87 argh, I was searching for In a situation like this one... rotateX: function () {
// rotate geometry around world x-axis
var m1 = new Matrix4();
return function rotateX( angle ) {
m1.makeRotationX( angle );
this.applyMatrix( m1 );
return this;
};
}(),
rotateY: function () {
// rotate geometry around world y-axis
var m1 = new Matrix4();
return function rotateY( angle ) {
m1.makeRotationY( angle );
this.applyMatrix( m1 );
return this;
};
}(), ...does each method need its own instance? Or can they all use a shared |
A method might call another method of the same class that both use the same type of temporary helper object. If we would now have a single shared instance, intermediate results get overwritten. An example for this is Lines 50 to 109 in a8e5062
BTW: These two methods are something special since they are directly assigned to the function constructor (not the prototype). In context of classes, these methods should ideally use the |
@Rich-Harris I think we could also change the mentioned methods by hand. If you like, I can support you in this task. |
I've made good progress on the automated conversion but haven't had a chance to finish it off this week — I was thinking that a manual conversion in those cases probably makes more sense, yes (otherwise we'd have a bunch of auto-generated |
I think you should use some type of prefix notation to denote these module scope variables from local function variables, otherwise people will make mistakes. Could be as simple as _position or m_position (flashbacks to MFC) or [functionName]_position would also be nice to reduce the scope of each variable to a specific function. |
What happened to this awesome PR? This is the first step to make the tree.js bundle tree-shakable |
Closing. All IIFEs are now removed from the code base. |
Following @Mugen87's suggestion in #14654 (comment):
A number of classes have methods that are returned from immediately-invoked function expressions (IIFEs), meaning that helper instances are created eagerly. This creates cyclical dependencies that make it tricky to convert the entire codebase to classes, and impede tree-shaking.
Because the codebase is composed of modules, we can simply hoist the helper variable declarations to the top of the modules that use them, and instantiate them lazily when the methods are invoked.