@@ -19,6 +19,8 @@ let menuState = {
1919 selectedIndex : 1 ,
2020} ;
2121
22+ let currentMenuBuild = null ;
23+
2224// Only assign to window if it exists (renderer context)
2325if ( typeof window !== 'undefined' ) {
2426 window . onMenuKeyDown = function onMenuKeyDown ( event ) {
@@ -973,6 +975,12 @@ export function openPlatformMenu(platformName, context, eltToFocus) {
973975 platformName = 'settings' ;
974976 }
975977
978+ // Prevent multiple concurrent menu builds
979+ if ( currentMenuBuild ) {
980+ console . log ( "Menu build already in progress, ignoring duplicate call" ) ;
981+ return ;
982+ }
983+
976984 LB . mode = 'menu' ;
977985 LB . currentPlatform = platformName ;
978986
@@ -986,7 +994,16 @@ export function openPlatformMenu(platformName, context, eltToFocus) {
986994 menu . dataset . menuPlatform = platformName ;
987995 menu . dataset . context = context || null ;
988996
989- buildPlatformMenuForm ( platformName ) . then ( platformMenuForm => menu . appendChild ( platformMenuForm ) ) ;
997+ currentMenuBuild = buildPlatformMenuForm ( platformName ) . then ( platformMenuForm => {
998+ // Only append if this is still the current build
999+ if ( currentMenuBuild ) {
1000+ menu . appendChild ( platformMenuForm ) ;
1001+ currentMenuBuild = null ; // Clear the build reference
1002+ }
1003+ } ) . catch ( error => {
1004+ console . error ( 'Failed to build platform menu:' , error ) ;
1005+ currentMenuBuild = null ; // Clear on error too
1006+ } ) ;
9901007
9911008 const header = document . getElementById ( 'header' ) ;
9921009
@@ -1029,10 +1046,20 @@ function focusElement(eltToFocus, menu) {
10291046
10301047async function closeSettingsOrPlatformMenu ( ) {
10311048
1049+ console . log ( "closeSettingsOrPlatformMenu: " ) ;
1050+
1051+ // Cancel any ongoing menu build
1052+ if ( currentMenuBuild ) {
1053+ currentMenuBuild = null ;
1054+ }
1055+
10321056 const menu = document . getElementById ( 'menu' ) ;
10331057
10341058 // updateFooterControls('dpad', 'same', 'Browse', 'on');
10351059
1060+ menu . innerHTML = '' ;
1061+ menu . style . height = '0' ;
1062+
10361063 if ( menu . dataset . context === 'slideshow' ) {
10371064 initSlideShow ( LB . currentPlatform ) ;
10381065 } else if ( menu . dataset . context === 'gallery' ) {
@@ -1041,8 +1068,7 @@ async function closeSettingsOrPlatformMenu() {
10411068 initGallery ( 'settings' ) ;
10421069 }
10431070
1044- menu . innerHTML = '' ;
1045- menu . style . height = '0' ;
1071+ console . log ( "menu.innerHTML: " , menu . innerHTML ) ;
10461072
10471073}
10481074
0 commit comments