Skip to content

Commit

Permalink
fix(navigation): resolve performance issue (#1092)
Browse files Browse the repository at this point in the history
The `blur`, `focus`, and `state` listeners all can perform heavy dom
mutations. Performing them at the same time the screen is rendering and
transitioning can cause delays or visual glitches. To resolve, these
listeners are delayed a small amount.

The use of `setTimeout` enables waiting for the screen to render before
mutating the dom. The `navStateMutationsDelay` experiment value is used
to enable the timeout. If the experiment is not set or has a value of
"0", no timeout is used. See
35499d9

| Before | After |
| -- | -- |
|
![before](https://github.com/user-attachments/assets/4563f32e-7c17-47ce-b1e9-d0eb4b50ad64)
|
![after](https://github.com/user-attachments/assets/0f1ab29e-8aed-4bf9-8ec0-c3845acd0960)
|


[Asana](https://app.asana.com/0/1204008699308084/1209403666139702)
  • Loading branch information
hgray-instawork authored Feb 26, 2025
1 parent 99f2b11 commit 248e31a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 24 deletions.
56 changes: 40 additions & 16 deletions src/core/components/hv-route/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,31 @@ function HvRouteFC(props: Types.Props) {

// Use the focus event to set the selected route
const unsubscribeFocus: () => void = nav.addListener('focus', () => {
const doc = docContext?.getDoc();
NavigatorService.setSelected(doc, id, docContext?.setDoc);
NavigatorService.addStackRoute(
doc,
id,
props.route,
nav.getState().routes[0]?.name,
navigationContext.entrypointUrl,
docContext?.setDoc,
);
if (navigationContext.onRouteFocus && props.route) {
navigationContext.onRouteFocus(props.route);
}
const navStateMutationsDelay =
navigationContext.experimentalFeatures?.navStateMutationsDelay || 0;
const updateRouteFocus = () => {
const doc = docContext?.getDoc();
NavigatorService.setSelected(doc, id, docContext?.setDoc);
NavigatorService.addStackRoute(
doc,
id,
props.route,
nav.getState().routes[0]?.name,
navigationContext.entrypointUrl,
docContext?.setDoc,
);
};
if (navStateMutationsDelay > 0) {
// The timeout ensures the processing occurs after the screen is rendered or shown
setTimeout(() => {
updateRouteFocus();
}, navStateMutationsDelay);
} else {
updateRouteFocus();
}
});

// Use the beforeRemove event to remove the route from the stack
Expand Down Expand Up @@ -621,12 +633,24 @@ function HvRouteFC(props: Types.Props) {

// Update the urls in each route when the state updates the params
const unsubscribeState: () => void = nav.addListener('state', event => {
NavigatorService.updateRouteUrlFromState(
docContext?.getDoc(),
id,
event.data?.state,
docContext?.setDoc,
);
const navStateMutationsDelay =
navigationContext.experimentalFeatures?.navStateMutationsDelay || 0;
const updateRouteUrlFromState = (e: NavigatorService.ListenerEvent) => {
NavigatorService.updateRouteUrlFromState(
docContext?.getDoc(),
id,
e.data?.state,
docContext?.setDoc,
);
};
if (navStateMutationsDelay > 0) {
// The timeout ensures the processing occurs after the screen is rendered or shown
setTimeout(() => {
updateRouteUrlFromState(event);
}, navStateMutationsDelay);
} else {
updateRouteUrlFromState(event);
}
});

return () => {
Expand Down
2 changes: 2 additions & 0 deletions src/core/components/hv-route/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as NavigatorService from 'hyperview/src/services/navigator';
import { ComponentType, ReactNode } from 'react';
import {
ExperimentalFeatures,
Fetch,
HvBehavior,
HvComponent,
Expand Down Expand Up @@ -29,6 +30,7 @@ export type NavigationContextProps = {
loadingScreen?: ComponentType<LoadingProps>;
handleBack?: ComponentType<{ children: ReactNode }>;
reload: Reload;
experimentalFeatures?: ExperimentalFeatures;
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/services/navigator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export class Navigator implements NavigationProvider {
}

export type {
ListenerEvent,
NavigationComponents,
NavigationProp,
NavigatorProps,
Expand Down
15 changes: 8 additions & 7 deletions src/services/navigator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export type NavigationNavigateParams = {
params?: NavigationNavigateParams | NavigationRouteParams;
};

export type ListenerEvent = {
data: { state: NavigationState | undefined } | undefined;
preventDefault: () => void;
};

export type ListenerCallback = (event: ListenerEvent) => void;

/**
* Minimal representation of the 'NavigationProp' used by react-navigation
*/
Expand All @@ -36,13 +43,7 @@ export type NavigationProp = {
goBack: () => void;
getState: () => NavigationState;
getParent: (id?: string) => NavigationProp | undefined;
addListener: (
eventName: string,
callback: (event: {
data: { state: NavigationState | undefined } | undefined;
preventDefault: () => void;
}) => void,
) => () => void;
addListener: (eventName: string, callback: ListenerCallback) => () => void;
isFocused: () => boolean;
};

Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,8 @@ export type Reload = (
opts: HvComponentOptions,
) => void;

export type ExperimentalFeatures = Record<string, never>;
export type ExperimentalFeatures = {
// Delay the mutation of the navigation state until after the screen has been rendered
// This is intended to improve the performance of navigation actions
navStateMutationsDelay?: number;
};

0 comments on commit 248e31a

Please sign in to comment.