Skip to content

Commit

Permalink
Refactor focus selector logic, better cleanup, lazier map copy
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Aug 26, 2024
1 parent 3e6dcbd commit fe16bbf
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/components/src/navigator/navigator-provider/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,32 @@ function goTo(
return { currentLocation, focusSelectors };
}

let focusSelectorsCopy;
let focusSelectorsCopy: typeof focusSelectors | undefined;
function getFocusSelectorsCopy() {
focusSelectorsCopy =
focusSelectorsCopy ?? new Map( state.focusSelectors );
return focusSelectorsCopy;
}

// Set a focus selector that will be used when navigating
// back to the current location.
if ( focusTargetSelector && currentLocation?.path ) {
if ( ! focusSelectorsCopy ) {
focusSelectorsCopy = new Map( state.focusSelectors );
}
focusSelectorsCopy.set( currentLocation.path, focusTargetSelector );
getFocusSelectorsCopy().set(
currentLocation.path,
focusTargetSelector
);
}

// Get the focus selector for the new location.
let currentFocusSelector;
if ( isBack ) {
if ( ! focusSelectorsCopy ) {
focusSelectorsCopy = new Map( state.focusSelectors );
if ( focusSelectors.get( path ) ) {
if ( isBack ) {
// Use the found focus selector only when navigating back.
currentFocusSelector = focusSelectors.get( path );
}
currentFocusSelector = focusSelectorsCopy.get( path );
focusSelectorsCopy.delete( path );
// Make a copy of the focusSelectors map to remove the focus selector
// only if necessary (ie. a focus selector was found).
getFocusSelectorsCopy().delete( path );
}

return {
Expand Down

0 comments on commit fe16bbf

Please sign in to comment.