Skip to content

Commit bfd68c8

Browse files
committed
Factor wing selector boilerplate into helper makers.
1 parent 45160d4 commit bfd68c8

1 file changed

Lines changed: 85 additions & 106 deletions

File tree

src/selectors/per-thread/stack-sample.ts

Lines changed: 85 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import type {
4747
IndexIntoStackTable,
4848
SamplesLikeTable,
4949
SampleCategoriesAndSubcategories,
50+
CallNodeArea,
5051
} from 'firefox-profiler/types';
5152
import type {
5253
CallNodeInfo,
@@ -242,13 +243,50 @@ export function getStackAndSampleSelectorsPerThread(
242243
ProfileData.getLowerWingCallNodeInfo
243244
);
244245

245-
const getLowerWingSelectedCallNodePath: Selector<CallNodePath> =
246+
// Returns the selected call node path stored under the given area in this
247+
// thread's ThreadViewOptions.
248+
const makeSelectedPathSelector = (
249+
area: CallNodeArea
250+
): Selector<CallNodePath> =>
246251
createSelector(
247252
threadSelectors.getViewOptions,
248-
(threadViewOptions): CallNodePath =>
249-
threadViewOptions.selectedCallNodePaths.LOWER_WING
253+
(threadViewOptions) => threadViewOptions.selectedCallNodePaths[area]
250254
);
251255

256+
// Returns the expanded call node paths stored under the given area.
257+
const makeExpandedPathsSelector = (area: CallNodeArea): Selector<PathSet> =>
258+
createSelector(
259+
threadSelectors.getViewOptions,
260+
(threadViewOptions) => threadViewOptions.expandedCallNodePaths[area]
261+
);
262+
263+
// Combines a CallNodeInfo selector with a CallNodePath selector to produce
264+
// the corresponding call node index.
265+
const makeCallNodeIndexFromPathSelector = (
266+
callNodeInfoSel: Selector<CallNodeInfo>,
267+
pathSel: Selector<CallNodePath>
268+
): Selector<IndexIntoCallNodeTable | null> =>
269+
createSelector(callNodeInfoSel, pathSel, (callNodeInfo, callNodePath) =>
270+
callNodeInfo.getCallNodeIndexFromPath(callNodePath)
271+
);
272+
273+
// Combines a CallNodeInfo selector with an expanded-paths selector to
274+
// produce the corresponding array of call node indexes.
275+
const makeExpandedIndexesSelector = (
276+
callNodeInfoSel: Selector<CallNodeInfo>,
277+
pathsSel: Selector<PathSet>
278+
): Selector<Array<IndexIntoCallNodeTable | null>> =>
279+
createSelector(callNodeInfoSel, pathsSel, (callNodeInfo, callNodePaths) =>
280+
Array.from(callNodePaths).map((path) =>
281+
callNodeInfo.getCallNodeIndexFromPath(path)
282+
)
283+
);
284+
285+
const getLowerWingSelectedCallNodePath =
286+
makeSelectedPathSelector('LOWER_WING');
287+
const getUpperWingSelectedCallNodePath =
288+
makeSelectedPathSelector('UPPER_WING');
289+
252290
const getSelectedCallNodePath: Selector<CallNodePath> = createSelector(
253291
threadSelectors.getViewOptions,
254292
UrlState.getInvertCallstack,
@@ -258,39 +296,18 @@ export function getStackAndSampleSelectorsPerThread(
258296
: threadViewOptions.selectedCallNodePaths.NON_INVERTED_TREE
259297
);
260298

261-
const getUpperWingSelectedCallNodePath: Selector<CallNodePath> =
262-
createSelector(
263-
threadSelectors.getViewOptions,
264-
(threadViewOptions): CallNodePath =>
265-
threadViewOptions.selectedCallNodePaths.UPPER_WING
266-
);
267-
268-
const getSelectedCallNodeIndex: Selector<IndexIntoCallNodeTable | null> =
269-
createSelector(
270-
getCallNodeInfo,
271-
getSelectedCallNodePath,
272-
(callNodeInfo, callNodePath) => {
273-
return callNodeInfo.getCallNodeIndexFromPath(callNodePath);
274-
}
275-
);
276-
277-
const getLowerWingSelectedCallNodeIndex: Selector<IndexIntoCallNodeTable | null> =
278-
createSelector(
279-
getLowerWingCallNodeInfo,
280-
getLowerWingSelectedCallNodePath,
281-
(callNodeInfo, callNodePath) => {
282-
return callNodeInfo.getCallNodeIndexFromPath(callNodePath);
283-
}
284-
);
285-
286-
const getUpperWingSelectedCallNodeIndex: Selector<IndexIntoCallNodeTable | null> =
287-
createSelector(
288-
getUpperWingCallNodeInfo,
289-
getUpperWingSelectedCallNodePath,
290-
(callNodeInfo, callNodePath) => {
291-
return callNodeInfo.getCallNodeIndexFromPath(callNodePath);
292-
}
293-
);
299+
const getSelectedCallNodeIndex = makeCallNodeIndexFromPathSelector(
300+
getCallNodeInfo,
301+
getSelectedCallNodePath
302+
);
303+
const getLowerWingSelectedCallNodeIndex = makeCallNodeIndexFromPathSelector(
304+
getLowerWingCallNodeInfo,
305+
getLowerWingSelectedCallNodePath
306+
);
307+
const getUpperWingSelectedCallNodeIndex = makeCallNodeIndexFromPathSelector(
308+
getUpperWingCallNodeInfo,
309+
getUpperWingSelectedCallNodePath
310+
);
294311

295312
const getExpandedCallNodePaths: Selector<PathSet> = createSelector(
296313
threadSelectors.getViewOptions,
@@ -301,47 +318,22 @@ export function getStackAndSampleSelectorsPerThread(
301318
: threadViewOptions.expandedCallNodePaths.NON_INVERTED_TREE
302319
);
303320

304-
const getLowerWingExpandedCallNodePaths: Selector<PathSet> = createSelector(
305-
threadSelectors.getViewOptions,
306-
(threadViewOptions) => threadViewOptions.expandedCallNodePaths.LOWER_WING
307-
);
308-
309-
const getUpperWingExpandedCallNodePaths: Selector<PathSet> = createSelector(
310-
threadSelectors.getViewOptions,
311-
(threadViewOptions) => threadViewOptions.expandedCallNodePaths.UPPER_WING
312-
);
321+
const getLowerWingExpandedCallNodePaths =
322+
makeExpandedPathsSelector('LOWER_WING');
323+
const getUpperWingExpandedCallNodePaths =
324+
makeExpandedPathsSelector('UPPER_WING');
313325

314-
const getExpandedCallNodeIndexes: Selector<
315-
Array<IndexIntoCallNodeTable | null>
316-
> = createSelector(
326+
const getExpandedCallNodeIndexes = makeExpandedIndexesSelector(
317327
getCallNodeInfo,
318-
getExpandedCallNodePaths,
319-
(callNodeInfo, callNodePaths) =>
320-
Array.from(callNodePaths).map((path) =>
321-
callNodeInfo.getCallNodeIndexFromPath(path)
322-
)
328+
getExpandedCallNodePaths
323329
);
324-
325-
const getLowerWingExpandedCallNodeIndexes: Selector<
326-
Array<IndexIntoCallNodeTable | null>
327-
> = createSelector(
330+
const getLowerWingExpandedCallNodeIndexes = makeExpandedIndexesSelector(
328331
getLowerWingCallNodeInfo,
329-
getLowerWingExpandedCallNodePaths,
330-
(callNodeInfo, callNodePaths) =>
331-
Array.from(callNodePaths).map((path) =>
332-
callNodeInfo.getCallNodeIndexFromPath(path)
333-
)
332+
getLowerWingExpandedCallNodePaths
334333
);
335-
336-
const getUpperWingExpandedCallNodeIndexes: Selector<
337-
Array<IndexIntoCallNodeTable | null>
338-
> = createSelector(
334+
const getUpperWingExpandedCallNodeIndexes = makeExpandedIndexesSelector(
339335
getUpperWingCallNodeInfo,
340-
getUpperWingExpandedCallNodePaths,
341-
(callNodeInfo, callNodePaths) =>
342-
Array.from(callNodePaths).map((path) =>
343-
callNodeInfo.getCallNodeIndexFromPath(path)
344-
)
336+
getUpperWingExpandedCallNodePaths
345337
);
346338

347339
const _getSampleIndexToNonInvertedCallNodeIndexForPreviewFilteredCtssThread: Selector<
@@ -885,66 +877,53 @@ export function getStackAndSampleSelectorsPerThread(
885877
}
886878
);
887879

888-
const getLowerWingRightClickedCallNodeIndex: Selector<null | IndexIntoCallNodeTable> =
880+
// Returns the right-clicked call node index in `area` for this thread, or
881+
// null if the right-clicked call node belongs to a different thread or area.
882+
const makeAreaRightClickedCallNodeIndexSelector = (
883+
area: CallNodeArea,
884+
callNodeInfoSel: Selector<CallNodeInfo>
885+
): Selector<null | IndexIntoCallNodeTable> =>
889886
createSelector(
890887
getRightClickedCallNodeInfo,
891-
getLowerWingCallNodeInfo,
888+
callNodeInfoSel,
892889
(rightClickedCallNodeInfo, callNodeInfo) => {
893890
if (
894891
rightClickedCallNodeInfo !== null &&
895892
rightClickedCallNodeInfo.threadsKey === threadsKey &&
896-
rightClickedCallNodeInfo.area === 'LOWER_WING'
893+
rightClickedCallNodeInfo.area === area
897894
) {
898895
return callNodeInfo.getCallNodeIndexFromPath(
899896
rightClickedCallNodeInfo.callNodePath
900897
);
901898
}
902-
903899
return null;
904900
}
905901
);
906902

903+
const getLowerWingRightClickedCallNodeIndex =
904+
makeAreaRightClickedCallNodeIndexSelector(
905+
'LOWER_WING',
906+
getLowerWingCallNodeInfo
907+
);
908+
909+
const getUpperWingRightClickedCallNodeIndex =
910+
makeAreaRightClickedCallNodeIndexSelector(
911+
'UPPER_WING',
912+
getUpperWingCallNodeInfo
913+
);
914+
907915
const getLowerWingRightClickedFuncIndex: Selector<null | IndexIntoFuncTable> =
908916
createSelector(
909-
getRightClickedCallNodeInfo,
917+
getLowerWingRightClickedCallNodeIndex,
910918
getLowerWingCallNodeInfo,
911-
(rightClickedCallNodeInfo, callNodeInfo) => {
912-
if (
913-
rightClickedCallNodeInfo === null ||
914-
rightClickedCallNodeInfo.threadsKey !== threadsKey ||
915-
rightClickedCallNodeInfo.area !== 'LOWER_WING'
916-
) {
917-
return null;
918-
}
919-
const callNodeIndex = callNodeInfo.getCallNodeIndexFromPath(
920-
rightClickedCallNodeInfo.callNodePath
921-
);
919+
(callNodeIndex, callNodeInfo) => {
922920
if (callNodeIndex === null) {
923921
return null;
924922
}
925923
return callNodeInfo.funcForNode(callNodeIndex);
926924
}
927925
);
928926

929-
const getUpperWingRightClickedCallNodeIndex: Selector<null | IndexIntoCallNodeTable> =
930-
createSelector(
931-
getRightClickedCallNodeInfo,
932-
getUpperWingCallNodeInfo,
933-
(rightClickedCallNodeInfo, callNodeInfo) => {
934-
if (
935-
rightClickedCallNodeInfo !== null &&
936-
rightClickedCallNodeInfo.threadsKey === threadsKey &&
937-
rightClickedCallNodeInfo.area === 'UPPER_WING'
938-
) {
939-
return callNodeInfo.getCallNodeIndexFromPath(
940-
rightClickedCallNodeInfo.callNodePath
941-
);
942-
}
943-
944-
return null;
945-
}
946-
);
947-
948927
const getRightClickedFunctionIndex: Selector<null | IndexIntoFuncTable> =
949928
createSelector(
950929
ProfileSelectors.getProfileViewOptions,

0 commit comments

Comments
 (0)