Skip to content
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

Isolate units between diagrams #92

Merged
merged 3 commits into from
Oct 10, 2024
Merged

Isolate units between diagrams #92

merged 3 commits into from
Oct 10, 2024

Conversation

scytacki
Copy link
Member

@scytacki scytacki commented Oct 7, 2024

  • create UnitsManager to contain the units array shared by multiple math instances, and the actions that work with the units array.
  • variable model looks for a unitsManager on its parent container.
  • add unitsManager to the AppStore so the variables can find it. This will also have to be added to the variables shared model in CLUE.
  • extract initializeMath from createMath since it is all about setting up the units of the math instance
  • export UnitsManager because shared variables model will need access it.
  • move IMathLib to eliminate circular dependency
  • add a cypress test for the case that exposes the shared unit problem as well as the complexity of getMathUnit being both an action and a view.
  • add comments explaining the complexity of getMathUnit

Here is a PR in CLUE that is using this PR and the next one:
concord-consortium/collaborative-learning#2424

Note: there is a console warning when used in CLUE:

Warning: Cannot update a component (_Diagram) while rendering a different component (_QuantityNode)

I haven't tracked down the warning precisely. This warning was also there before this PR. It makes sense given the double nature of getMathUnit. It will be called when rendering QuantityNode, which will then update the observable units. This update of the observable units will trigger any components that use the units to render their values, units, or error messages to be re-rendered.

So this illustrates the 2 issues with mixing views and actions are:

  • if it is labeled as action the values it accesses won't be observed.
  • if it is labeled as a view it might be used by a React component and then trigger a component update during the React component's render.

Because this problem existed before this PR I don't think it is worth fixing in this PR. I think the right fix is a refactoring which keeps the units updated based on the current list of variables. So basically a reaction that is watching all of the variables and reconstructing the units array whenever a variable's unit is changed. The tricky part is figuring out where to put this reaction so it will work both in the stand-alone quantity-playground and when it is used inside of CLUE.

…ath instances, and the actions that work with the units array.

- variable model looks for a unitsManager on its parent container.
- add unitsManager to the AppStore so the variables can find it. This will also have to be added to the variables shared model.
- extract initializeMath from createMath since it is all about setting up the units of the math instance
- export UnitsManager because shared variables model will need access it.
- move IMathLib to eliminate circular dependency
- add a cypress test for the case that exposes the problem with it being an action
- add comments explaining the complexity of getMathUnit
@scytacki scytacki requested a review from bgoldowsky October 7, 2024 21:43
Copy link
Contributor

@bgoldowsky bgoldowsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restructuring looks good. I didn't test it though. Added some comments re. documentation.

// - this adds the units of the input variable to the unitsManager.units array.
// - a temporary `mathValue` is returned for the input variable
// - `computedValueIncludingMessageAndError` returns a value for the equation variable.
// - because the units array was updated, and `self.math` is used by `mathValue`, both this triggers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// - because the units array was updated, and `self.math` is used by `mathValue`, both this triggers
// - because the units array was updated, and `self.math` is used by `mathValue`, this triggers

return mathUnit;
}

// NOTE: this is both an action and a view. It is called by MST views to get the math.Unit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that it is an action, but it is not clear to me why you say it is also a view.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to update the comment to clarify this. Technically it is not a view, but it is used by real views, so that indirectly makes it function as a view.

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 97.22222% with 2 lines in your changes missing coverage. Please review.

Project coverage is 71.71%. Comparing base (e1bf9c7) to head (00ca1f4).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/diagram/models/variable.ts 90.90% 1 Missing ⚠️
src/diagram/units-manager.ts 97.72% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #92      +/-   ##
==========================================
+ Coverage   71.31%   71.71%   +0.39%     
==========================================
  Files          46       46              
  Lines        1248     1269      +21     
  Branches      290      291       +1     
==========================================
+ Hits          890      910      +20     
- Misses        343      344       +1     
  Partials       15       15              
Flag Coverage Δ
jest 71.71% <97.22%> (+0.39%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@scytacki scytacki merged commit c1c5339 into main Oct 10, 2024
7 checks passed
@scytacki scytacki deleted the 188356923-isolate-units branch October 10, 2024 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants