-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[DataGrid] Fix memoized selectors with arguments #15215
Open
MBilalShafi
wants to merge
9
commits into
mui:master
Choose a base branch
from
MBilalShafi:fix-memoized-argument-selectors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−11
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
782d560
[DataGrid] Improve v8 selectors
MBilalShafi 5e111b8
Only recompute current selector on args change
MBilalShafi 912e09d
Add shallow comparison for complex arguments
MBilalShafi 1694698
Fix types issue + other updates
MBilalShafi 2c43856
Fix eslint
MBilalShafi f673456
Add eslint argsIgnorePattern for derived arguments selector
MBilalShafi c698628
Merge branch 'master' into fix-memoized-argument-selectors
MBilalShafi 9b8acec
Prettier
MBilalShafi 2f4463d
Update eslint rule
MBilalShafi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KenanYusuf There isn't a perfect fix for it atm, I've settled on making
args
an optional argument, which would solve the problem you mentioned but not warn now if a selector requires arguments but isn't passed. To solve this problem I think we'll have to go back to this approach.I guess the tradeoff is liveable since not providing arguments where required should generate runtime errors for the users to fix them anyways.
Thoughts?
CC @mui/xgrid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree on this.
It may be a separate issue, but given the following selectors:
I am seeing this error when calling the selector function:
Full error
If I change it to the following, the selector function is typed correctly but the param is unused:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's kinda expected. The derived selectors have to have the same arguments (if not more), only then the underlying selectors will be provided with the correct arguments to compute the substate correctly.
gridVisibleColumnDefinitionsSelector
requireslistView
argument andgridVisibleColumnFieldsSelector
is derived from it. It should also requirelistView
argument forgridVisibleColumnDefinitionsSelector
provide the correct value for thevisibleColumns
argument.If multiple selectors require different arguments, we could use an object as argument. For example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it makes sense, but in scenarios where the selector arg is not used, do we just disable
@typescript-eslint/no-unused-vars
on the line of the unused param?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could explicitly define the function type to avoid adding an ignore directive and a cleaner code.
How does the following feel:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels quite verbose doing this for each selector that is derived from another with args.
Ideally,
derivedSelector2
in the example above would include the typed selector args in the function signature based on the selectors that are passed to it.The same way that TS knows here that this first param is
GridListColDef[]
because it hasgridVisibleColumnDefinitionsSelector
as the first argument ofcreateSelectorMemoizedV8
:I haven't fully wrapped my head around the types for these selector creator functions, so I don't know if it is possible, but that would be the ideal DX I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KenanYusuf You are right, it's a bit verbose. It aligns with the choice we made in the selectors with arguments PR, i.e. we should explicitly type the arguments when defining the output function.
I think that having the underlying selectors' output function's arguments type detected automatically won't be possible without the two-step generics, as currently it's only possible to infer the function's return type.
We could introduce an ignore pattern for the
@typescript-eslint/no-unused-vars
rule:To allow something like following without the need to add the ignore directive.
(Typescript compiler does it automatically.)
It will be only needed for the selectors that are derived from some selectors with arguments but they do not require any arguments which I assume would be very less use-cases.
How does adding the eslint ignore pattern sound to reduce verbosity for such use cases? Underscore pattern is widely used and shouldn't contribute to reduced readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the context from the initial discussion, makes sense. I think the underscore pattern is a good compromise given the complexities of the alternate solutions. I'm going to continue testing these changes today 👍