@@ -73,45 +73,20 @@ function handleAsyncHandlerResult(result: unknown, route: RouteObject, handlerKe
73
73
'then' in result &&
74
74
typeof ( result as Promise < unknown > ) . then === 'function'
75
75
) {
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
+ } ) ;
77
85
} else if ( Array . isArray ( result ) ) {
78
- handleSynchronousArrayResult ( result , route ) ;
86
+ processResolvedRoutes ( result ) ;
79
87
}
80
88
}
81
89
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
-
115
90
/**
116
91
* Processes resolved routes by adding them to allRoutes and checking for nested async handlers.
117
92
*/
@@ -139,28 +114,19 @@ function createAsyncHandlerProxy(
139
114
} ) ;
140
115
}
141
116
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
-
158
117
/**
159
118
* Recursively checks a route for async handlers and sets up Proxies to add discovered child routes to allRoutes when called.
160
119
*/
161
120
export function checkRouteForAsyncHandler ( route : RouteObject ) : void {
162
121
// 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
+ }
164
130
165
131
// Recursively check child routes
166
132
if ( Array . isArray ( route . children ) ) {
@@ -193,14 +159,9 @@ export function createV6CompatibleWrapCreateBrowserRouter<
193
159
addRoutesToAllRoutes ( routes ) ;
194
160
195
161
// 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
+ }
204
165
205
166
const router = createRouterFunction ( routes , opts ) ;
206
167
const basename = opts ?. basename ;
0 commit comments