Skip to content

Commit 537e744

Browse files
feat(dashboards): use memoized variables selector (#6019)
Co-authored-by: Sahas Chitlange chitlangesahas@gmail.com
1 parent 16b82fe commit 537e744

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/shared/components/cells/Cell.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
} from 'src/types'
2929

3030
// Selectors
31-
import {getAllVariables} from 'src/variables/selectors'
31+
import {getAllVariablesMemoized} from 'src/variables/selectors'
3232
import {getWindowPeriodFromQueryBuilder} from 'src/timeMachine/selectors'
3333

3434
// Constants
@@ -235,7 +235,8 @@ const mstp = (state: AppState, ownProps: OwnProps) => {
235235
state,
236236
view?.id
237237
)
238-
const variables = getAllVariables(state)
238+
const variables = getAllVariablesMemoized(state)
239+
239240
return {variables, view, windowPeriodFromQueryBuilder}
240241
}
241242

src/variables/selectors/index.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Libraries
22
import {get} from 'lodash'
3+
import {createSelector} from 'reselect'
34

45
// Utils
56
import {getActiveQuery} from 'src/timeMachine/selectors'
@@ -15,6 +16,7 @@ import {
1516
WINDOW_PERIOD,
1617
} from 'src/variables/constants'
1718
import {currentContext} from 'src/shared/selectors/currentContext'
19+
import {getCurrentDashboardId} from 'src/dashboards/selectors/'
1820

1921
// Types
2022
import {
@@ -137,6 +139,34 @@ export const getAllVariables = (
137139
return vars
138140
}
139141

142+
const getAllVariableIds = createSelector(
143+
state => state,
144+
getCurrentDashboardId,
145+
(state, currentDashboardId) => {
146+
return getUserVariableNames(state, currentDashboardId).concat([
147+
TIME_RANGE_START,
148+
TIME_RANGE_STOP,
149+
WINDOW_PERIOD,
150+
])
151+
}
152+
)
153+
154+
export const getAllVariablesMemoized = createSelector(
155+
state => state,
156+
getAllVariableIds,
157+
(state, allVariableIds) => {
158+
const variables = []
159+
for (const variableId of allVariableIds) {
160+
const variable = getVariable(state, variableId)
161+
if (variable) {
162+
variables.push(variable)
163+
}
164+
}
165+
166+
return variables
167+
}
168+
)
169+
140170
export const getAllVariablesForZoomRequery = (
141171
state: AppState,
142172
domain: number[],

0 commit comments

Comments
 (0)