Skip to content
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(navigation): resolve performance issue #1092

Merged
merged 7 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};