@@ -188,23 +188,24 @@ describe('standardOpenAPIMatcher', () => {
188188 it ( 'lazy router inside lazy router with concurrent requests' , async ( ) => {
189189 const base = os . $context < any > ( )
190190
191- let resolveNested ! : ( value : { default : any } ) => void
191+ const resolvers : ( ( value : { default : any } ) => void ) [ ] = [ ]
192192 const nestedLoader = vi . fn ( ( ) => new Promise < { default : any } > ( ( resolve ) => {
193- resolveNested = resolve
193+ resolvers . push ( resolve )
194194 } ) )
195195 const pongLoader = vi . fn ( ( ) => Promise . resolve ( { default : pong } ) )
196196
197197 const rpcMatcher = new StandardOpenAPIMatcher ( )
198198 rpcMatcher . init ( base . router ( {
199199 nested : base . lazy ( nestedLoader ) ,
200200 } ) )
201+ const initSpy = vi . spyOn ( rpcMatcher , 'init' )
201202
202203 const match1 = rpcMatcher . match ( 'POST' , '/nested/pong' )
203204 const match2 = rpcMatcher . match ( 'POST' , '/nested/pong' )
204205
205- expect ( nestedLoader ) . toHaveBeenCalledTimes ( 1 )
206+ expect ( nestedLoader ) . toHaveBeenCalledTimes ( 2 )
206207
207- resolveNested ( { default : { pong : base . lazy ( pongLoader ) } } )
208+ resolvers . forEach ( resolve => resolve ( { default : { pong : base . lazy ( pongLoader ) } } ) )
208209
209210 expect ( await match1 ) . toEqual ( {
210211 path : [ 'nested' , 'pong' ] ,
@@ -216,17 +217,82 @@ describe('standardOpenAPIMatcher', () => {
216217 procedure : pong ,
217218 } )
218219
219- expect ( nestedLoader ) . toHaveBeenCalledTimes ( 1 )
220- expect ( pongLoader ) . toHaveBeenCalledTimes ( 1 )
221-
222220 // subsequent requests still work without reloading
223221 expect ( await rpcMatcher . match ( 'POST' , '/nested/pong' ) ) . toEqual ( {
224222 path : [ 'nested' , 'pong' ] ,
225223 procedure : pong ,
226224 } )
227225
228- expect ( nestedLoader ) . toHaveBeenCalledTimes ( 1 )
229- expect ( pongLoader ) . toHaveBeenCalledTimes ( 1 )
226+ expect ( initSpy . mock . calls . map ( ( [ , path ] ) => path ) ) . toEqual ( [ [ 'nested' ] , [ 'nested' , 'pong' ] ] )
227+ } )
228+
229+ it ( 'matches while another match is still loading, and drops the slower load' , async ( ) => {
230+ const base = os . $context < any > ( )
231+
232+ let resolveFirst ! : ( value : { default : any } ) => void
233+ const nestedLoader = vi . fn ( )
234+ . mockReturnValueOnce ( new Promise ( ( resolve ) => {
235+ resolveFirst = resolve
236+ } ) )
237+ . mockResolvedValueOnce ( { default : { pong } } )
238+
239+ const rpcMatcher = new StandardOpenAPIMatcher ( )
240+ rpcMatcher . init ( base . router ( {
241+ nested : base . lazy ( nestedLoader ) ,
242+ } ) )
243+ const initSpy = vi . spyOn ( rpcMatcher , 'init' )
244+
245+ const slow = rpcMatcher . match ( 'POST' , '/nested/pong' )
246+
247+ expect ( await rpcMatcher . match ( 'POST' , '/nested/pong' ) ) . toEqual ( {
248+ path : [ 'nested' , 'pong' ] ,
249+ procedure : pong ,
250+ } )
251+
252+ // the slower load resolves to a different router
253+ resolveFirst ( { default : { pong : ping } } )
254+
255+ expect ( await slow ) . toEqual ( {
256+ path : [ 'nested' , 'pong' ] ,
257+ procedure : pong ,
258+ } )
259+
260+ expect ( initSpy ) . toHaveBeenCalledTimes ( 1 )
261+ } )
262+
263+ it ( 'indexes lazy routers sharing a prefix once each while one load is stuck' , async ( ) => {
264+ const base = os . $context < any > ( )
265+
266+ const find = base . route ( { method : 'GET' , path : '/find' } ) . handler ( ( ) => 'find' )
267+ const list = base . route ( { method : 'GET' , path : '/list' } ) . handler ( ( ) => 'list' )
268+
269+ let resolveStuck ! : ( value : { default : any } ) => void
270+ const findLoader = vi . fn ( )
271+ . mockReturnValueOnce ( new Promise ( ( resolve ) => {
272+ resolveStuck = resolve
273+ } ) )
274+ . mockResolvedValue ( { default : { find } } )
275+
276+ const rpcMatcher = new StandardOpenAPIMatcher ( )
277+ rpcMatcher . init ( base . router ( {
278+ a : base . prefix ( '/users' ) . lazy ( findLoader ) ,
279+ b : base . prefix ( '/users' ) . lazy ( ( ) => Promise . resolve ( { default : { list } } ) ) ,
280+ } ) )
281+ const initSpy = vi . spyOn ( rpcMatcher , 'init' )
282+
283+ const stuck = rpcMatcher . match ( 'GET' , '/users/find' )
284+ const [ found , listed ] = await Promise . all ( [
285+ rpcMatcher . match ( 'GET' , '/users/find' ) ,
286+ rpcMatcher . match ( 'GET' , '/users/list' ) ,
287+ ] )
288+
289+ expect ( found ) . toEqual ( { path : [ 'a' , 'find' ] , procedure : expect . any ( Object ) , params : undefined } )
290+ expect ( listed ) . toEqual ( { path : [ 'b' , 'list' ] , procedure : expect . any ( Object ) , params : undefined } )
291+
292+ resolveStuck ( { default : { find } } )
293+
294+ expect ( await stuck ) . toEqual ( { path : [ 'a' , 'find' ] , procedure : expect . any ( Object ) , params : undefined } )
295+ expect ( initSpy . mock . calls . map ( ( [ , path ] ) => path ! . join ( '/' ) ) . sort ( ) ) . toEqual ( [ 'a' , 'b' ] )
230296 } )
231297
232298 it ( 'multiple lazy routers with lazy router inside lazy router' , async ( ) => {
@@ -260,6 +326,7 @@ describe('standardOpenAPIMatcher', () => {
260326 planets : base . prefix ( '/planets' ) . lazy ( planetsLoader ) ,
261327 unused : base . prefix ( '/unused' ) . lazy ( unusedLoader ) ,
262328 } ) )
329+ const initSpy = vi . spyOn ( rpcMatcher , 'init' )
263330
264331 // concurrent requests across different lazy branches
265332 const [ match1 , match2 , match3 ] = await Promise . all ( [
@@ -283,12 +350,6 @@ describe('standardOpenAPIMatcher', () => {
283350 procedure : pong ,
284351 } )
285352
286- expect ( usersLoader ) . toHaveBeenCalledTimes ( 1 )
287- expect ( userFindLoader ) . toHaveBeenCalledTimes ( 1 )
288- expect ( planetsLoader ) . toHaveBeenCalledTimes ( 1 )
289- expect ( planetNestedLoader ) . toHaveBeenCalledTimes ( 1 )
290- expect ( planetDeepLoader ) . toHaveBeenCalledTimes ( 1 )
291-
292353 // prefixed router not matched by any request stays lazy
293354 expect ( unusedLoader ) . toHaveBeenCalledTimes ( 0 )
294355
@@ -298,10 +359,8 @@ describe('standardOpenAPIMatcher', () => {
298359 procedure : pong ,
299360 } )
300361
301- expect ( planetsLoader ) . toHaveBeenCalledTimes ( 1 )
302- expect ( planetNestedLoader ) . toHaveBeenCalledTimes ( 1 )
303- expect ( planetDeepLoader ) . toHaveBeenCalledTimes ( 1 )
304362 expect ( unusedLoader ) . toHaveBeenCalledTimes ( 0 )
363+ expect ( initSpy . mock . calls . map ( ( [ , path ] ) => path ! . join ( '.' ) ) . sort ( ) ) . toEqual ( [ 'planets' , 'planets.nested' , 'planets.nested.deep' , 'users' , 'users.find' ] )
305364 } )
306365
307366 it ( 'lazy router can retry after loader failure' , async ( ) => {
0 commit comments