@@ -324,26 +324,36 @@ abstract class _ReadRoute<ENTITY, LOGGED_IN_OR_NOT, LOCALS extends (Authenticate
324
324
// return;
325
325
// }
326
326
return new Promise < String | undefined > ( async ( resolve ) => {
327
- this . stopListening ( clientIdInternalApp , listenId , this . route ) ;
328
- const clientInfo = this . askless . clientMiddleware . clients . getOrCreateClientInfo ( clientIdInternalApp ) ;
327
+ this . askless . logger ( "listen @internal " + listenId , "debug" ) ;
328
+
329
+ const clientInfo = this . askless . clientMiddleware . clients . getOrCreateClientInfo ( clientIdInternalApp ) ;
329
330
// const start = Date.now();
330
331
const locals = Object . assign ( { } , clientInfo . locals ) ;
331
332
332
- clientInfo . routesBeingListen . push ( {
333
- clientIdInternalApp : clientIdInternalApp ,
334
- listenId : listenId ,
335
- route : this . route ,
336
- params : params ,
337
- locals : locals ,
338
- authenticationStatus : this . authenticationStatus ,
339
- } ) ;
340
-
333
+ if ( listenId ) {
334
+ const existingIndex = clientInfo . routesBeingListen . findIndex ( ( c ) => c . listenId == listenId ) ;
335
+ const prev = existingIndex != - 1 ? clientInfo . routesBeingListen [ existingIndex ] : { } ;
336
+ const data = Object . assign ( prev , {
337
+ clientIdInternalApp : clientIdInternalApp ,
338
+ listenId : listenId ,
339
+ route : this . route ,
340
+ params : params ,
341
+ locals : locals ,
342
+ authenticationStatus : this . authenticationStatus ,
343
+ at : new Date ( ) ,
344
+ } ) ;
345
+ if ( existingIndex != - 1 ) {
346
+ clientInfo . routesBeingListen [ existingIndex ] = data ;
347
+ } else {
348
+ clientInfo . routesBeingListen . push ( data ) ;
349
+ }
350
+ }
341
351
let _getEntity : ( ) => ENTITY ;
342
352
let response ;
343
353
if ( this . authenticationStatus == "authenticatedOnly" && clientInfo . authentication != "authenticated" ) {
344
354
response = new AsklessError ( {
345
355
code : AsklessErrorCode . PENDING_AUTHENTICATION ,
346
- description : "Could not perform the operation on (LISTEN) \"" + this . route + "\" because authentication is required" ,
356
+ description : "Could not perform the operation on (LISTEN) \"" + this . route + "\" because authentication is required, but user authentication is " + clientInfo . authentication ,
347
357
} ) ;
348
358
} else {
349
359
response = await this . readInternal ( {
@@ -384,7 +394,8 @@ abstract class _ReadRoute<ENTITY, LOGGED_IN_OR_NOT, LOCALS extends (Authenticate
384
394
) ;
385
395
if ( error ?. code == AsklessErrorCode . PERMISSION_DENIED || error ?. code == AsklessErrorCode . PENDING_AUTHENTICATION ) {
386
396
this . askless . logger ( "listen: the error is \"" + error . code + "\", calling stopListening..." , "error" , error ) ;
387
- this . stopListening ( clientIdInternalApp , listenId , this . route ) ;
397
+ this . askless . logger ( "stopListening #4" , "debug" ) ;
398
+ await this . stopListening ( clientIdInternalApp , listenId , this . route ) ;
388
399
this . askless . logger ( "onClientStartsListening not called" , "error" ) ;
389
400
}
390
401
resolve ( undefined ) ;
@@ -461,10 +472,10 @@ abstract class _ReadRoute<ENTITY, LOGGED_IN_OR_NOT, LOCALS extends (Authenticate
461
472
toOutput ( entity : ENTITY ) :any { } ;
462
473
463
474
/** @internal */
464
- stopListening ( clientIdInternalApp : string ,
475
+ async stopListening ( clientIdInternalApp : string ,
465
476
listenId ?:string ,
466
- route ?: string
467
- ) : void {
477
+ route ?: string ,
478
+ ) : Promise < void > {
468
479
if ( clientIdInternalApp == null ) throw Error ( "clientIdInternalApp is undefined" ) ;
469
480
if ( listenId == null && route != null )
470
481
throw Error ( "please, inform the listenId" ) ;
@@ -483,19 +494,21 @@ abstract class _ReadRoute<ENTITY, LOGGED_IN_OR_NOT, LOCALS extends (Authenticate
483
494
if ( ! remove . length ) {
484
495
this . askless . logger ( "stopListening: NO item for " + ( this . route ?? 'unknown route' ) + " listenId: " + listenId + ' ' + JSON . stringify ( clientInfo . routesBeingListen . map ( e => e . listenId + " (" + e . route + ")" ) ) , "debug" ) ;
485
496
}
486
- remove . forEach ( async ( p ) => {
487
- const context :AuthenticateUserContext < any > & ClientAndRouteContext = {
488
- userId : clientInfo . userId ,
489
- claims : clientInfo . claims ,
490
- locals : p . locals ,
491
- params : p . params ,
492
- } ;
493
- await this . onClientStopsListening ( context as any ) ;
494
- } ) ;
495
- remove . forEach ( ( r ) => {
496
- clientInfo . routesBeingListen . splice ( clientInfo . routesBeingListen . indexOf ( r ) , 1 ) ;
497
- }
498
- ) ;
497
+ for ( let r = 0 ; r < remove . length ; r ++ ) {
498
+ clientInfo . routesBeingListen . splice ( clientInfo . routesBeingListen . indexOf ( remove [ r ] ) , 1 ) ;
499
+ }
500
+ for ( let r = 0 ; r < remove . length ; r ++ ) {
501
+ const p = remove [ r ] ;
502
+ if ( ! clientInfo . routesBeingListen . find ( ( r => r . route == p . route ) ) ) {
503
+ const context : AuthenticateUserContext < any > & ClientAndRouteContext = {
504
+ userId : clientInfo . userId ,
505
+ claims : clientInfo . claims ,
506
+ locals : p . locals ,
507
+ params : p . params ,
508
+ } ;
509
+ await this . onClientStopsListening ( context as any ) ;
510
+ }
511
+ }
499
512
}
500
513
}
501
514
0 commit comments