Skip to content

Commit d8216c1

Browse files
committed
Simplify
1 parent 244a953 commit d8216c1

File tree

1 file changed

+21
-60
lines changed

1 file changed

+21
-60
lines changed

packages/react/src/reactrouterv6-compat-utils.tsx

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,45 +73,20 @@ function handleAsyncHandlerResult(result: unknown, route: RouteObject, handlerKe
7373
'then' in result &&
7474
typeof (result as Promise<unknown>).then === 'function'
7575
) {
76-
handlePromiseResult(result as Promise<unknown>, route, handlerKey);
76+
(result as Promise<unknown>)
77+
.then((resolvedRoutes: unknown) => {
78+
if (Array.isArray(resolvedRoutes)) {
79+
processResolvedRoutes(resolvedRoutes);
80+
}
81+
})
82+
.catch((e: unknown) => {
83+
DEBUG_BUILD && debug.warn(`Error resolving async handler '${handlerKey}' for route`, route, e);
84+
});
7785
} else if (Array.isArray(result)) {
78-
handleSynchronousArrayResult(result, route);
86+
processResolvedRoutes(result);
7987
}
8088
}
8189

82-
/**
83-
* Handles promise results from async handlers.
84-
*/
85-
function handlePromiseResult(promise: Promise<unknown>, route: RouteObject, handlerKey: string): void {
86-
promise
87-
.then((resolvedRoutes: unknown) => {
88-
if (Array.isArray(resolvedRoutes)) {
89-
addResolvedRoutesToParent(resolvedRoutes, route);
90-
processResolvedRoutes(resolvedRoutes);
91-
}
92-
})
93-
.catch((e: unknown) => {
94-
DEBUG_BUILD && debug.warn(`Error resolving async handler '${handlerKey}' for route`, route, e);
95-
});
96-
}
97-
98-
/**
99-
* Handles synchronous array results from handlers.
100-
*/
101-
function handleSynchronousArrayResult(result: RouteObject[], route: RouteObject): void {
102-
addResolvedRoutesToParent(result, route);
103-
processResolvedRoutes(result);
104-
}
105-
106-
/**
107-
* Adds resolved routes as children to the parent route.
108-
*/
109-
function addResolvedRoutesToParent(resolvedRoutes: RouteObject[], parentRoute: RouteObject): void {
110-
parentRoute.children = Array.isArray(parentRoute.children)
111-
? [...parentRoute.children, ...resolvedRoutes]
112-
: resolvedRoutes;
113-
}
114-
11590
/**
11691
* Processes resolved routes by adding them to allRoutes and checking for nested async handlers.
11792
*/
@@ -139,28 +114,19 @@ function createAsyncHandlerProxy(
139114
});
140115
}
141116

142-
/**
143-
* Sets up proxies for all function properties in a route's handle object.
144-
*/
145-
function setupHandleProxies(route: RouteObject): void {
146-
if (!route.handle || typeof route.handle !== 'object') {
147-
return;
148-
}
149-
150-
for (const key of Object.keys(route.handle)) {
151-
const maybeFn = route.handle[key];
152-
if (typeof maybeFn === 'function') {
153-
route.handle[key] = createAsyncHandlerProxy(maybeFn, route, key);
154-
}
155-
}
156-
}
157-
158117
/**
159118
* Recursively checks a route for async handlers and sets up Proxies to add discovered child routes to allRoutes when called.
160119
*/
161120
export function checkRouteForAsyncHandler(route: RouteObject): void {
162121
// Set up proxies for any functions in the route's handle
163-
setupHandleProxies(route);
122+
if (route.handle && typeof route.handle === 'object') {
123+
for (const key of Object.keys(route.handle)) {
124+
const maybeFn = route.handle[key];
125+
if (typeof maybeFn === 'function') {
126+
route.handle[key] = createAsyncHandlerProxy(maybeFn, route, key);
127+
}
128+
}
129+
}
164130

165131
// Recursively check child routes
166132
if (Array.isArray(route.children)) {
@@ -193,14 +159,9 @@ export function createV6CompatibleWrapCreateBrowserRouter<
193159
addRoutesToAllRoutes(routes);
194160

195161
// Check for async handlers that might contain sub-route declarations
196-
const checkAsyncHandlers = (): void => {
197-
for (const route of routes) {
198-
checkRouteForAsyncHandler(route);
199-
}
200-
};
201-
202-
// Start checking async handlers
203-
checkAsyncHandlers();
162+
for (const route of routes) {
163+
checkRouteForAsyncHandler(route);
164+
}
204165

205166
const router = createRouterFunction(routes, opts);
206167
const basename = opts?.basename;

0 commit comments

Comments
 (0)