simularium / simularium-viewer

NPM package to view Simularium trajectories in 3D
Apache License 2.0
2 stars 0 forks source link

Fix `VisGeometry` constructor overwriting properties it initializes #319

Closed frasercl closed 1 year ago

frasercl commented 1 year ago

Problem

This addresses a comment I left in #314: a new VisGeometry object must have its setupScene method called before it can be used, despite the fact that setupScene is already called once in its constructor.

Solution

After calling setupScene to properly initialize scene objects and place them in the scene graph, VisGeometry's constructor turned out to be reinitializing many key objects (including lights, bounding box, and the renderer itself) without the same setup. Thus the setupScene call after construction was required to recreate these objects a third time with the correct configuration. I removed initializers for objects that are initialized in setupScene from the constructor and was able to remove the setupScene call after the constructor without issue.

This creates a bit of a problem for TypeScript: it wants to be sure that all non-optional declared properties of a class are given initial values in the constructor, and is unwilling to check methods called by the constructor for initializations. There are a few possible solutions to this:

github-actions[bot] commented 1 year ago

jest coverage report ๐Ÿงช

Total coverage

Status Category Percentage Covered / Total
๐Ÿ”ด Statements 40.16% 1951/4858
๐Ÿ”ด Branches 43.97% 814/1851
๐Ÿ”ด Functions 36.83% 400/1086
๐Ÿ”ด Lines 40.41% 1869/4624

Status of coverage: ๐ŸŸข - ok, ๐ŸŸก - slightly more than threshold, ๐Ÿ”ด - under the threshold

meganrm commented 1 year ago

I kinda like option 3 more? I think what you wrote is right, which is this class is designed to "set up a scene" so it makes sense for that all to be part of the constructor. But I don't feel strongly about it so unless @toloudis thinks it should be done that way this seems good enough to me

frasercl commented 1 year ago

I kinda like option 3 more? I think what you wrote is right, which is this class is designed to "set up a scene" so it makes sense for that all to be part of the constructor. But I don't feel strongly about it so unless @toloudis thinks it should be done that way this seems good enough to me

Yeah I also basically prefer option 3, but was being cautious (probably too cautious) about making changes to the API. Given that this class is entirely internal it doesn't make much sense to think too hard about the set of use cases we should or shouldn't support; the use that is there already is likely the only one there ever will be.

toloudis commented 1 year ago

If setupScene is not being called anywhere else, then it's fine to inline it in the constructor. No strong preference there -- I think this code is ok but if you inlined some more of the member variable initialization in the ctor then you could get rid of those exclamation points.