-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
fix: Unnecessary queries when changing filter values #16994
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ import { | |
getChartMetadataRegistry, | ||
} from '@superset-ui/core'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { areObjectsEqual } from 'src/reduxUtils'; | ||
import { isEqual, isEqualWith } from 'lodash'; | ||
import { getChartDataRequest } from 'src/chart/chartAction'; | ||
import Loading from 'src/components/Loading'; | ||
import BasicErrorAlert from 'src/components/ErrorMessage/BasicErrorAlert'; | ||
|
@@ -105,10 +105,17 @@ const FilterValue: React.FC<FilterProps> = ({ | |
time_range, | ||
}); | ||
const filterOwnState = filter.dataMask?.ownState || {}; | ||
// TODO: We should try to improve our useEffect hooks to depend more on | ||
// granular information instead of big objects that require deep comparison. | ||
const customizer = ( | ||
objValue: Partial<QueryFormData>, | ||
othValue: Partial<QueryFormData>, | ||
key: string, | ||
) => (key === 'url_params' ? true : undefined); | ||
if ( | ||
!isRefreshing && | ||
(!areObjectsEqual(formData, newFormData) || | ||
!areObjectsEqual(ownState, filterOwnState) || | ||
(!isEqualWith(formData, newFormData, customizer) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could enhance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agreed, let's do that by adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I agree. This is the implementation of
The first thing is that is very inefficient because it creates new objects ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of |
||
!isEqual(ownState, filterOwnState) || | ||
isDashboardRefreshing) | ||
) { | ||
setFormData(newFormData); | ||
|
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.
Can we wrap
filterValues
in line 51 in useMemo instead? LikeThere 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.
That's a good idea - however, won't we need to use stringify
filter
in the deps array? Seems like the object reference may change without the contents actually changing.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.
Ah, now I see that
useFilters
hooks returnsObject.entries
which creates a new object on each run... Yeah, that means that it probably needsJSON.stringify
, but in general I'm not sure if the pattern used inuseFilters
is goodThere 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.
Maybe we could utilise a comparison function in
useSelector
(inuseFilters
hook)? https://react-redux.js.org/api/hooks#useselectorThere 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.
However, that might as well be a part of some refactor PR. I'm fine with using
JSON.stringify
here as getting rid of those redundant queries has much higher priority 👍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, it's definitely an antipattern. However, until we have a guideline for avoiding them I'd suggest going with
JSON.stringify
for now, leaving a// TODO:
here and then cleaning these stringifys up in follow-up refactor PRs once we've settled on a good pattern for being able to use strict equality in the deps arrays.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. Let's use
JSON.stringify
for now and plan to deal with all occurrences of it in a specific refactor effort. Meanwhile, we should avoid using objects that require deep comparison asuseEffect
dependencies.