@@ -903,23 +903,64 @@ class InfraNodus {
903903 }
904904 }
905905
906- public static async getUserId ( params : { headerToken : string } ) {
907- try {
908- const postResult = await this . genericPost (
909- "api/v1/userId" ,
910- {
911- headerToken : params . headerToken ,
912- } ,
913- { credentials : "include" }
914- ) ;
906+ // Cached InfraNodus user id, keyed by the API key it was fetched with —
907+ // the id never changes for a given key, so one fetch per session is
908+ // enough. Keeping the iframe URL stable from the first render also
909+ // prevents the graph viewer from rebooting mid-load when the id arrives
910+ private static userIdCache : { apiKey : string ; userId : string } | null =
911+ null ;
912+ private static userIdInFlight : {
913+ apiKey : string ;
914+ promise : Promise < { userId ?: string ; error ?: boolean } > ;
915+ } | null = null ;
916+
917+ public static getCachedUserId ( ) : string | null {
918+ const apiKey = SETTINGS . INFRANODUS_API_KEY ;
919+ return apiKey && InfraNodus . userIdCache ?. apiKey === apiKey
920+ ? InfraNodus . userIdCache . userId
921+ : null ;
922+ }
915923
916- console . log ( "postResult" , postResult ) ;
924+ public static async getUserId ( params : {
925+ headerToken : string ;
926+ } ) : Promise < { userId ?: string ; error ?: boolean } > {
927+ const apiKey = params . headerToken ;
917928
918- return { userId : postResult . data . userId } ;
919- } catch ( err ) {
920- console . error ( "Error when submitting content to InfraNodus" , err ) ;
921- return { error : true } ;
929+ if ( InfraNodus . userIdCache ?. apiKey === apiKey ) {
930+ return { userId : InfraNodus . userIdCache . userId } ;
922931 }
932+ if ( InfraNodus . userIdInFlight ?. apiKey === apiKey ) {
933+ return InfraNodus . userIdInFlight . promise ;
934+ }
935+
936+ const promise = ( async ( ) => {
937+ try {
938+ const postResult = await this . genericPost (
939+ "api/v1/userId" ,
940+ {
941+ headerToken : apiKey ,
942+ } ,
943+ { credentials : "include" }
944+ ) ;
945+
946+ const userId = postResult . data . userId ;
947+ if ( userId ) InfraNodus . userIdCache = { apiKey, userId } ;
948+
949+ return { userId } ;
950+ } catch ( err ) {
951+ console . error (
952+ "Error when getting the user id from InfraNodus" ,
953+ err
954+ ) ;
955+ return { error : true } ;
956+ } finally {
957+ if ( InfraNodus . userIdInFlight ?. apiKey === apiKey )
958+ InfraNodus . userIdInFlight = null ;
959+ }
960+ } ) ( ) ;
961+
962+ InfraNodus . userIdInFlight = { apiKey, promise } ;
963+ return promise ;
923964 }
924965}
925966
0 commit comments